diff --git a/app/main/views/templates.py b/app/main/views/templates.py index 87f32b60f..e9e5f5b61 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -2,6 +2,7 @@ from functools import partial from flask import abort, flash, jsonify, redirect, render_template, request, url_for from flask_login import current_user +from markupsafe import Markup from notifications_python_client.errors import HTTPError from notifications_utils import SMS_CHAR_COUNT_LIMIT @@ -679,8 +680,23 @@ def count_content_length(service_id, template_type): ) +def _is_latin1(s): + return bool(s.encode(encoding="latin-1", errors="strict")) + + def _get_content_count_error_and_message_for_template(template): + url = "https://en.wikipedia.org/wiki/ISO/IEC_8859-1" if template.template_type == "sms": + s1 = f"Use of characters outside the IEC_8859-1 character set may increase " + s2 = "the message fragment count, resulting in additional charges, and these IEC_8859-1 " + s3 = "characters may not display properly on some older mobile devices." + + warning = "" + try: + _is_latin1(template.content) + except UnicodeEncodeError: + warning = f"{s1}{s2}{s3}" + if template.is_message_too_long(): return True, ( f"You have " @@ -689,11 +705,16 @@ def _get_content_count_error_and_message_for_template(template): ) if template.placeholders: return False, ( - f"Will be charged as {message_count(template.fragment_count, template.template_type)} " - f"(not including personalization)" + Markup( + f"Will be charged as {message_count(template.fragment_count, template.template_type)} " + f"(not including personalization). {warning}" + ) ) return False, ( - f"Will be charged as {message_count(template.fragment_count, template.template_type)} " + # Markup marks html contents safe so that they render properly. Don't use it if there is user input. + Markup( + f"Will be charged as {message_count(template.fragment_count, template.template_type)}. {warning} " + ) ) diff --git a/app/templates/views/edit-sms-template.html b/app/templates/views/edit-sms-template.html index b3f1da603..47ddb2379 100644 --- a/app/templates/views/edit-sms-template.html +++ b/app/templates/views/edit-sms-template.html @@ -50,7 +50,7 @@
-
+
@@ -69,7 +69,7 @@

- +

How to customize your message

diff --git a/tests/app/main/views/test_templates.py b/tests/app/main/views/test_templates.py index 409a83ae0..bc97a79ad 100644 --- a/tests/app/main/views/test_templates.py +++ b/tests/app/main/views/test_templates.py @@ -2112,7 +2112,7 @@ def test_content_count_json_endpoint( html = json.loads(response.get_data(as_text=True))["html"] snippet = BeautifulSoup(html, "html.parser").select_one("span") - assert normalize_spaces(snippet.text) == expected_message + assert expected_message in normalize_spaces(snippet.text) if snippet.has_attr("class"): assert snippet["class"] == [expected_class]