from django.apps import AppConfig
from django.conf import settings
from django.core import checks
from django.template import engines
from django.template.backends.django import DjangoTemplates
from django.utils.text import capfirst
from django.utils.translation import gettext_lazy as _


@checks.register()
def check_context_processors(app_configs, **kwargs):
    errors = []

    for engine in engines.all():
        if isinstance(engine, DjangoTemplates):
            django_templates_instance = engine.engine
            break
    else:
        django_templates_instance = None

    if django_templates_instance:
        if (
            "django.contrib.messages.context_processors.messages"
            not in django_templates_instance.context_processors
            and "admin.E404" not in settings.SILENCED_SYSTEM_CHECKS
        ):
            errors.append(
                checks.Error(
                    "If using 'user_messages.context_processors.messages'"
                    " instead of the official messages context processor"
                    " you have to add 'admin.E404' to SILENCED_SYSTEM_CHECKS.",
                    id="user_messages.E001",
                )
            )

    if ("admin.E406" not in settings.SILENCED_SYSTEM_CHECKS and
    "django.contrib.messages" not in settings.INSTALLED_APPS):
        errors.append(
            checks.Error(
                "If using 'user_messages' instead of django.contrib.messages"
                " you have to add 'admin.E406' to SILENCED_SYSTEM_CHECKS.",
                id="user_messages.E002",
            )
        )

    return errors


class UserMessagesConfig(AppConfig):
    default_auto_field = "django.db.models.AutoField"
    name = "user_messages"
    verbose_name = capfirst(_("user messages"))

[((291, 308), 'django.core.checks.register', 'checks.register', ([], {}), '()\n', (306, 308), False, 'from django.core import checks\n'), ((397, 410), 'django.template.engines.all', 'engines.all', ([], {}), '()\n', (408, 410), False, 'from django.template import engines\n'), ((1766, 1784), 'django.utils.translation.gettext_lazy', '_', (['"""user messages"""'], {}), "('user messages')\n", (1767, 1784), True, 'from django.utils.translation import gettext_lazy as _\n'), ((1366, 1532), 'django.core.checks.Error', 'checks.Error', (['"""If using \'user_messages\' instead of django.contrib.messages you have to add \'admin.E406\' to SILENCED_SYSTEM_CHECKS."""'], {'id': '"""user_messages.E002"""'}), '(\n    "If using \'user_messages\' instead of django.contrib.messages you have to add \'admin.E406\' to SILENCED_SYSTEM_CHECKS."\n    , id=\'user_messages.E002\')\n', (1378, 1532), False, 'from django.core import checks\n'), ((883, 1093), 'django.core.checks.Error', 'checks.Error', (['"""If using \'user_messages.context_processors.messages\' instead of the official messages context processor you have to add \'admin.E404\' to SILENCED_SYSTEM_CHECKS."""'], {'id': '"""user_messages.E001"""'}), '(\n    "If using \'user_messages.context_processors.messages\' instead of the official messages context processor you have to add \'admin.E404\' to SILENCED_SYSTEM_CHECKS."\n    , id=\'user_messages.E001\')\n', (895, 1093), False, 'from django.core import checks\n')]