-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtemplate.py
More file actions
904 lines (800 loc) · 29.8 KB
/
Copy pathtemplate.py
File metadata and controls
904 lines (800 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
import math
import sys
from datetime import datetime
from html import unescape
from os import path
from flask import Markup
from jinja2 import Environment, FileSystemLoader
from notifications_utils import EMAIL_CHAR_COUNT_LIMIT, SMS_CHAR_COUNT_LIMIT, TEMPLATE_NAME_CHAR_COUNT_LIMIT
from notifications_utils.columns import Columns
from notifications_utils.field import Field
from notifications_utils.formatters import (
add_language_divs,
add_prefix,
add_rtl_divs,
add_trailing_newline,
autolink_sms,
escape_html,
escape_lang_tags,
escape_rtl_tags,
make_quotes_smart,
nl2br,
nl2li,
normalise_newlines,
normalise_whitespace,
notify_email_markdown,
notify_email_preheader_markdown,
notify_letter_preview_markdown,
notify_plain_text_email_markdown,
remove_empty_lines,
remove_language_divs,
remove_nested_list_padding,
remove_rtl_divs,
remove_smart_quotes_from_email_addresses,
remove_whitespace_before_punctuation,
replace_hyphens_with_en_dashes,
replace_hyphens_with_non_breaking_hyphens,
sms_encode,
strip_dvla_markup,
strip_leading_whitespace,
strip_pipes,
strip_unsupported_characters,
tweak_dvla_list_markup,
unlink_govuk_escaped,
)
from notifications_utils.sanitise_text import SanitiseSMS
from notifications_utils.strftime_codes import no_pad_day
from notifications_utils.take import Take
from notifications_utils.template_change import TemplateChange
from notifications_utils.validate_html import check_if_string_contains_valid_html
template_env = Environment(
loader=FileSystemLoader(
path.join(
path.dirname(path.abspath(__file__)),
"jinja_templates",
)
)
)
default_placeholders = {
"en": {
"email_recipient": "((email address))",
"sms_recipient": "((phone number))",
},
"fr": {
"email_recipient": "((adresse courriel))",
"sms_recipient": "((numéro de téléphone))",
},
}
class Template:
encoding = "utf-8"
def __init__(
self,
template,
values=None,
redact_missing_personalisation=False,
jinja_path=None,
):
if not isinstance(template, dict):
raise TypeError("Template must be a dict")
if values is not None and not isinstance(values, dict):
raise TypeError("Values must be a dict")
self.id = template.get("id", None)
self.name = template.get("name", None)
self.content = template["content"]
self.values = values
self.template_type = template.get("template_type", None)
self._template = template
self.redact_missing_personalisation = redact_missing_personalisation
if jinja_path is not None:
self.template_env = Environment(
loader=FileSystemLoader(
path.join(
path.dirname(jinja_path),
"jinja_templates",
)
)
)
else:
self.template_env = Environment(
loader=FileSystemLoader(
path.join(
path.dirname(path.abspath(__file__)),
"jinja_templates",
)
)
)
def __repr__(self):
return '{}("{}", {})'.format(self.__class__.__name__, self.content, self.values)
def __str__(self):
return Markup(
Field(
self.content,
self.values,
html="escape",
redact_missing_personalisation=self.redact_missing_personalisation,
)
)
@property
def values(self):
if hasattr(self, "_values"):
return self._values
return {}
@values.setter
def values(self, value):
if not value:
self._values = {}
else:
placeholders = Columns.from_keys(self.placeholders)
self._values = Columns(value).as_dict_with_keys(
self.placeholders | set(key for key in value.keys() if Columns.make_key(key) not in placeholders.keys())
)
@property
def placeholders(self):
return Field(self.content).placeholders
@property
def placeholders_meta(self):
return Field(self.content).placeholders_meta
@property
def missing_data(self):
return list(placeholder for placeholder in self.placeholders if self.values.get(placeholder) is None)
@property
def additional_data(self):
return self.values.keys() - self.placeholders
def get_raw(self, key, default=None):
return self._template.get(key, default)
def compare_to(self, new):
return TemplateChange(self, new)
def is_message_too_long(self):
return False
def is_name_too_long(self):
return False
class SMSMessageTemplate(Template):
CHAR_COUNT_LIMIT = SMS_CHAR_COUNT_LIMIT
NAME_CHAR_LIMIT = TEMPLATE_NAME_CHAR_COUNT_LIMIT
def __init__(
self,
template,
values=None,
prefix=None,
show_prefix=True,
sender=None,
jinja_path=None,
):
self.prefix = prefix
self.show_prefix = show_prefix
self.sender = sender
super().__init__(template, values, jinja_path=jinja_path)
def __str__(self):
return (
Take(Field(self.content, self.values, html="passthrough"))
.then(add_prefix, self.prefix)
.then(sms_encode)
.then(remove_whitespace_before_punctuation)
.then(normalise_newlines)
.then(str.strip)
)
@property
def prefix(self):
return self._prefix if self.show_prefix else None
@prefix.setter
def prefix(self, value):
self._prefix = value
def _encoded_content(self):
"""Return the SMS-encoded content used for both character counting and Unicode detection.
When values are set, placeholders are already replaced via __str__. When no values are
set, placeholder syntax is stripped before encoding so that placeholder names don't
inflate the character count or skew Unicode detection.
normalise_newlines is applied in both paths so that CRLF sequences (\\r\\n) submitted by
browsers are counted as a single newline unit, matching what is actually transmitted.
"""
if self._values:
# we always want to call SMSMessageTemplate.__str__ regardless of subclass, to avoid any html formatting
return SMSMessageTemplate.__str__(self)
return normalise_newlines(sms_encode(add_prefix(Field.placeholder_pattern.sub("", self.content.strip()), self.prefix)))
@property
def content_count(self):
return count_sms_character_units(self._encoded_content())
@property
def fragment_count(self):
content = self._encoded_content()
return get_sms_fragment_count(count_sms_character_units(content), is_unicode(content))
def is_message_too_long(self):
return self.content_count > self.CHAR_COUNT_LIMIT
def is_name_too_long(self):
return len(self.name) > self.NAME_CHAR_LIMIT if self.name else False
class SMSPreviewTemplate(SMSMessageTemplate):
def __init__(
self,
template,
values=None,
prefix=None,
show_prefix=True,
sender=None,
show_recipient=False,
show_sender=False,
downgrade_non_sms_characters=True,
redact_missing_personalisation=False,
jinja_path=None,
user_language="en",
):
self.user_language = user_language
self.show_recipient = show_recipient
self.show_sender = show_sender
self.downgrade_non_sms_characters = downgrade_non_sms_characters
super().__init__(template, values, prefix, show_prefix, sender, jinja_path=jinja_path)
self.redact_missing_personalisation = redact_missing_personalisation
self.jinja_template = self.template_env.get_template("sms_preview_template.jinja2")
def __str__(self):
return Markup(
self.jinja_template.render(
{
"sender": self.sender,
"show_sender": self.show_sender,
"recipient": Field(default_placeholders[self.user_language]["sms_recipient"], self.values, html="escape"),
"show_recipient": self.show_recipient,
"body": Take(
Field(
self.content,
self.values,
html="escape",
redact_missing_personalisation=self.redact_missing_personalisation,
)
)
.then(
add_prefix,
(escape_html(self.prefix) or None) if self.show_prefix else None,
)
.then(sms_encode if self.downgrade_non_sms_characters else str)
.then(remove_whitespace_before_punctuation)
.then(nl2br)
.then(autolink_sms),
}
)
)
class WithSubjectTemplate(Template):
def __init__(
self,
template,
values=None,
redact_missing_personalisation=False,
jinja_path=None,
):
self._subject = template["subject"]
super().__init__(
template,
values,
redact_missing_personalisation=redact_missing_personalisation,
jinja_path=jinja_path,
)
def __str__(self):
return str(
Field(
self.content,
self.values,
html="passthrough",
redact_missing_personalisation=self.redact_missing_personalisation,
markdown_lists=True,
)
)
@property
def subject(self):
return Markup(
Take(
Field(
self._subject,
self.values,
html="escape",
redact_missing_personalisation=self.redact_missing_personalisation,
)
)
.then(do_nice_typography)
.then(normalise_whitespace)
)
@property
def placeholders(self):
return Field(self._subject).placeholders | Field(self.content).placeholders
class PlainTextEmailTemplate(WithSubjectTemplate):
def __str__(self):
return (
Take(Field(self.content, self.values, html="passthrough", markdown_lists=True))
.then(unlink_govuk_escaped)
.then(strip_unsupported_characters)
.then(add_trailing_newline)
.then(notify_plain_text_email_markdown)
.then(do_nice_typography)
.then(unescape)
.then(strip_leading_whitespace)
.then(add_trailing_newline)
)
@property
def subject(self):
return Markup(
Take(
Field(
self._subject,
self.values,
html="passthrough",
redact_missing_personalisation=self.redact_missing_personalisation,
)
)
.then(do_nice_typography)
.then(normalise_whitespace)
)
class HTMLEmailTemplate(WithSubjectTemplate):
# Instantiate with regular jinja for test mocking (tests expect this to exist before init)
jinja_template = template_env.get_template("email/email_template.jinja2")
PREHEADER_LENGTH_IN_CHARACTERS = 256
CHAR_COUNT_LIMIT = EMAIL_CHAR_COUNT_LIMIT
NAME_CHAR_LIMIT = TEMPLATE_NAME_CHAR_COUNT_LIMIT
def __init__(
self,
template,
values=None,
fip_banner_english=True,
fip_banner_french=False,
complete_html=True,
brand_logo=None,
brand_text=None,
brand_colour=None,
logo_with_background_colour=False,
brand_name=None,
jinja_path=None,
allow_html=False,
alt_text_en=None,
alt_text_fr=None,
lang=None,
):
super().__init__(template, values, jinja_path=jinja_path)
self.fip_banner_english = fip_banner_english
self.fip_banner_french = fip_banner_french
self.complete_html = complete_html
self.brand_logo = brand_logo
self.brand_text = brand_text
self.brand_colour = brand_colour
self.logo_with_background_colour = logo_with_background_colour
self.brand_name = brand_name
self.allow_html = allow_html
self.alt_text_en = alt_text_en
self.alt_text_fr = alt_text_fr
self.text_direction_rtl = template.get("text_direction_rtl", False)
# BCP 47 language tag for the rendered <html lang="..."> attribute. Defaults to
# "en" so existing callers keep their current behaviour, but French / bilingual
# callers should pass "fr" or "und" so screen readers announce the document
# language correctly (WCAG 3.1.1).
self.lang = lang or "en"
# set this again to make sure the correct either utils / downstream local jinja is used
# however, don't set if we are in a test environment (to preserve the above mock)
if "pytest" not in sys.modules:
self.jinja_template = self.template_env.get_template("email/email_template.jinja2")
@property
def preheader(self):
return " ".join(
Take(
Field(
self.content,
self.values,
html="strip" if self.allow_html else "escape",
markdown_lists=True,
)
)
.then(unlink_govuk_escaped)
.then(strip_unsupported_characters)
.then(add_trailing_newline)
.then(notify_email_preheader_markdown)
.then(remove_language_divs)
.then(remove_rtl_divs)
.then(do_nice_typography)
.split()
)[: self.PREHEADER_LENGTH_IN_CHARACTERS].strip()
def __str__(self):
return self.jinja_template.render(
{
"subject": self.subject,
"body": get_html_email_body(self.content, self.values, html="passthrough" if self.allow_html else "escape"),
"preheader": self.preheader,
"fip_banner_english": self.fip_banner_english,
"fip_banner_french": self.fip_banner_french,
"complete_html": self.complete_html,
"brand_logo": self.brand_logo,
"brand_text": self.brand_text,
"brand_colour": self.brand_colour,
"logo_with_background_colour": self.logo_with_background_colour,
"brand_name": self.brand_name,
"alt_text_en": self.alt_text_en,
"alt_text_fr": self.alt_text_fr,
"text_direction_rtl": self.text_direction_rtl,
"lang": self.lang,
}
)
@property
def content_count(self):
if self.missing_data:
# variables have not yet been populated, so just take the length of
# of the template counting content like "((name))" as 8 characters
return len(self._template["content"])
# this is the length of the template after placeholders have been replaced
plaintext_email = Take(Field(self.content, self.values, html="passthrough", markdown_lists=True))
return len(plaintext_email)
def is_message_too_long(self):
return self.content_count > self.CHAR_COUNT_LIMIT
def is_name_too_long(self):
return len(self.name) > self.NAME_CHAR_LIMIT if self.name else False
class EmailPreviewTemplate(WithSubjectTemplate):
CHAR_COUNT_LIMIT = EMAIL_CHAR_COUNT_LIMIT
NAME_CHAR_LIMIT = TEMPLATE_NAME_CHAR_COUNT_LIMIT
def __init__(
self,
template,
values=None,
from_name=None,
from_address=None,
reply_to=None,
show_recipient=True,
redact_missing_personalisation=False,
jinja_path=None,
fip_banner_english=None,
fip_banner_french=None,
brand_colour=None,
brand_logo=None,
brand_text=None,
brand_name=None,
logo_with_background_colour=None,
asset_domain=None,
allow_html=False,
alt_text_en=None,
alt_text_fr=None,
user_language="en",
lang=None,
):
super().__init__(
template,
values,
redact_missing_personalisation=redact_missing_personalisation,
jinja_path=jinja_path,
)
self.from_name = from_name
self.from_address = from_address
self.reply_to = reply_to
self.show_recipient = show_recipient
self.jinja_template = self.template_env.get_template("email/email_preview_template.jinja2")
self.fip_banner_english = fip_banner_english
self.fip_banner_french = fip_banner_french
self.brand_colour = brand_colour
self.brand_logo = brand_logo
self.brand_text = brand_text
self.brand_name = brand_name
self.asset_domain = asset_domain or "assets.notification.canada.ca"
self.allow_html = allow_html
self.alt_text_en = alt_text_en
self.alt_text_fr = alt_text_fr
self.user_language = user_language
self.text_direction_rtl = template.get("text_direction_rtl", False)
self.lang = lang or "en"
def __str__(self):
return Markup(
self.jinja_template.render(
{
"body": get_html_email_body(
self.content,
self.values,
redact_missing_personalisation=self.redact_missing_personalisation,
html="passthrough" if self.allow_html else "escape",
),
"subject": self.subject,
"from_name": escape_html(self.from_name),
"from_address": self.from_address,
"reply_to": self.reply_to,
"recipient": Field(default_placeholders[self.user_language]["email_recipient"], self.values),
"show_recipient": self.show_recipient,
"fip_banner_english": self.fip_banner_english,
"fip_banner_french": self.fip_banner_french,
"brand_colour": self.brand_colour,
"brand_logo": self.brand_logo,
"brand_text": self.brand_text,
"brand_name": self.brand_name,
"asset_domain": self.asset_domain,
"alt_text_en": self.alt_text_en,
"alt_text_fr": self.alt_text_fr,
"text_direction_rtl": self.text_direction_rtl,
"lang": self.lang,
}
)
)
@property
def subject(self):
return (
Take(
Field(
self._subject,
self.values,
html="escape",
redact_missing_personalisation=self.redact_missing_personalisation,
)
)
.then(do_nice_typography)
.then(normalise_whitespace)
)
@property
def content_count(self):
if self.missing_data:
# variables have not yet been populated, so just take the length of
# of the template counting content like "((name))" as 8 characters
return len(self._template["content"])
# this is the length of the template after placeholders have been replaced
plaintext_email = Take(Field(self.content, self.values, html="passthrough", markdown_lists=True))
return len(plaintext_email)
def is_message_too_long(self):
return self.content_count > self.CHAR_COUNT_LIMIT
def is_name_too_long(self):
return len(self.name) > self.NAME_CHAR_LIMIT if self.name else False
class LetterPreviewTemplate(WithSubjectTemplate):
jinja_template = template_env.get_template("letter_pdf/preview.jinja2")
address_block = "\n".join(
[
"((address line 1))",
"((address line 2))",
"((address line 3))",
"((address line 4))",
"((address line 5))",
"((address line 6))",
"((postcode))",
]
)
def __init__(
self,
template,
values=None,
contact_block=None,
admin_base_url="http://localhost:6012",
logo_file_name=None,
redact_missing_personalisation=False,
date=None,
):
self.contact_block = (contact_block or "").strip()
super().__init__(
template,
values,
redact_missing_personalisation=redact_missing_personalisation,
)
self.admin_base_url = admin_base_url
self.logo_file_name = logo_file_name
self.date = date or datetime.utcnow()
def __str__(self):
return Markup(
self.jinja_template.render(
{
"admin_base_url": self.admin_base_url,
"logo_file_name": self.logo_file_name,
# logo_class should only ever be None, svg or png
"logo_class": self.logo_file_name.lower()[-3:] if self.logo_file_name else None,
"subject": self.subject,
"message": self._message,
"address": self._address_block,
"contact_block": self._contact_block,
"date": self._date,
}
)
)
@property
def subject(self):
return (
Take(
Field(
self._subject,
self.values,
redact_missing_personalisation=self.redact_missing_personalisation,
html="escape",
)
)
.then(do_nice_typography)
.then(strip_pipes)
.then(strip_dvla_markup)
.then(normalise_whitespace)
)
@property
def placeholders(self):
return super().placeholders | Field(self.contact_block).placeholders
@property
def values_with_default_optional_address_lines(self):
keys = Columns.from_keys(
set(self.values.keys())
| {
"address line 3",
"address line 4",
"address line 5",
"address line 6",
}
).keys()
return {key: Columns(self.values).get(key) or "" for key in keys}
@property
def _address_block(self):
return (
Take(
Field(
self.address_block,
(
self.values_with_default_optional_address_lines
if all(
Columns(self.values).get(key)
for key in {
"address line 1",
"address line 2",
"postcode",
}
)
else self.values
),
html="escape",
translated=True,
)
)
.then(strip_pipes)
.then(remove_empty_lines)
.then(remove_whitespace_before_punctuation)
.then(nl2li)
)
@property
def _contact_block(self):
return (
Take(
Field(
"\n".join(line.strip() for line in self.contact_block.split("\n")),
self.values,
redact_missing_personalisation=self.redact_missing_personalisation,
html="escape",
)
)
.then(remove_whitespace_before_punctuation)
.then(nl2br)
.then(strip_pipes)
)
@property
def _date(self):
return self.date.strftime(f"{no_pad_day()} %B %Y")
@property
def _message(self):
return (
Take(
Field(
strip_dvla_markup(self.content),
self.values,
html="escape",
markdown_lists=True,
redact_missing_personalisation=self.redact_missing_personalisation,
)
)
.then(strip_pipes)
.then(add_trailing_newline)
.then(notify_letter_preview_markdown)
.then(do_nice_typography)
.then(replace_hyphens_with_non_breaking_hyphens)
.then(tweak_dvla_list_markup)
)
class LetterPrintTemplate(LetterPreviewTemplate):
jinja_template = template_env.get_template("letter_pdf/print.jinja2")
class LetterImageTemplate(LetterPreviewTemplate):
jinja_template = template_env.get_template("letter_image_template.jinja2")
first_page_number = 1
max_page_count = 10
def __init__(
self,
template,
values=None,
image_url=None,
page_count=None,
contact_block=None,
postage="second",
):
super().__init__(template, values, contact_block=contact_block)
if not image_url:
raise TypeError("image_url is required")
if not page_count:
raise TypeError("page_count is required")
if postage not in {"first", "second"}:
raise TypeError("postage must be first or second")
self.image_url = image_url
self.page_count = int(page_count)
self.postage = postage
@property
def last_page_number(self):
return min(self.page_count, self.max_page_count) + self.first_page_number
@property
def page_numbers(self):
return list(range(self.first_page_number, self.last_page_number))
@property
def too_many_pages(self):
return self.page_count > self.max_page_count
def __str__(self):
return Markup(
self.jinja_template.render(
{
"image_url": self.image_url,
"page_numbers": self.page_numbers,
"too_many_pages": self.too_many_pages,
"address": self._address_block,
"contact_block": self._contact_block,
"date": self._date,
"subject": self.subject,
"message": self._message,
"postage": self.postage,
}
)
)
class NeededByTemplateError(Exception):
def __init__(self, keys):
super(NeededByTemplateError, self).__init__(", ".join(keys))
class NoPlaceholderForDataError(Exception):
def __init__(self, keys):
super(NoPlaceholderForDataError, self).__init__(", ".join(keys))
def get_sms_fragment_count(character_count, is_unicode):
if is_unicode:
return 1 if character_count <= 70 else math.ceil(float(character_count) / 67)
else:
return 1 if character_count <= 160 else math.ceil(float(character_count) / 153)
def is_unicode(content):
non_gsm_allowed = (
SanitiseSMS.WELSH_NON_GSM_CHARACTERS
| SanitiseSMS.FRENCH_NON_GSM_CHARACTESR
| SanitiseSMS.INUKTITUK_CHARACTERS
| SanitiseSMS.CREE_CHARACTERS
| SanitiseSMS.OJIBWE_CHARACTERS
)
return set(content) & non_gsm_allowed
# GSM 03.38 extension characters — each occupies 2 units (basic char + escape prefix)
_GSM_EXTENDED_CHARS = set("^{}\\[~]|\u20ac")
def count_sms_character_units(content):
"""Return the number of GSM character units in *content*.
In GSM-7 mode, extension characters (^, {, }, \\, [, ~, ], |, €) each cost
2 units due to the required escape byte. All other GSM characters cost 1
unit. In Unicode (UCS-2) mode every character costs 1 unit.
"""
if is_unicode(content):
return len(content)
return sum(2 if c in _GSM_EXTENDED_CHARS else 1 for c in content)
def get_html_email_body(template_content, template_values, redact_missing_personalisation=False, html="escape"):
if html == "passthrough" and check_if_string_contains_valid_html(template_content) != []:
# template_content contains invalid html, so escape it
html = "escape"
return (
Take(
Field(
template_content,
template_values,
html=html,
markdown_lists=True,
redact_missing_personalisation=redact_missing_personalisation,
markdown_renderer=_render_conditional_email_markdown,
)
)
.then(unlink_govuk_escaped)
.then(strip_unsupported_characters)
.then(add_trailing_newline)
.then(escape_lang_tags)
.then(escape_rtl_tags)
.then(notify_email_markdown)
.then(remove_nested_list_padding)
.then(add_language_divs)
.then(add_rtl_divs)
.then(do_nice_typography)
)
def _render_conditional_email_markdown(content):
"""Render markdown for multiline conditional preview content.
Conditionals are pre-rendered, so we need to mirror the parsing pipeline in get_html_email_body
to ensure that language tags, RTL, lists, etc. that are inside conditionals are also pre-rendered
else the main markdown rendering pass in get_html_email_body breaks them apart and mangles formatting.
"""
result = escape_lang_tags(content)
result = escape_rtl_tags(result)
result = notify_email_markdown(result)
result = remove_nested_list_padding(result)
result = add_language_divs(result)
result = add_rtl_divs(result)
return result
def do_nice_typography(value):
return (
Take(value)
.then(remove_whitespace_before_punctuation)
.then(make_quotes_smart)
.then(remove_smart_quotes_from_email_addresses)
.then(replace_hyphens_with_en_dashes)
)