mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-09 10:59:50 -04:00
Updates to text area validation on template edit page
This commit is contained in:
@@ -108,32 +108,17 @@ class OnlySMSCharacters:
|
|||||||
)
|
)
|
||||||
if non_sms_characters:
|
if non_sms_characters:
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
"You cannot use {} in {}. {} will not show up properly on everyone’s phones.".format(
|
"Please remove the unaccepted character {} in your message, then save again".format(
|
||||||
formatted_list(
|
formatted_list(
|
||||||
non_sms_characters,
|
non_sms_characters,
|
||||||
conjunction="or",
|
conjunction="and",
|
||||||
before_each="",
|
before_each="",
|
||||||
after_each="",
|
after_each="",
|
||||||
),
|
),
|
||||||
{
|
|
||||||
"sms": "text messages",
|
|
||||||
}.get(self._template_type),
|
|
||||||
("It" if len(non_sms_characters) == 1 else "They"),
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# class NoPlaceholders:
|
|
||||||
|
|
||||||
# def __init__(self, message=None):
|
|
||||||
# self.message = message or (
|
|
||||||
# 'You can’t use ((double brackets)) to personalize this message'
|
|
||||||
# )
|
|
||||||
|
|
||||||
# def __call__(self, form, field):
|
|
||||||
# if Field(field.data).placeholders:
|
|
||||||
# raise ValidationError(self.message)
|
|
||||||
|
|
||||||
|
|
||||||
class LettersNumbersSingleQuotesFullStopsAndUnderscoresOnly:
|
class LettersNumbersSingleQuotesFullStopsAndUnderscoresOnly:
|
||||||
regex = re.compile(r"^[a-zA-Z0-9\s\._']+$")
|
regex = re.compile(r"^[a-zA-Z0-9\s\._']+$")
|
||||||
|
|||||||
@@ -680,19 +680,20 @@ 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):
|
def _get_content_count_error_and_message_for_template(template):
|
||||||
if template.template_type == "sms":
|
if template.template_type == "sms":
|
||||||
s1 = "<html><body>Templates won't save with these characters ™ ∞ • – ≠ “ ‘ … ≤ ≥ or any emoji present. "
|
s1 = "<html><body>Looks like your template may have one of these characters • ™ ∞ ≤ or ≥ or emoji, which won't save."
|
||||||
s2 = "<br>The errors they create lead to texts displaying incorrectly on older mobile phones.</body></html>"
|
s2 = "<br>Please remove any unaccepted characters or emojis and try again.</body></html>"
|
||||||
|
|
||||||
|
# Define characters that should be blocked
|
||||||
|
BLOCKED_CHARACTERS = {"•", "™", "∞", "≤", "≥"}
|
||||||
|
|
||||||
|
def contains_blocked_characters(content):
|
||||||
|
"""Check if the content contains explicitly blocked characters."""
|
||||||
|
return any(c in BLOCKED_CHARACTERS for c in content)
|
||||||
|
|
||||||
warning = ""
|
warning = ""
|
||||||
try:
|
if contains_blocked_characters(template.content): # Block only disallowed characters
|
||||||
_is_latin1(template.content)
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
warning = f"{s1}{s2}"
|
warning = f"{s1}{s2}"
|
||||||
|
|
||||||
# If message is too long, return the length error
|
# If message is too long, return the length error
|
||||||
@@ -705,15 +706,9 @@ def _get_content_count_error_and_message_for_template(template):
|
|||||||
|
|
||||||
# If there's a warning, return it alone and hide the "Will be charged as..." text
|
# If there's a warning, return it alone and hide the "Will be charged as..." text
|
||||||
if warning:
|
if warning:
|
||||||
return True, Markup(warning)
|
return False, Markup(warning)
|
||||||
|
|
||||||
# Otherwise, show the message count as usual
|
# Otherwise, show the message count as usual
|
||||||
if template.placeholders:
|
|
||||||
return False, Markup(
|
|
||||||
f"Will be charged as {message_count(template.fragment_count, template.template_type)} "
|
|
||||||
f"(not including personalization)."
|
|
||||||
)
|
|
||||||
|
|
||||||
return False, Markup(
|
return False, Markup(
|
||||||
f"Will be charged as {message_count(template.fragment_count, template.template_type)}."
|
f"Will be charged as {message_count(template.fragment_count, template.template_type)}."
|
||||||
)
|
)
|
||||||
|
|||||||
191
poetry.lock
generated
191
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user