Skip to content

Commit 1088779

Browse files
Feat/add support for custom alt text (#291)
* feat(alt text): add support for alt text to classes * feat(alt text): add support for alt text to jinja templates * feat(alt text): update tests to cover all scenarios * chore: formatting * Up the version number --------- Co-authored-by: Jumana Bahrainwala <jzbahrai@uwaterloo.ca>
1 parent 0146718 commit 1088779

7 files changed

Lines changed: 33 additions & 13 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
docopt==0.6.2
22
Flask==2.3.3
33
markupsafe==2.1.4
4-
git+https://github.com/cds-snc/notifier-utils.git@52.2.0#egg=notifications-utils
4+
git+https://github.com/cds-snc/notifier-utils.git@52.2.1#egg=notifications-utils

notifications_utils/jinja_templates/email/_custom_logo_no_background_colour.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<tr>
1010
<td style="padding: 0 10px 0 {% if brand_colour %} 8px; border-left: solid 2px {{ brand_colour }}{% else %} 10px;{% endif %}">
1111
<img src="{{ brand_logo }}" style="display: block; border: 0" height="{% if brand_text -%} 27 {%- else -%} 108 {%- endif %}"
12-
alt="{% if brand_text %} {% else -%}{{ brand_name }}{%- endif %}" />
12+
alt="{% if alt_text_en %}{{ alt_text_en }} / {{ alt_text_fr }}{% else -%}{{ brand_name}}{%- endif %}" />
1313
</td>
1414
{% if brand_text %}
1515
<td width="100%" style="font-family: Helvetica, Arial, sans-serif; font-size: 18px; line-height: 23px;" valign="center">

notifications_utils/jinja_templates/email/_custom_logo_with_background_colour.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
border="0"
1818
style="display: block; border: 0;"
1919
height="{% if brand_text -%} 27 {%- else -%} 54 {%- endif %}"
20-
alt="{% if brand_text %} {% else -%}{{ brand_name }}{%- endif %}"
20+
alt="{% if alt_text_en %}{{ alt_text_en }} / {{ alt_text_fr }}{% else -%}{{ brand_name}}{%- endif %}"
2121
/>
2222
</td>
2323
{% endif %}

notifications_utils/jinja_templates/email/email_preview_template.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<img
6565
src="https://{{ asset_domain }}/{{ brand_logo }}"
6666
style="padding-left:0; display: block; border: 0; height:{% if brand_text -%} 27 {%- else -%} 108 {%- endif %}px"
67-
alt="{% if brand_text %} {% else -%}{{ brand_name }}{%- endif %}"
67+
alt="{% if alt_text_en %}{{ alt_text_en }} / {{ alt_text_fr }}{% else -%}{{ brand_name}}{%- endif %}"
6868
/>
6969
</div>
7070

notifications_utils/template.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ def __init__(
363363
brand_name=None,
364364
jinja_path=None,
365365
allow_html=False,
366+
alt_text_en=None,
367+
alt_text_fr=None,
366368
):
367369
super().__init__(template, values, jinja_path=jinja_path)
368370
self.fip_banner_english = fip_banner_english
@@ -374,6 +376,8 @@ def __init__(
374376
self.logo_with_background_colour = logo_with_background_colour
375377
self.brand_name = brand_name
376378
self.allow_html = allow_html
379+
self.alt_text_en = alt_text_en
380+
self.alt_text_fr = alt_text_fr
377381
# set this again to make sure the correct either utils / downstream local jinja is used
378382
# however, don't set if we are in a test environment (to preserve the above mock)
379383
if "pytest" not in sys.modules:
@@ -413,6 +417,8 @@ def __str__(self):
413417
"brand_colour": self.brand_colour,
414418
"logo_with_background_colour": self.logo_with_background_colour,
415419
"brand_name": self.brand_name,
420+
"alt_text_en": self.alt_text_en,
421+
"alt_text_fr": self.alt_text_fr,
416422
}
417423
)
418424

@@ -456,6 +462,8 @@ def __init__(
456462
logo_with_background_colour=None,
457463
asset_domain=None,
458464
allow_html=False,
465+
alt_text_en=None,
466+
alt_text_fr=None,
459467
):
460468
super().__init__(
461469
template,
@@ -476,6 +484,8 @@ def __init__(
476484
self.brand_name = brand_name
477485
self.asset_domain = asset_domain or "assets.notification.canada.ca"
478486
self.allow_html = allow_html
487+
self.alt_text_en = alt_text_en
488+
self.alt_text_fr = alt_text_fr
479489

480490
def __str__(self):
481491
return Markup(
@@ -500,6 +510,8 @@ def __str__(self):
500510
"brand_text": self.brand_text,
501511
"brand_name": self.brand_name,
502512
"asset_domain": self.asset_domain,
513+
"alt_text_en": self.alt_text_en,
514+
"alt_text_fr": self.alt_text_fr,
503515
}
504516
)
505517
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ include = '(notifications_utils|tests)/.*\.pyi?$'
55

66
[tool.poetry]
77
name = "notifications-utils"
8-
version = "52.2.0"
8+
version = "52.2.1"
99
description = "Shared python code for Notification - Provides logging utils etc."
1010
authors = ["Canadian Digital Service"]
1111
license = "MIT license"

tests/test_template_types.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ def test_alt_text_with_brand_text_and_fip_banner_english_shown(renderer):
143143
brand_text="Example",
144144
logo_with_background_colour=True,
145145
brand_name="Notify Logo",
146+
alt_text_en="alt_text_en",
147+
alt_text_fr="alt_text_fr",
146148
)
147149
)
148-
assert 'alt=" "' in email
150+
assert 'alt="alt_text_en / alt_text_fr"' in email
149151
assert 'alt="Notify Logo"' not in email
150152

151153

@@ -159,10 +161,12 @@ def test_alt_text_with_no_brand_text_and_fip_banner_english_shown(renderer):
159161
brand_text=None,
160162
logo_with_background_colour=True,
161163
brand_name="Notify Logo",
164+
alt_text_en="alt_text_en",
165+
alt_text_fr="alt_text_fr",
162166
)
163167
)
164168
assert 'alt="Symbol of the Government of Canada / Symbole du gouvernement du Canada"' in email
165-
assert 'alt="Notify Logo"' in email
169+
assert 'alt="alt_text_en / alt_text_fr"' in email
166170

167171

168172
@pytest.mark.parametrize("renderer", [HTMLEmailTemplate, EmailPreviewTemplate])
@@ -184,15 +188,17 @@ def test_alt_text_with_no_brand_text_and_fip_banner_french_shown(renderer):
184188

185189
@pytest.mark.parametrize("renderer", [HTMLEmailTemplate, EmailPreviewTemplate])
186190
@pytest.mark.parametrize(
187-
"logo_with_background_colour, brand_text, expected_alt_text",
191+
"logo_with_background_colour, brand_text, alt_text_en, alt_text_fr, expected_alt_text",
188192
[
189-
(True, None, 'alt="Notify Logo"'),
190-
(True, "Example", 'alt=" "'),
191-
(False, "Example", 'alt=" "'),
192-
(False, None, 'alt="Notify Logo"'),
193+
(True, None, None, None, 'alt="Notify Logo"'),
194+
(True, "Example", "alt_text_en", "alt_text_fr", 'alt="alt_text_en / alt_text_fr"'),
195+
(False, "Example", None, None, 'alt="Notify Logo"'),
196+
(False, None, "alt_text_en", "alt_text_fr", 'alt="alt_text_en / alt_text_fr"'),
193197
],
194198
)
195-
def test_alt_text_with_no_fip_banner(logo_with_background_colour, brand_text, expected_alt_text, renderer):
199+
def test_alt_text_with_no_fip_banner(
200+
logo_with_background_colour, brand_text, alt_text_en, alt_text_fr, expected_alt_text, renderer
201+
):
196202
email = str(
197203
renderer(
198204
{"content": "hello world", "subject": ""},
@@ -201,6 +207,8 @@ def test_alt_text_with_no_fip_banner(logo_with_background_colour, brand_text, ex
201207
brand_text=brand_text,
202208
logo_with_background_colour=logo_with_background_colour,
203209
brand_name="Notify Logo",
210+
alt_text_en=alt_text_en,
211+
alt_text_fr=alt_text_fr,
204212
)
205213
)
206214

0 commit comments

Comments
 (0)