from django.conf import settings
from django.forms.renderers import DjangoTemplates
from django.utils.functional import cached_property


class OcyanFormRenderer(DjangoTemplates):
    """
    This class is needed because django uses a different template renderer for
    form widgets than for regular templates. We do need the loaders we have
    set for the regular templates to be the same for form templates.
    Otherwise we can not oveeride form widgets in app extensions.
    """

    @cached_property
    def engine(self):
        engine = super().engine  # pylint: disable=E1101

        TEMPLATES = settings.TEMPLATES[0]

        return self.backend(
            {
                "APP_DIRS": False,
                "DIRS": engine.dirs,
                "NAME": "djangoforms",
                "OPTIONS": {"loaders": TEMPLATES["OPTIONS"].get("loaders")},
            }
        )
