django.core.checks.Error

Here are the examples of the python api django.core.checks.Error taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

200 Examples 7

Example 1

Project: PyClassLessons
Source File: checks.py
View license
    def _check_exclude(self, cls, model):
        """ Check that exclude is a sequence without duplicates. """

        if cls.exclude is None:  # default value is None
            return []
        elif not isinstance(cls.exclude, (list, tuple)):
            return must_be('a list or tuple', option='exclude', obj=cls, id='admin.E014')
        elif len(cls.exclude) > len(set(cls.exclude)):
            return [
                checks.Error(
                    "The value of 'exclude' contains duplicate field(s).",
                    hint=None,
                    obj=cls,
                    id='admin.E015',
                )
            ]
        else:
            return []

Example 2

Project: PyClassLessons
Source File: checks.py
View license
    def _check_radio_fields_value(self, cls, model, val, label):
        """ Check type of a value of `radio_fields` dictionary. """

        from django.contrib.admin.options import HORIZONTAL, VERTICAL

        if val not in (HORIZONTAL, VERTICAL):
            return [
                checks.Error(
                    "The value of '%s' must be either admin.HORIZONTAL or admin.VERTICAL." % label,
                    hint=None,
                    obj=cls,
                    id='admin.E024',
                )
            ]
        else:
            return []

Example 3

Project: PyClassLessons
Source File: checks.py
View license
    def _check_view_on_site_url(self, cls, model):
        if hasattr(cls, 'view_on_site'):
            if not callable(cls.view_on_site) and not isinstance(cls.view_on_site, bool):
                return [
                    checks.Error(
                        "The value of 'view_on_site' must be a callable or a boolean value.",
                        hint=None,
                        obj=cls,
                        id='admin.E025',
                    )
                ]
            else:
                return []
        else:
            return []

Example 4

Project: PyClassLessons
Source File: checks.py
View license
    def _check_list_display_links_item(self, cls, model, field_name, label):
        if field_name not in cls.list_display:
            return [
                checks.Error(
                    "The value of '%s' refers to '%s', which is not defined in 'list_display'." % (
                        label, field_name
                    ),
                    hint=None,
                    obj=cls,
                    id='admin.E111',
                )
            ]
        else:
            return []

Example 5

Project: PyClassLessons
Source File: checks.py
View license
    def _check_relation(self, cls, parent_model):
        try:
            _get_foreign_key(parent_model, cls.model, fk_name=cls.fk_name)
        except ValueError as e:
            return [checks.Error(e.args[0], hint=None, obj=cls, id='admin.E202')]
        else:
            return []

Example 6

Project: PyClassLessons
Source File: checks.py
View license
def must_be(type, option, obj, id):
    return [
        checks.Error(
            "The value of '%s' must be %s." % (option, type),
            hint=None,
            obj=obj,
            id=id,
        ),
    ]

Example 7

Project: PyClassLessons
Source File: checks.py
View license
def must_inherit_from(parent, option, obj, id):
    return [
        checks.Error(
            "The value of '%s' must inherit from '%s'." % (option, parent),
            hint=None,
            obj=obj,
            id=id,
        ),
    ]

Example 8

Project: PyClassLessons
Source File: checks.py
View license
def refer_to_missing_field(field, option, model, obj, id):
    return [
        checks.Error(
            "The value of '%s' refers to '%s', which is not an attribute of '%s.%s'." % (
                option, field, model._meta.app_label, model._meta.object_name
            ),
            hint=None,
            obj=obj,
            id=id,
        ),
    ]

Example 9

Project: PyClassLessons
Source File: fields.py
View license
    def _check_field_name(self):
        if self.name.endswith("_"):
            return [
                checks.Error(
                    'Field names must not end with an underscore.',
                    hint=None,
                    obj=self,
                    id='fields.E001',
                )
            ]
        else:
            return []

Example 10

Project: PyClassLessons
Source File: fields.py
View license
    def _check_object_id_field(self):
        try:
            self.model._meta.get_field(self.fk_field)
        except FieldDoesNotExist:
            return [
                checks.Error(
                    "The GenericForeignKey object ID references the non-existent field '%s'." % self.fk_field,
                    hint=None,
                    obj=self,
                    id='contenttypes.E001',
                )
            ]
        else:
            return []

Example 11

