Updates to text area validation on template edit page

This commit is contained in:
Jonathan Bobel
2025-03-18 15:55:01 -04:00
parent bc83fab764
commit 1d911c4d44
3 changed files with 33 additions and 204 deletions

View File

@@ -108,32 +108,17 @@ class OnlySMSCharacters:
)
if non_sms_characters:
raise ValidationError(
"You cannot use {} in {}. {} will not show up properly on everyones phones.".format(
"Please remove the unaccepted character {} in your message, then save again".format(
formatted_list(
non_sms_characters,
conjunction="or",
conjunction="and",
before_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 cant use ((double brackets)) to personalize this message'
# )
# def __call__(self, form, field):
# if Field(field.data).placeholders:
# raise ValidationError(self.message)
class LettersNumbersSingleQuotesFullStopsAndUnderscoresOnly:
regex = re.compile(r"^[a-zA-Z0-9\s\._']+$")

View File

@@ -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):
if template.template_type == "sms":
s1 = "<html><body>Templates won't save with these characters ™ ∞ ≠ “ ≤ ≥ or any emoji present. "
s2 = "<br>The errors they create lead to texts displaying incorrectly on older mobile phones.</body></html>"
s1 = "<html><body>Looks like your template may have one of these characters ™ ∞ ≤ or ≥ or emoji, which won't save."
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 = ""
try:
_is_latin1(template.content)
except UnicodeEncodeError:
if contains_blocked_characters(template.content): # Block only disallowed characters
warning = f"{s1}{s2}"
# 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 warning:
return True, Markup(warning)
return False, Markup(warning)
# 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(
f"Will be charged as {message_count(template.fragment_count, template.template_type)}."
)

191
poetry.lock generated

File diff suppressed because it is too large Load Diff