Skip to content

Commit

Permalink
Merge pull request #4875 from open-formulieren/chore/migrate-deprecat…
Browse files Browse the repository at this point in the history
…ed-summary-tag

Migrate deprecated summary tag
  • Loading branch information
sergei-maertens authored Dec 9, 2024
2 parents 53d6f8d + b92a1dd commit dd38e63
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 15 deletions.
5 changes: 3 additions & 2 deletions docs/manual/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down
52 changes: 52 additions & 0 deletions src/openforms/config/migrations/0068_update_summary_tags.py
Original file line number Diff line number Diff line change
@@ -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),
]
49 changes: 49 additions & 0 deletions src/openforms/config/tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
42 changes: 42 additions & 0 deletions src/openforms/emails/migrations/0003_update_summary_tags.py
Original file line number Diff line number Diff line change
@@ -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),
]
5 changes: 1 addition & 4 deletions src/openforms/emails/templatetags/form_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions src/openforms/emails/tests/test_confirmation_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_nested_components(self):
"email": "[email protected]",
},
)
email = ConfirmationEmailTemplate(content="{% summary %}")
email = ConfirmationEmailTemplate(content="{% confirmation_summary %}")
context = get_confirmation_email_context_data(submission)
rendered_content = render_email_template(email.content, context)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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("<ul><li>Value 1</li><li>Value 2</li></ul>", rendered_content)

Expand Down Expand Up @@ -472,7 +472,7 @@ def test_summary_heading_behaviour(self):
ConfirmationEmailTemplateFactory.create(
form=submission.form,
subject="Subject",
content="{% summary %}{% appointment_information %}",
content="{% confirmation_summary %}{% appointment_information %}",
)
template = get_confirmation_email_templates(submission)[1]
context = get_confirmation_email_context_data(submission)
Expand All @@ -498,7 +498,7 @@ def test_summary_heading_behaviour(self):
ConfirmationEmailTemplateFactory.create(
form=submission.form,
subject="Subject",
content="{% summary %}{% appointment_information %}",
content="{% confirmation_summary %}{% appointment_information %}",
)
template = get_confirmation_email_templates(submission)[1]
context = get_confirmation_email_context_data(submission)
Expand Down Expand Up @@ -640,7 +640,7 @@ class ConfirmationEmailRenderingIntegrationTest(HTMLAssertMixin, TestCase):
<p>Kijk voor meer informatie op <a href="http://gemeente.nl">de homepage</a></p>
{% summary %}
{% confirmation_summary %}
{% appointment_information %}
Expand Down
81 changes: 81 additions & 0 deletions src/openforms/emails/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -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"
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 %}
""",
)

Expand Down

0 comments on commit dd38e63

Please sign in to comment.