Project: PyClassLessons
Source File: model_checks.py
View license
@register(Tags.models)
def check_all_models(app_configs=None, **kwargs):
    errors = []
    for model in apps.get_models():
        if app_configs is None or model._meta.app_config in app_configs:
            if not inspect.ismethod(model.check):
                errors.append(
                    Error(
                        "The '%s.check()' class method is "
                        "currently overridden by %r." % (
                            model.__name__, model.check),
                        hint=None,
                        obj=model,
                        id='models.E020'
                    )
                )
            else:
                errors.extend(model.check(**kwargs))
    return errors

Example 12

Project: PyClassLessons
Source File: files.py
View license
    def _check_unique(self):
        if self._unique_set_explicitly:
            return [
                checks.Error(
                    "'unique' is not a valid argument for a %s." % self.__class__.__name__,
                    hint=None,
                    obj=self,
                    id='fields.E200',
                )
            ]
        else:
            return []

Example 13

Project: PyClassLessons
Source File: files.py
View license
    def _check_primary_key(self):
        if self._primary_key_set_explicitly:
            return [
                checks.Error(
                    "'primary_key' is not a valid argument for a %s." % self.__class__.__name__,
                    hint=None,
                    obj=self,
                    id='fields.E201',
                )
            ]
        else:
            return []

Example 14

Project: PyClassLessons
Source File: files.py
View license
    def _check_image_library_installed(self):
        try:
            from django.utils.image import Image  # NOQA
        except ImproperlyConfigured:
            return [
                checks.Error(
                    'Cannot use ImageField because Pillow is not installed.',
                    hint=('Get Pillow at https://pypi.python.org/pypi/Pillow '
                          'or run command "pip install pillow".'),
                    obj=self,
                    id='fields.E210',
                )
            ]
        else:
            return []

Example 15

View license
def _check_db_index(self):
    # Taken from django.db.models.fields.__init__
    if self.db_index not in (None, True, False):
        return [
            checks.Error(
                "'db_index' must be None, True or False.",
                hint=None,
                obj=self,
                id='fields.E006',
            )
        ]
    else:
        return []

Example 16

Project: Django--an-app-at-a-time
Source File: checks.py
View license
    def _check_exclude(self, cls, model):
        """ Check that exclude is a sequence without duplicates. """

        if cls.exclude is None:  # default value is None
            return []
        elif not isinstance(cls.exclude, (list, tuple)):
            return must_be('a list or tuple', option='exclude', obj=cls, id='admin.E014')
        elif len(cls.exclude) > len(set(cls.exclude)):
            return [
                checks.Error(
                    "The value of 'exclude' contains duplicate field(s).",
                    hint=None,
                    obj=cls,
                    id='admin.E015',
                )
            ]
        else:
            return []

Example 17

Project: Django--an-app-at-a-time
Source File: checks.py
View license
    def _check_radio_fields_value(self, cls, model, val, label):
        """ Check type of a value of `radio_fields` dictionary. """

        from django.contrib.admin.options import HORIZONTAL, VERTICAL

        if val not in (HORIZONTAL, VERTICAL):
            return [
                checks.Error(
                    "The value of '%s' must be either admin.HORIZONTAL or admin.VERTICAL." % label,
                    hint=None,
                    obj=cls,
                    id='admin.E024',
                )
            ]
        else:
            return []

Example 18

Project: Django--an-app-at-a-time
Source File: checks.py
View license
    def _check_view_on_site_url(self, cls, model):
        if hasattr(cls, 'view_on_site'):
            if not callable(cls.view_on_site) and not isinstance(cls.view_on_site, bool):
                return [
                    checks.Error(
                        "The value of 'view_on_site' must be a callable or a boolean value.",
                        hint=None,
                        obj=cls,
                        id='admin.E025',
                    )
                ]
            else:
                return []
        else:
            return []

Example 19

Project: Django--an-app-at-a-time
Source File: checks.py
View license
    def _check_list_display_links_item(self, cls, model, field_name, label):
        if field_name not in cls.list_display:
            return [
                checks.Error(
                    "The value of '%s' refers to '%s', which is not defined in 'list_display'." % (
                        label, field_name
                    ),
                    hint=None,
                    obj=cls,
                    id='admin.E111',
                )
            ]
        else:
            return []

Example 20

Project: Django--an-app-at-a-time
Source File: checks.py
View license
    def _check_relation(self, cls, parent_model):
        try:
            _get_foreign_key(parent_model, cls.model, fk_name=cls.fk_name)
        except ValueError as e:
            return [checks.Error(e.args[0], hint=None, obj=cls, id='admin.E202')]
        else:
            return []

Example 21

Project: Django--an-app-at-a-time
Source File: checks.py
View license
def must_be(type, option, obj, id):
    return [
        checks.Error(
            "The value of '%s' must be %s." % (option, type),
            hint=None,
            obj=obj,
            id=id,
        ),
    ]

