diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/settings/base.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/settings/base.py index dac901d7..e573b7b8 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/settings/base.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/config/settings/base.py @@ -211,7 +211,7 @@ # https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND "BACKEND": "django.template.backends.django.DjangoTemplates", # https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs - "DIRS": [APP_DIR / "templates"], + "DIRS": [APP_DIR / "dtl_templates"], "OPTIONS": { # https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders # https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/home/admin.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/home/admin.py new file mode 100644 index 00000000..fd839506 --- /dev/null +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/home/admin.py @@ -0,0 +1,44 @@ +from allauth.account.models import EmailAddress +from django.contrib.auth.admin import admin +from django.contrib.auth.models import Group +from django.contrib.sites.models import Site +from robots.admin import RuleAdmin +from robots.models import Rule, Url + +from {{cookiecutter.repo_name}}.util.admin import force_django_template + +admin.site.unregister(Rule) +admin.site.unregister(Url) +admin.site.unregister(Site) +admin.site.unregister(Group) +admin.site.unregister(EmailAddress) + + +@admin.register(Rule) +@force_django_template +class CustomRuleAdmin(RuleAdmin): + pass + + +@admin.register(Url) +@force_django_template +class UrlAdmin(admin.ModelAdmin): + pass + + +@admin.register(Site) +@force_django_template +class SiteAdmin(admin.ModelAdmin): + pass + + +@admin.register(Group) +@force_django_template +class GroupAdmin(admin.ModelAdmin): + pass + + +@admin.register(EmailAddress) +@force_django_template +class EmailAdmin(admin.ModelAdmin): + pass diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/util/admin.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/util/admin.py index 1970afb6..8a3ca071 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/util/admin.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/util/admin.py @@ -1,5 +1,69 @@ -# from django.contrib import admin # noqa +from django.contrib.admin.decorators import display +from django.contrib.admin.options import helpers +from django.contrib.auth.admin import admin +from django.forms.renderers import DjangoTemplates +from django.template.base import mark_safe -# from .models import TestFileModel # noqa -# admin.site.register(TestFileModel) # noqa +def force_django_template(AdminClass: type[admin.ModelAdmin]): + """ Replaces your admin class with one which forces the use of the DjangoTemplates renderer + Noted exceptions: + - sub-widgets rendered by the RelatedFieldWidgetWrapper + Usage: + @admin.register(MyModel) + @force_django_template + class MyModelAdmin(admin.ModelAdmin): + ... + """ + action_form_class = None + if ActionFormClass := getattr(AdminClass, "action_form", None): + + class ForcedDjangoActionForm(ActionFormClass): + default_renderer = DjangoTemplates() + + action_form_class = ForcedDjangoActionForm + + def get_form(self, request, obj=None, change=False, **kwargs): + form = AdminClass.get_form(self, request, obj=obj, change=change, **kwargs) + form.default_renderer = DjangoTemplates() + return form + + def changelist_view(self, request, extra_context=None): + template_response = AdminClass.changelist_view( + self, request, extra_context=extra_context + ) + template_response.using = "django" + return template_response + + def get_changelist_form(self, request, **kwargs): + Form = AdminClass.get_changelist_form(self, request, **kwargs) + Form.renderer = DjangoTemplates() + return Form + + def get_changelist_formset(self, request, **kwargs): + FormSet = AdminClass.get_changelist_formset(self, request, **kwargs) + FormSet.renderer = DjangoTemplates() + return FormSet + + @display(description=mark_safe('')) + def action_checkbox(self, obj): + """ + A list_display column containing a checkbox widget. + """ + del self + return helpers.checkbox.render( + helpers.ACTION_CHECKBOX_NAME, str(obj.pk), renderer=DjangoTemplates() + ) + + return type( + f"{AdminClass.__name__}_forceddjangotemplates", + (AdminClass,), + { + "get_form": get_form, + "action_form": action_form_class, + "changelist_view": changelist_view, + "get_changelist_form": get_changelist_form, + "get_changelist_formset": get_changelist_formset, + "action_checkbox": action_checkbox, + }, + )