Skip to content

Commit

Permalink
🔨 [#1451] -- Add management command to check for template tag usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed May 9, 2022
1 parent 62121f7 commit ffbbe30
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ omit =
src/openforms/plugins/management/commands/disable_demo_plugins.py
src/openforms/payments/management/commands/checkpaymentemaildupes.py
src/openforms/registrations/contrib/email/views.py
src/openforms/utils/management/commands/detect_deprecated_tags_usage.py

[coverage:report]
omit =
Expand All @@ -64,3 +65,4 @@ omit =
src/openforms/plugins/management/commands/disable_demo_plugins.py
src/openforms/payments/management/commands/checkpaymentemaildupes.py
src/openforms/registrations/contrib/email/views.py
src/openforms/utils/management/commands/detect_deprecated_tags_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Tuple

from django.apps import apps
from django.core.management import BaseCommand

DEPRECATED_TAGS = ("{% display_value",)


MODELS_AND_FIELDS = (
(
"config.GlobalConfiguration",
(
"submission_confirmation_template",
"confirmation_email_subject",
"confirmation_email_content",
),
),
(
"forms.Form",
(
"submission_confirmation_template",
"explanation_template",
),
),
)


class Command(BaseCommand):
help = "Detect deprecated template tag usage in user-templates"

def handle(self, **options):
for model, fields in MODELS_AND_FIELDS:
self.check_model(model, fields)

self.stdout.write("Done checking.")

def check_model(self, model: str, fields: Tuple[str]):
model_cls = apps.get_model(model)

for tag in DEPRECATED_TAGS:
filters = {f"{field}__contains": tag for field in fields}
queryset = model_cls.objects.filter(**filters).values_list("id", flat=True)
if not queryset:
continue
self.stdout.write(
f"Model {model} instances containing deprecated tag '{tag}': {', '.join(queryset)}"
)

0 comments on commit ffbbe30

Please sign in to comment.