Example 22

Project: Django--an-app-at-a-time
Source File: checks.py
View license
def must_inherit_from(parent, option, obj, id):
    return [
        checks.Error(
            "The value of '%s' must inherit from '%s'." % (option, parent),
            hint=None,
            obj=obj,
            id=id,
        ),
    ]

Example 23

Project: Django--an-app-at-a-time
Source File: checks.py
View license
def refer_to_missing_field(field, option, model, obj, id):
    return [
        checks.Error(
            "The value of '%s' refers to '%s', which is not an attribute of '%s.%s'." % (
                option, field, model._meta.app_label, model._meta.object_name
            ),
            hint=None,
            obj=obj,
            id=id,
        ),
    ]

Example 24

Project: Django--an-app-at-a-time
Source File: fields.py
View license
    def _check_field_name(self):
        if self.name.endswith("_"):
            return [
                checks.Error(
                    'Field names must not end with an underscore.',
                    hint=None,
                    obj=self,
                    id='fields.E001',
                )
            ]
        else:
            return []

Example 25

Project: Django--an-app-at-a-time
Source File: fields.py
View license
    def _check_object_id_field(self):
        try:
            self.model._meta.get_field(self.fk_field)
        except FieldDoesNotExist:
            return [
                checks.Error(
                    "The GenericForeignKey object ID references the non-existent field '%s'." % self.fk_field,
                    hint=None,
                    obj=self,
                    id='contenttypes.E001',
                )
            ]
        else:
            return []

Example 26

View license
@register(Tags.models)
def check_all_models(app_configs=None, **kwargs):
    errors = []
    for model in apps.get_models():
        if app_configs is None or model._meta.app_config in app_configs:
            if not inspect.ismethod(model.check):
                errors.append(
                    Error(
                        "The '%s.check()' class method is "
                        "currently overridden by %r." % (
                            model.__name__, model.check),
                        hint=None,
                        obj=model,
                        id='models.E020'
                    )
                )
            else:
                errors.extend(model.check(**kwargs))
    return errors

Example 27

View license
    def check_field(self, field, **kwargs):
        class ErrorList(list):
            """A dummy list class that emulates API used by the older
            validate_field() method. When validate_field() is fully
            deprecated, this dummy can be removed too.
            """
            def add(self, opts, error_message):
                self.append(checks.Error(error_message, hint=None, obj=field))

        errors = ErrorList()
        # Some tests create fields in isolation -- the fields are not attached
        # to any model, so they have no `model` attribute.
        opts = field.model._meta if hasattr(field, 'model') else None
        self.validate_field(errors, field, opts)
        return list(errors)

Example 28

Project: Django--an-app-at-a-time
Source File: files.py
View license
    def _check_unique(self):
        if self._unique_set_explicitly:
            return [
                checks.Error(
                    "'unique' is not a valid argument for a %s." % self.__class__.__name__,
                    hint=None,
                    obj=self,
                    id='fields.E200',
                )
            ]
        else:
            return []

Example 29

Project: Django--an-app-at-a-time
Source File: files.py
View license
    def _check_primary_key(self):
        if self._primary_key_set_explicitly:
            return [
                checks.Error(
                    "'primary_key' is not a valid argument for a %s." % self.__class__.__name__,
                    hint=None,
                    obj=self,
                    id='fields.E201',
                )
            ]
        else:
            return []

Example 30

Project: Django--an-app-at-a-time
Source File: files.py
View license
    def _check_image_library_installed(self):
        try:
            from PIL import Image  # NOQA
        except ImportError:
            return [
                checks.Error(
                    'Cannot use ImageField because Pillow is not installed.',
                    hint=('Get Pillow at https://pypi.python.org/pypi/Pillow '
                          'or run command "pip install Pillow".'),
                    obj=self,
                    id='fields.E210',
                )
            ]
        else:
            return []

Example 31

Project: coursys
Source File: checks.py
View license
def _add_error(errors, msg, ident):
    errors.append(
        Error(
            msg,
            hint=None,
            obj=BitField,
            id='coredata.E%03i' % (ident)
        )
    )

Example 32

Project: amy
Source File: checks.py
View license
@register(Tags.templates)
def check_pydata_installed_before_workshops(**kwargs):
    errors = []
    if settings.INSTALLED_APPS.index('pydata') > \
       settings.INSTALLED_APPS.index('workshops'):
        errors.append(
            Error(
                '`pydata` installed after `workshops` app.',
                hint=('Add `pydata` to INSTALLED_APPS before the `workshops` app.'),
                id='amy.E003',
            ),
        )
    return errors

