diff --git a/docs/manual/templates.rst b/docs/manual/templates.rst index 0ac9f4f5e0..38dd952022 100644 --- a/docs/manual/templates.rst +++ b/docs/manual/templates.rst @@ -265,9 +265,10 @@ Variabele Beschrijving van medeondertekenen. ================================== =========================================================================== -.. note:: +.. versionremoved:: 3.0.0 - De speciale instructie ``{% summary %}`` is verouderd en zal vanaf versie 3.0.0 niet meer beschikbaar zijn. + De speciale instructie ``{% summary %}`` is vervangen door + ``{% confirmation_summary %}``. Voorbeeld --------- diff --git a/src/openforms/appointments/tests/test_confirmation_emails.py b/src/openforms/appointments/tests/test_confirmation_emails.py index 260fb818c8..bba646d163 100644 --- a/src/openforms/appointments/tests/test_confirmation_emails.py +++ b/src/openforms/appointments/tests/test_confirmation_emails.py @@ -102,7 +102,7 @@ def setUpTestData(cls): super().setUpTestData() cls.config = GlobalConfiguration( - confirmation_email_content="{% summary %}\n{% appointment_information %}", + confirmation_email_content="{% confirmation_summary %}\n{% appointment_information %}", confirmation_email_subject="CONFIRMATION", ) diff --git a/src/openforms/config/migrations/0068_update_summary_tags.py b/src/openforms/config/migrations/0068_update_summary_tags.py new file mode 100644 index 0000000000..c3ddc00912 --- /dev/null +++ b/src/openforms/config/migrations/0068_update_summary_tags.py @@ -0,0 +1,52 @@ +# Generated by Django 4.2.16 on 2024-11-29 18:47 + +import re + +from django.db import migrations +from django.db.migrations.state import StateApps + + +def replace_tag(tpl: str) -> str: + return re.sub( + r"{%\s*summary\s*%}", + "{% confirmation_summary %}", + tpl, + ) + + +def update_summary_tags(apps: StateApps, _): + GlobalConfiguration = apps.get_model("config", "GlobalConfiguration") + config = GlobalConfiguration.objects.first() + if config is None: + return + + # the cosign fields are new in 3.0.0 so they're not expected to hold the legacy + # tag. + config.submission_confirmation_template_en = replace_tag( + config.submission_confirmation_template_en + ) + config.submission_confirmation_template_nl = replace_tag( + config.submission_confirmation_template_nl + ) + config.confirmation_email_content_nl = replace_tag( + config.confirmation_email_content_nl + ) + config.confirmation_email_content_en = replace_tag( + config.confirmation_email_content_en + ) + config.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ( + "config", + "0068_alter_globalconfiguration_cosign_request_template_and_more", + ), + ] + + operations = [ + # reverse not needed, since the new format worked on old code too + migrations.RunPython(update_summary_tags, migrations.RunPython.noop), + ] diff --git a/src/openforms/config/tests/test_migrations.py b/src/openforms/config/tests/test_migrations.py index d07e73f1d0..9f66ffc799 100644 --- a/src/openforms/config/tests/test_migrations.py +++ b/src/openforms/config/tests/test_migrations.py @@ -60,3 +60,52 @@ def test_feature_flags_created(self): self.assertTrue(config.enable_demo_plugins) self.assertFalse(config.display_sdk_information) + + +class MigrateSummaryTag(TestMigrations): + app = "config" + migrate_from = "0068_alter_globalconfiguration_cosign_request_template_and_more" + migrate_to = "0068_update_summary_tags" + + def setUpBeforeMigration(self, apps: StateApps): + GlobalConfiguration = apps.get_model("config", "GlobalConfiguration") + test_template = r""" + Some prefix + {% summary %} + {%summary%} + {% summary%} + {% summary %} + {% other_tag %} + """ + GlobalConfiguration.objects.update_or_create( + pk=1, + defaults={ + "submission_confirmation_template_nl": test_template, + "submission_confirmation_template_en": test_template, + "confirmation_email_content_nl": test_template, + "confirmation_email_content_en": test_template, + }, + ) + + def test_content_migrated(self): + GlobalConfiguration = self.apps.get_model("config", "GlobalConfiguration") + config = GlobalConfiguration.objects.get() + + expected = r""" + Some prefix + {% confirmation_summary %} + {% confirmation_summary %} + {% confirmation_summary %} + {% confirmation_summary %} + {% other_tag %} + """ + for field in ( + "submission_confirmation_template_nl", + "submission_confirmation_template_en", + "confirmation_email_content_nl", + "confirmation_email_content_en", + ): + with self.subTest(field=field): + result = getattr(config, field) + + self.assertEqual(result, expected) diff --git a/src/openforms/emails/migrations/0003_update_summary_tags.py b/src/openforms/emails/migrations/0003_update_summary_tags.py new file mode 100644 index 0000000000..7ce56e73a7 --- /dev/null +++ b/src/openforms/emails/migrations/0003_update_summary_tags.py @@ -0,0 +1,42 @@ +# Generated by Django 4.2.16 on 2024-11-29 18:33 + +import re + +from django.db import migrations +from django.db.migrations.state import StateApps +from django.db.models import Q + +SUMMARY_REGEX = r"{%\s*summary\s*%}" + + +def replace_tag(tpl: str) -> str: + return re.sub( + SUMMARY_REGEX, + "{% confirmation_summary %}", + tpl, + ) + + +def update_summary_tags(apps: StateApps, _): + ConfirmationEmailTemplate = apps.get_model("emails", "ConfirmationEmailTemplate") + # * The field cosign_content is new in 3.0.0 so it's not expected to hold the legacy + # tag. + # * At this point in time, only nl/en are supported. + q = Q(content_nl__regex=SUMMARY_REGEX) | Q(content_en__regex=SUMMARY_REGEX) + qs = ConfirmationEmailTemplate.objects.filter(q) + for obj in qs: + obj.content_en = replace_tag(obj.content_en) + obj.content_nl = replace_tag(obj.content_nl) + obj.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("emails", "0002_confirmationemailtemplate_cosign_content_and_more"), + ] + + operations = [ + # reverse not needed, since the new format worked on old code too + migrations.RunPython(update_summary_tags, migrations.RunPython.noop), + ] diff --git a/src/openforms/emails/templatetags/form_summary.py b/src/openforms/emails/templatetags/form_summary.py index 29cb3d46a7..0ace645333 100644 --- a/src/openforms/emails/templatetags/form_summary.py +++ b/src/openforms/emails/templatetags/form_summary.py @@ -14,7 +14,7 @@ @register.simple_tag(takes_context=True) -def summary(context): +def confirmation_summary(context): submission = context["_submission"] # if it's a new-style appointment submission, there are no steps or summary to render if get_appointment(submission) is not None: @@ -43,9 +43,6 @@ def summary(context): return get_template(name).render(context.flatten()) -register.simple_tag(name="confirmation_summary", takes_context=True)(summary) - - @register.simple_tag() def whitespace(amount: int, base=" ") -> str: return base * amount diff --git a/src/openforms/emails/tests/test_confirmation_email.py b/src/openforms/emails/tests/test_confirmation_email.py index d252905456..2c58b64b84 100644 --- a/src/openforms/emails/tests/test_confirmation_email.py +++ b/src/openforms/emails/tests/test_confirmation_email.py @@ -182,7 +182,7 @@ def test_nested_components(self): "email": "test@example.com", }, ) - email = ConfirmationEmailTemplate(content="{% summary %}") + email = ConfirmationEmailTemplate(content="{% confirmation_summary %}") context = get_confirmation_email_context_data(submission) rendered_content = render_email_template(email.content, context) @@ -229,7 +229,7 @@ def test_attachment(self): }, ) context = get_confirmation_email_context_data(submission) - rendered_content = render_email_template("{% summary %}", context) + rendered_content = render_email_template("{% confirmation_summary %}", context) self.assertTagWithTextIn("td", "Name", rendered_content) self.assertTagWithTextIn("td", "Jane", rendered_content) @@ -321,7 +321,7 @@ def test_checkboxes_ordering(self): ) context = get_confirmation_email_context_data(submission) - rendered_content = render_email_template("{% summary %}", context) + rendered_content = render_email_template("{% confirmation_summary %}", context) self.assertInHTML("
Kijk voor meer informatie op de homepage
- {% summary %} + {% confirmation_summary %} {% appointment_information %} diff --git a/src/openforms/emails/tests/test_migrations.py b/src/openforms/emails/tests/test_migrations.py new file mode 100644 index 0000000000..b7c40691ba --- /dev/null +++ b/src/openforms/emails/tests/test_migrations.py @@ -0,0 +1,81 @@ +from textwrap import dedent + +from django.db.migrations.state import StateApps + +from openforms.utils.tests.test_migrations import TestMigrations + + +class MigrateSummaryTag(TestMigrations): + app = "emails" + migrate_from = "0002_confirmationemailtemplate_cosign_content_and_more" + migrate_to = "0003_update_summary_tags" + + def setUpBeforeMigration(self, apps: StateApps): + Form = apps.get_model("forms", "Form") + ConfirmationEmailTemplate = apps.get_model( + "emails", "ConfirmationEmailTemplate" + ) + + form1 = Form.objects.create(name="test 1") + form2 = Form.objects.create(name="test 2") + + test_template = dedent( + r""" + Some prefix + {% summary %} + {%summary%} + {% summary%} + {%summary %} + {% other_tag %} + """ + ) + + ConfirmationEmailTemplate.objects.create( + form=form1, + content_nl=test_template, + content_en=r"Leave {% summar %}{% y %}untouched", + ) + ConfirmationEmailTemplate.objects.create( + form=form2, + cosign_content_nl=test_template, + content_en=r"Leave {% confirmation_summary %} untouched", + ) + + def test_content_migrated(self): + ConfirmationEmailTemplate = self.apps.get_model( + "emails", "ConfirmationEmailTemplate" + ) + + with self.subTest("templates form 1"): + tpl1 = ConfirmationEmailTemplate.objects.get(form__name="test 1") + + expected = dedent( + r""" + Some prefix + {% confirmation_summary %} + {% confirmation_summary %} + {% confirmation_summary %} + {% confirmation_summary %} + {% other_tag %} + """ + ) + self.assertEqual(tpl1.content_nl, expected) + self.assertEqual(tpl1.content_en, r"Leave {% summar %}{% y %}untouched") + + with self.subTest("templates form 2"): + tpl2 = ConfirmationEmailTemplate.objects.get(form__name="test 2") + + expected = dedent( + r""" + Some prefix + {% summary %} + {%summary%} + {% summary%} + {%summary %} + {% other_tag %} + """ + ) + self.assertEqual(tpl2.cosign_content_nl, expected) + self.assertEqual( + tpl2.content_en, r"Leave {% confirmation_summary %} untouched" + ) diff --git a/src/openforms/submissions/tests/test_tasks_confirmation_emails.py b/src/openforms/submissions/tests/test_tasks_confirmation_emails.py index f386508816..a5b6363816 100644 --- a/src/openforms/submissions/tests/test_tasks_confirmation_emails.py +++ b/src/openforms/submissions/tests/test_tasks_confirmation_emails.py @@ -181,7 +181,7 @@ def test_complete_submission_send_confirmation_email_with_summary(self): ConfirmationEmailTemplateFactory.create( form=submission.form, subject="Confirmation mail", - content="Information filled in: {{ foo }} and {{ bar }}. Submission summary: {% summary %}", + content="Information filled in: {{ foo }} and {{ bar }}. Submission summary: {% confirmation_summary %}", ) # "execute" the celery task @@ -655,7 +655,7 @@ def test_template_is_rendered_in_submission_language(self): subject="Translated confirmation mail", content="""Translated content {{form_name}} - {% summary %} + {% confirmation_summary %} """, )