Returns (model, perm,) for all models that match models/exclude patterns
    and are visible by current user.
765293
def filter_models(request, models, exclude):
    
    items = get_avail_models(request)
    included = []

    def full_name(model):
        return '%s.%s' % (model.__module__, model.__name__)

    # I beleive that that implemented
    # O(len(patterns)*len(matched_patterns)*len(all_models))
    # algorythm is fine for model lists because they are small and admin
    # performance is not a bottleneck. If it is not the case then the code
    # should be optimized.

    if len(models) == 0:
        included = items
    else:
        for pattern in models:
            wildcard_models = []
            for item in items:
                model, perms = item
                model_str = full_name(model)
                if model_str == pattern:
                    # exact match
                    included.append(item)
                elif fnmatch(model_str, pattern) and \
                        item not in wildcard_models:
                    # wildcard match, put item in separate list so it can be
                    # sorted alphabetically later
                    wildcard_models.append(item)
            if wildcard_models:
                # sort wildcard matches alphabetically before adding them
                wildcard_models.sort(
                    key=lambda x: x[0]._meta.verbose_name_plural
                )
                included += wildcard_models

    result = included[:]
    for pattern in exclude:
        for item in included:
            model, perms = item
            if fnmatch(full_name(model), pattern):
                try:
                    result.remove(item)
                except ValueError:  # if the item was already removed skip
                    pass
    return result
15547
def has_a_matching_perm(self, perm_list, obj=None):
        
        # If there are no permissions to check, just return true
        if not perm_list:
            return True
        # Check that user has at least one of the required permissions.
        for perm in perm_list:
            if self.has_perm(perm, obj):
                return True
        return False
990408
def extract_applicable_files(config, options)
      included_patterns = options[:files]
      excluded_patterns = config['exclude']
      excluded_patterns += options.fetch(:excluded_files, [])

      HamlLint::FileFinder.new(config).find(included_patterns, excluded_patterns)
    end
205281
def _find_models(self, constructor, table_name, constraints=None, *, columns=None,
                   order_by=None, limiting=None):
    """"""
    for record in self.find_all(table_name, constraints, columns=columns, order_by=order_by,
                                limiting=limiting):
      yield constructor(record)
1091690
def find_model(config, obj, mods):
    """"""
    for mod in mods:
        if mod[0] != config:
            continue

        if len(mod) == 2:
            return mod[1]

        if len(mod) == 3 and mod[1] in obj:
            return mod[2]

    return None
179151
def _get_custom_models(models):
    """"""
    if models is None:
        models = Model.model_class_reverse_map.values()

    custom_models = OrderedDict()
    for cls in models:
        impl = getattr(cls, "__implementation__", None)

        if impl is not None:
            model = CustomModel(cls)
            custom_models[model.full_name] = model

    if not custom_models:
        return None
    return custom_models
982375
def get_by_model(self, model):
        """"""
        content_type = ContentType.objects.get_for_model(model)
        return self.filter(content_type=content_type)
1095761
def find_models(self, constructor, constraints=None, *, columns=None, order_by=None,
                  limiting=None, table_name=None):
    """"""
    return self._find_models(
      constructor, table_name or constructor.table_name, constraints, columns=columns,
      order_by=order_by, limiting=limiting)
928931
def _reflect_all(exclude_tables=None, admin=None, read_only=False, schema=None):
    
    AutomapModel.prepare(  # pylint:disable=maybe-no-member
        db.engine, reflect=True, schema=schema)
    for cls in AutomapModel.classes:
        if exclude_tables and cls.__table__.name in exclude_tables:
            continue
        if read_only:
            cls.__methods__ = {'GET'}
        register_model(cls, admin)
555893
def _filter_queryset(self, perms, queryset):
        """"""
        user = self.request.user if self.request else AnonymousUser()
        return get_objects_for_user(user, perms, queryset)
479626
def get_model(name):
    

    model = MODELS.get(name.lower(), None)

    assert model, "Could not locate model by name '%s'" % name

    return model
811378
def by_user_and_perm(cls, user_id, perm_name, db_session=None):
        
        db_session = get_db_session(db_session)
        query = db_session.query(cls.model).filter(cls.model.user_id == user_id)
        query = query.filter(cls.model.perm_name == perm_name)
        return query.first()
143701
def get_follows(self, model_or_obj_or_qs):
        
        fname = self.fname(model_or_obj_or_qs)
        
        if isinstance(model_or_obj_or_qs, QuerySet):
            return self.filter(**{'%s__in' % fname: model_or_obj_or_qs})
        
        if inspect.isclass(model_or_obj_or_qs):
            return self.exclude(**{fname:None})

        return self.filter(**{fname:model_or_obj_or_qs})
1000778
def basic_search(self, pattern):
        
        lookup = None
        for pattern in pattern.split():
            query_part = models.Q()
            for field in SEARCH_FIELDS:
                query_part |= models.Q(**{'%s__icontains' % field: pattern})
            if lookup is None:
                lookup = query_part
            else:
                lookup |= query_part

        return self.get_queryset().filter(lookup)
1240486
def lookup_model_by_kind(kind):
    
    model = _known_models.get(kind)
    if model is None:
        raise RuntimeError(f"Model for kind {kind!r} not found.")
    return model
611794
def for_model(self, model, content_type=None):
        '''  '''
        content_type = content_type or ContentType.objects.get_for_model(model)
        kwargs = {
                    self.ct_field: content_type,
                    self.fk_field: model.pk
                 }
        objects = self.get_query_set().filter(**kwargs)
        return objects
83054
def _find_references(model_name, references=None):
    
    references = references or []
    references.append(model_name)

    ref = MODELS[model_name].get('reference')
    if ref is not None:
        _find_references(ref, references)

    parent_models = [m for m in references]
    parent_models.reverse()

    return parent_models