Validate length of broadcast content

Depends on:
- [ ] https://github.com/alphagov/notifications-utils/pull/826/files

Adds error messages for when the content of a broadcast template is too
long.

The error message is explicit when this is cause by non-GSM characters.
We may not want to expose this complexity to our users, but it’s useful
for now while we’re testing things out.
This commit is contained in:
Chris Hill-Scott
2020-12-24 13:56:24 +00:00
parent 223003517a
commit 8302b2b667
5 changed files with 58 additions and 2 deletions

View File

@@ -40,6 +40,7 @@ from wtforms.validators import URL, DataRequired, Length, Optional, Regexp
from app import format_thousands
from app.main.validators import (
BroadcastLength,
CommonlyUsedPassword,
CsvFileValidator,
DoesNotStartWithDoubleZero,
@@ -1324,6 +1325,7 @@ class BroadcastTemplateForm(SMSTemplateForm):
def validate_template_content(self, field):
OnlySMSCharacters(template_type='broadcast')(None, field)
NoPlaceholders()(None, field)
BroadcastLength()(None, field)
class LetterAddressForm(StripWhitespaceForm):

View File

@@ -7,6 +7,7 @@ from notifications_utils.recipients import (
validate_email_address,
)
from notifications_utils.sanitise_text import SanitiseSMS
from notifications_utils.template import BroadcastMessageTemplate
from wtforms import ValidationError
from app.main._commonly_used_passwords import commonly_used_passwords
@@ -120,6 +121,28 @@ class NoPlaceholders:
raise ValidationError(self.message)
class BroadcastLength:
def __call__(self, form, field):
template = BroadcastMessageTemplate({
'template_type': 'broadcast',
'content': field.data,
})
if template.content_too_long:
non_gsm_characters = list(sorted(template.non_gsm_characters))
if non_gsm_characters:
raise ValidationError(
f'Content must be {template.max_content_count:,.0f} '
f'characters or fewer because it contains '
f'{formatted_list(non_gsm_characters, conjunction="and", before_each="", after_each="")}'
)
raise ValidationError(
f'Content must be {template.max_content_count:,.0f} '
f'characters or fewer'
)
class LettersNumbersFullStopsAndUnderscoresOnly:
regex = re.compile(r'^[a-zA-Z0-9\s\._]+$')