-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4875 from open-formulieren/chore/migrate-deprecat…
…ed-summary-tag Migrate deprecated summary tag
- Loading branch information
Showing
9 changed files
with
237 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/openforms/config/migrations/0068_update_summary_tags.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/openforms/emails/migrations/0003_update_summary_tags.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
|
@@ -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("<ul><li>Value 1</li><li>Value 2</li></ul>", rendered_content) | ||
|
||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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 %} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters