From 3af1dfaef40b4a3eff8e8f1271d030ff9b9c8c1f Mon Sep 17 00:00:00 2001 From: alligatortower Date: Mon, 10 Jul 2023 08:43:54 -0400 Subject: [PATCH 1/3] add decorator which forces django template admin --- .../{{cookiecutter.repo_name}}/home/admin.py | 44 ++++++++++++ .../{{cookiecutter.repo_name}}/util/admin.py | 70 ++++++++++++++++++- 2 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/home/admin.py 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..a113a1c5 --- /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 testhydra.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, + }, + ) From 4a24a9863db2497415569f4f02c6b3c788e9fb51 Mon Sep 17 00:00:00 2001 From: alligatortower Date: Mon, 10 Jul 2023 09:18:16 -0400 Subject: [PATCH 2/3] fix cookiecutter missed --- .../{{cookiecutter.repo_name}}/home/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/home/admin.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/home/admin.py index a113a1c5..fd839506 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/home/admin.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/home/admin.py @@ -5,7 +5,7 @@ from robots.admin import RuleAdmin from robots.models import Rule, Url -from testhydra.util.admin import force_django_template +from {{cookiecutter.repo_name}}.util.admin import force_django_template admin.site.unregister(Rule) admin.site.unregister(Url) From af073f3d3953712357f6f106fc3107ec48d14fdc Mon Sep 17 00:00:00 2001 From: alligatortower Date: Tue, 11 Jul 2023 08:59:49 -0400 Subject: [PATCH 3/3] point dtl away from our custom templates --- .../{{cookiecutter.repo_name}}/config/settings/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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