Example 33

Project: amy
Source File: checks.py
View license
@register(Tags.security)
def check_pydata_username_password_in_settings(**kwargs):
    errors = []
    if not hasattr(settings, 'PYDATA_USERNAME_SECRET'):
        errors.append(
            Error('`PYDATA_USERNAME_SECRET` is missing in settings.py'),
        )
    if not hasattr(settings, 'PYDATA_PASSWORD_SECRET'):
        errors.append(
            Error('`PYDATA_PASSWORD_SECRET` is missing in settings.py'),
        )
    return errors

Example 34

Project: wagtail
Source File: checks.py
View license
@register()
def base_form_class_check(app_configs, **kwargs):
    from wagtail.wagtailadmin.forms import WagtailAdminPageForm
    from wagtail.wagtailcore.models import get_page_models

    errors = []

    for cls in get_page_models():
        if not issubclass(cls.base_form_class, WagtailAdminPageForm):
            errors.append(Error(
                "{}.base_form_class does not extend WagtailAdminPageForm".format(
                    cls.__name__),
                hint="Ensure that {}.{} extends WagtailAdminPageForm".format(
                    cls.base_form_class.__module__,
                    cls.base_form_class.__name__),
                obj=cls,
                id='wagtailadmin.E001'))

    return errors

Example 35

Project: wagtail
Source File: checks.py
View license
@register()
def get_form_class_check(app_configs, **kwargs):
    from wagtail.wagtailadmin.forms import WagtailAdminPageForm
    from wagtail.wagtailcore.models import get_page_models

    errors = []

    for cls in get_page_models():
        edit_handler = cls.get_edit_handler()
        if not issubclass(edit_handler.get_form_class(cls), WagtailAdminPageForm):
            errors.append(Error(
                "{cls}.get_edit_handler().get_form_class({cls}) does not extend WagtailAdminPageForm".format(
                    cls=cls.__name__),
                hint="Ensure that the EditHandler for {cls} creates a subclass of WagtailAdminPageForm".format(
                    cls=cls.__name__),
                obj=cls,
                id='wagtailadmin.E002'))

    return errors

Example 36

Project: django-cachalot
Source File: apps.py
View license
@register(Tags.compatibility)
def check_compatibility(app_configs, **kwargs):
    cache_alias = cachalot_settings.CACHALOT_CACHE
    caches = {cache_alias: settings.CACHES[cache_alias]}

    errors = []
    for setting, k, valid_values in (
            (settings.DATABASES, 'ENGINE', VALID_DATABASE_ENGINES),
            (caches, 'BACKEND', VALID_CACHE_BACKENDS)):
        for config in setting.values():
            value = config[k]
            if value not in valid_values:
                errors.append(
                    Error(
                        '`%s` is not compatible with django-cachalot.' % value,
                        id='cachalot.E001',
                    )
                )
    return errors

Example 37

Project: django-mysql
Source File: dynamic.py
View license
    def _check_mariadb_dyncol(self):
        errors = []
        if mariadb_dyncol is None:
            errors.append(
                checks.Error(
                    "'mariadb_dyncol' is required to use DynamicField",
                    hint="Install the 'mariadb_dyncol' library from 'pip'",
                    obj=self,
                    id='django_mysql.E012'
                )
            )
        return errors

Example 38

Project: django-mysql
Source File: json.py
View license
    def _check_default(self):
        errors = []
        if isinstance(self.default, (list, dict)):
            errors.append(
                checks.Error(
                    'Do not use mutable defaults for JSONField',
                    hint=collapse_spaces('''
                        Mutable defaults get shared between all instances of
                        the field, which probably isn't what you want. You
                        should replace your default with a callable, e.g.
                        replace default={{}} with default=dict.

                        The default you passed was '{}'.
                    '''.format(self.default)),
                    obj=self,
                    id='django_mysql.E017',
                )
            )
        return errors

Example 39

Project: django-mysql
Source File: sizes.py
View license
    def check(self, **kwargs):
        errors = super(SizedBinaryField, self).check(**kwargs)
        if self.size_class not in (1, 2, 3, 4):
            errors.append(
                checks.Error(
                    'size_class must be 1, 2, 3, or 4',
                    hint=None,
                    obj=self,
                    id='django_mysql.E007'
                )
            )
        return errors

Example 40

