Skip to content

Commit b92c68e

Browse files
Add migration for updating email templates with bilingual content (#2628)
* Add migration for updating email templates with bilingual content * chore: Update migration files --------- Co-authored-by: Jumana B <[email protected]>
1 parent 369d4b2 commit b92c68e

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
"""
2+
3+
Revision ID: 0494_update_some_templates
4+
Revises: 0493_update_user_deact_tmpl
5+
Create Date: 2025-09-15 00:00:00
6+
7+
"""
8+
from datetime import datetime
9+
10+
from alembic import op
11+
from flask import current_app
12+
13+
revision = "0494_update_some_templates"
14+
down_revision = "0493_update_user_deact_tmpl"
15+
16+
CAT_AUTH_ID = "b6c42a7e-2a26-4a07-802b-123a5c3198a9"
17+
18+
templates = [
19+
{
20+
"id": current_app.config["FORCED_PASSWORD_RESET_TEMPLATE_ID"],
21+
"template_type": "email",
22+
"category_id": CAT_AUTH_ID,
23+
"subject": "Reset your password | Réinitialiser votre mot de passe",
24+
"content": """[[fr]]
25+
(la version française suit)
26+
[[/fr]]
27+
28+
[[en]]
29+
Hi ((user_name)),
30+
31+
To reset your password, use this link:
32+
33+
[Password reset](((url))?lang=en)
34+
35+
This is your unique link. Do not share this link with anyone.
36+
37+
If GC Notify did not prompt you to change your password [contact us](https://notification.canada.ca/contact?lang=en).
38+
[[/en]]
39+
40+
For details about the password change, read [incidents and service interruptions](https://notification.canada.ca/system-status#h-incidents-and-service-interruptions).
41+
42+
___
43+
44+
[[fr]]
45+
Bonjour ((user_name)),
46+
47+
Pour réinitialiser votre mot de passe, utilisez ce lien :
48+
49+
[Réinitialisation de votre mot de passe](((url))?lang=fr)
50+
51+
Ce lien est unique. Ne le transmettez à personne.
52+
53+
Si Notification GC ne vous a pas invité·e à changer votre mot de passe, [nous contacter](https://notification.canada.ca/contact?lang=fr).
54+
[[/fr]]
55+
56+
Pour plus de détails sur le changement de mot de passe, lisez [Incidents et interruptions de service](https://notification.canada.ca/etat-du-systeme#h-incidents-et-interruptions-de-service).""",
57+
},
58+
{
59+
"id": current_app.config["PASSWORD_RESET_TEMPLATE_ID"],
60+
"template_type": "email",
61+
"category_id": CAT_AUTH_ID,
62+
"subject": "Reset your password | Réinitialiser votre mot de passe",
63+
"content": """[[en]]
64+
Hi ((user_name)),
65+
66+
We received a request to reset your password on GC Notify.
67+
68+
If you didn't request this email, you can ignore it – your password has not been changed.
69+
70+
To reset your password, use this link:
71+
[Password reset](((url)) ""Password reset"")
72+
[[/en]]
73+
74+
___
75+
76+
[[fr]]
77+
Bonjour ((user_name)),
78+
79+
Nous avons reçu une demande de réinitialisation de votre mot de passe dans Notification GC.
80+
81+
Si vous n'avez pas demandé ce courriel, vous pouvez l'ignorer - votre mot de passe n'a pas été changé.
82+
83+
Pour réinitialiser votre mot de passe, utilisez ce lien :
84+
[Réinitialisation du mot de passe](((url)) ""Réinitialisation du mot de passe"")
85+
[[/fr]]""",
86+
},
87+
]
88+
89+
90+
def upgrade():
91+
conn = op.get_bind()
92+
93+
for template in templates:
94+
current_version = conn.execute("select version from templates where id='{}'".format(template["id"])).fetchone()
95+
name = conn.execute("select name from templates where id='{}'".format(template["id"])).fetchone()
96+
template["version"] = current_version[0] + 1
97+
template["name"] = name[0]
98+
99+
template_update = """
100+
UPDATE templates SET content = '{}', subject = '{}', version = '{}', updated_at = '{}', template_category_id = '{}'
101+
WHERE id = '{}'
102+
"""
103+
template_update_no_subject = """
104+
UPDATE templates SET content = '{}', version = '{}', updated_at = '{}', template_category_id = '{}'
105+
WHERE id = '{}'
106+
"""
107+
template_history_insert = """
108+
INSERT INTO templates_history (id, name, template_type, created_at, content, archived, service_id, subject,
109+
created_by_id, version, hidden, template_category_id)
110+
VALUES ('{}', '{}', '{}', '{}', '{}', False, '{}', '{}', '{}', {}, false, '{}')
111+
"""
112+
113+
for template in templates:
114+
escaped_content = template["content"].replace("'", "''")
115+
escaped_subject = template["subject"].replace("'", "''") if template["subject"] is not None else None
116+
117+
if template["subject"] is not None:
118+
op.execute(
119+
template_update.format(
120+
escaped_content,
121+
escaped_subject,
122+
template["version"],
123+
datetime.utcnow(),
124+
template["category_id"],
125+
template["id"],
126+
)
127+
)
128+
else:
129+
op.execute(
130+
template_update_no_subject.format(
131+
escaped_content,
132+
template["version"],
133+
datetime.utcnow(),
134+
template["category_id"],
135+
template["id"],
136+
)
137+
)
138+
139+
op.execute(
140+
template_history_insert.format(
141+
template["id"],
142+
template["name"],
143+
template["template_type"],
144+
datetime.utcnow(),
145+
escaped_content,
146+
current_app.config["NOTIFY_SERVICE_ID"],
147+
escaped_subject if escaped_subject is not None else "",
148+
current_app.config["NOTIFY_USER_ID"],
149+
template["version"],
150+
template["category_id"],
151+
)
152+
)
153+
154+
def downgrade():
155+
pass

0 commit comments

Comments
 (0)