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"))

[((10, 1, 10, 18), 'django.core.checks.register', 'checks.register', ({}, {}), '()', False, 'from django.core import checks\n'), ((14, 18, 14, 31), 'django.template.engines.all', 'engines.all', ({}, {}), '()', False, 'from django.template import engines\n'), ((52, 28, 52, 46), 'django.utils.translation.gettext_lazy', '_', ({(52, 30, 52, 45): '"""user messages"""'}, {}), "('user messages')", True, 'from django.utils.translation import gettext_lazy as _\n'), ((39, 12, 43, 13), 'django.core.checks.Error', 'checks.Error', (), '', False, 'from django.core import checks\n'), ((28, 16, 33, 17), 'django.core.checks.Error', 'checks.Error', (), '', False, 'from django.core import checks\n')]