Project: django-mysql
Source File: sizes.py
View license
    def check(self, **kwargs):
        errors = super(SizedTextField, self).check(**kwargs)
        if self.size_class not in (1, 2, 3, 4):
            errors.append(
                checks.Error(
                    'size_class must be 1, 2, 3, or 4',
                    hint=None,
                    obj=self,
                    id='django_mysql.E008'
                )
            )
        return errors

Example 41

Project: cgstudiomap
Source File: checks.py
View license
    def _check_exclude(self, obj):
        """ Check that exclude is a sequence without duplicates. """

        if obj.exclude is None:  # default value is None
            return []
        elif not isinstance(obj.exclude, (list, tuple)):
            return must_be('a list or tuple', option='exclude', obj=obj, id='admin.E014')
        elif len(obj.exclude) > len(set(obj.exclude)):
            return [
                checks.Error(
                    "The value of 'exclude' contains duplicate field(s).",
                    hint=None,
                    obj=obj.__class__,
                    id='admin.E015',
                )
            ]
        else:
            return []

Example 42

Project: cgstudiomap
Source File: checks.py
View license
    def _check_radio_fields_value(self, obj, val, label):
        """ Check type of a value of `radio_fields` dictionary. """

        from django.contrib.admin.options import HORIZONTAL, VERTICAL

        if val not in (HORIZONTAL, VERTICAL):
            return [
                checks.Error(
                    "The value of '%s' must be either admin.HORIZONTAL or admin.VERTICAL." % label,
                    hint=None,
                    obj=obj.__class__,
                    id='admin.E024',
                )
            ]
        else:
            return []

Example 43

Project: cgstudiomap
Source File: checks.py
View license
    def _check_view_on_site_url(self, obj):
        if hasattr(obj, 'view_on_site'):
            if not callable(obj.view_on_site) and not isinstance(obj.view_on_site, bool):
                return [
                    checks.Error(
                        "The value of 'view_on_site' must be a callable or a boolean value.",
                        hint=None,
                        obj=obj.__class__,
                        id='admin.E025',
                    )
                ]
            else:
                return []
        else:
            return []

Example 44

Project: cgstudiomap
Source File: checks.py
View license
    def _check_list_display_links_item(self, obj, field_name, label):
        if field_name not in obj.list_display:
            return [
                checks.Error(
                    "The value of '%s' refers to '%s', which is not defined in 'list_display'." % (
                        label, field_name
                    ),
                    hint=None,
                    obj=obj.__class__,
                    id='admin.E111',
                )
            ]
        else:
            return []

Example 45

Project: cgstudiomap
Source File: checks.py
View license
    def _check_relation(self, obj, parent_model):
        try:
            _get_foreign_key(parent_model, obj.model, fk_name=obj.fk_name)
        except ValueError as e:
            return [checks.Error(e.args[0], hint=None, obj=obj.__class__, id='admin.E202')]
        else:
            return []

Example 46

Project: cgstudiomap
Source File: checks.py
View license
def must_be(type, option, obj, id):
    return [
        checks.Error(
            "The value of '%s' must be %s." % (option, type),
            hint=None,
            obj=obj.__class__,
            id=id,
        ),
    ]

Example 47

Project: cgstudiomap
Source File: checks.py
View license
def must_inherit_from(parent, option, obj, id):
    return [
        checks.Error(
            "The value of '%s' must inherit from '%s'." % (option, parent),
            hint=None,
            obj=obj.__class__,
            id=id,
        ),
    ]

Example 48

Project: cgstudiomap
Source File: checks.py
View license
def refer_to_missing_field(field, option, model, obj, id):
    return [
        checks.Error(
            "The value of '%s' refers to '%s', which is not an attribute of '%s.%s'." % (
                option, field, model._meta.app_label, model._meta.object_name
            ),
            hint=None,
            obj=obj.__class__,
            id=id,
        ),
    ]

Example 49

Project: cgstudiomap
Source File: fields.py
View license
    def _check_field_name(self):
        if self.name.endswith("_"):
            return [
                checks.Error(
                    'Field names must not end with an underscore.',
                    hint=None,
                    obj=self,
                    id='fields.E001',
                )
            ]
        else:
            return []

Example 50

Project: cgstudiomap
Source File: fields.py
View license
    def _check_object_id_field(self):
        try:
            self.model._meta.get_field(self.fk_field)
        except FieldDoesNotExist:
            return [
                checks.Error(
                    "The GenericForeignKey object ID references the non-existent field '%s'." % self.fk_field,
                    hint=None,
                    obj=self,
                    id='contenttypes.E001',
                )
            ]
        else:
            return []