Ejemplo n.º 1
0
    def _build_kml_sources(self, sources):
        """
        Go through the given sources and return a 3-tuple of the application
        label, module name, and field name of every GeometryField encountered
        in the sources.

        If no sources are provided, then all models.
        """
        kml_sources = []
        if sources is None:
            sources = apps.get_models()
        for source in sources:
            if isinstance(source, models.base.ModelBase):
                for field in source._meta.fields:
                    if isinstance(field, GeometryField):
                        kml_sources.append(
                            (source._meta.app_label, source._meta.model_name,
                             field.name))
            elif isinstance(source, (list, tuple)):
                if len(source) != 3:
                    raise ValueError(
                        'Must specify a 3-tuple of (app_label, module_name, field_name).'
                    )
                kml_sources.append(source)
            else:
                raise TypeError('KML Sources must be a model or a 3-tuple.')
        return kml_sources
Ejemplo n.º 2
0
def check_generic_foreign_keys(app_configs=None, **kwargs):
    from .fields import GenericForeignKey

    if app_configs is None:
        models = apps.get_models()
    else:
        models = chain.from_iterable(app_config.get_models()
                                     for app_config in app_configs)
    errors = []
    fields = (obj for model in models for obj in vars(model).values()
              if isinstance(obj, GenericForeignKey))
    for field in fields:
        errors.extend(field.check())
    return errors
Ejemplo n.º 3
0
def check_model_name_lengths(app_configs=None, **kwargs):
    if app_configs is None:
        models = apps.get_models()
    else:
        models = chain.from_iterable(app_config.get_models()
                                     for app_config in app_configs)
    errors = []
    for model in models:
        if len(model._meta.model_name) > 100:
            errors.append(
                Error(
                    'Model names must be at most 100 characters (got %d).' %
                    (len(model._meta.model_name), ),
                    obj=model,
                    id='contenttypes.E005',
                ))
    return errors
Ejemplo n.º 4
0
def check_all_models(app_configs=None, **kwargs):
    errors = []
    if app_configs is None:
        models = apps.get_models()
    else:
        models = chain.from_iterable(app_config.get_models() for app_config in app_configs)
    for model in models:
        if not inspect.ismethod(model.check):
            errors.append(
                Error(
                    "The '%s.check()' class method is currently overridden by %r."
                    % (model.__name__, model.check),
                    obj=model,
                    id='models.E020'
                )
            )
        else:
            errors.extend(model.check(**kwargs))
    return errors
Ejemplo n.º 5
0
 def get_context_data(self, **kwargs):
     m_list = [m._meta for m in apps.get_models()]
     return super().get_context_data(**{**kwargs, 'models': m_list})
Ejemplo n.º 6
0
def check_models_permissions(app_configs=None, **kwargs):
    if app_configs is None:
        models = apps.get_models()
    else:
        models = chain.from_iterable(app_config.get_models()
                                     for app_config in app_configs)

    Permission = apps.get_model('auth', 'Permission')
    permission_name_max_length = Permission._meta.get_field('name').max_length
    errors = []

    for model in models:
        opts = model._meta
        builtin_permissions = dict(_get_builtin_permissions(opts))
        # Check builtin permission name length.
        max_builtin_permission_name_length = (max(
            len(name) for name in builtin_permissions.values())
                                              if builtin_permissions else 0)
        if max_builtin_permission_name_length > permission_name_max_length:
            verbose_name_max_length = (permission_name_max_length -
                                       (max_builtin_permission_name_length -
                                        len(opts.verbose_name_raw)))
            errors.append(
                checks.Error(
                    "The verbose_name of model '%s.%s' must be at most %d characters "
                    "for its builtin permission names to be at most %d characters."
                    % (opts.app_label, opts.object_name,
                       verbose_name_max_length, permission_name_max_length),
                    obj=model,
                    id='auth.E007',
                ))
        codenames = set()
        for codename, name in opts.permissions:
            # Check custom permission name length.
            if len(name) > permission_name_max_length:
                errors.append(
                    checks.Error(
                        "The permission named '%s' of model '%s.%s' is longer than %d characters."
                        % (name, opts.app_label, opts.object_name,
                           permission_name_max_length),
                        obj=model,
                        id='auth.E008',
                    ))
            # Check custom permissions codename clashing.
            if codename in builtin_permissions:
                errors.append(
                    checks.Error(
                        "The permission codenamed '%s' clashes with a builtin permission "
                        "for model '%s.%s'." %
                        (codename, opts.app_label, opts.object_name),
                        obj=model,
                        id='auth.E005',
                    ))
            elif codename in codenames:
                errors.append(
                    checks.Error(
                        "The permission codenamed '%s' is duplicated for model '%s.%s'."
                        % (codename, opts.app_label, opts.object_name),
                        obj=model,
                        id='auth.E006',
                    ))
            codenames.add(codename)

    return errors