diff --git a/app/__init__.py b/app/__init__.py index 8aa82abb7..561b322f8 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -409,7 +409,7 @@ def valid_phone_number(phone_number): def format_notification_type(notification_type): return { 'email': 'Email', - 'sms': 'SMS', + 'sms': 'Text message', 'letter': 'Letter' }[notification_type] diff --git a/app/models/template_list.py b/app/models/template_list.py index def776b08..144a4675a 100644 --- a/app/models/template_list.py +++ b/app/models/template_list.py @@ -1,3 +1,6 @@ +from app import format_notification_type + + class TemplateList(): def __init__( @@ -128,12 +131,17 @@ class TemplateListTemplate(TemplateListItem): ): super().__init__(template, ancestors) self.service_id = service_id - self.hint = { - 'email': 'Email template', - 'sms': 'Text message template', - 'letter': 'Letter template', - 'broadcast': 'Broadcast template', - }.get(template['template_type']) + self.template_type = template['template_type'] + self.content = template.get('content') + + @property + def hint(self): + if self.template_type == 'broadcast': + max_length_in_chars = 40 + if len(self.content) > (max_length_in_chars + 2): + return self.content[:max_length_in_chars].strip() + '…' + return self.content + return format_notification_type(self.template_type) + ' template' class TemplateListFolder(TemplateListItem): diff --git a/tests/app/main/views/test_templates.py b/tests/app/main/views/test_templates.py index 8dc5daeb1..e6cf72527 100644 --- a/tests/app/main/views/test_templates.py +++ b/tests/app/main/views/test_templates.py @@ -199,6 +199,75 @@ def test_should_show_page_for_choosing_a_template( mock_get_template_folders.assert_called_once_with(SERVICE_ONE_ID) +def test_should_show_page_of_broadcast_templates( + mocker, + client_request, + service_one, + fake_uuid, + mock_get_template_folders, +): + service_one['permissions'] += ['broadcast'] + mocker.patch( + 'app.service_api_client.get_service_templates', + return_value={'data': [ + template_json( + SERVICE_ONE_ID, + fake_uuid, + type_='broadcast', + name='A', + content='a' * 40, + ), + template_json( + SERVICE_ONE_ID, + fake_uuid, + type_='broadcast', + name='B', + content='b' * 42, + ), + template_json( + SERVICE_ONE_ID, + fake_uuid, + type_='broadcast', + name='C', + content='c' * 43, + ), + template_json( + SERVICE_ONE_ID, + fake_uuid, + type_='broadcast', + name='D', + # This should be truncated at 40 chars, then have the + # trailing space stripped + content=('d' * 39) + ' ' + ('d' * 40), + ), + ]} + ) + page = client_request.get( + 'main.choose_template', + service_id=SERVICE_ONE_ID, + ) + assert [ + ( + normalize_spaces(template.select_one('.govuk-link').text), + normalize_spaces(template.select_one('.govuk-hint').text), + ) + for template in page.select('.template-list-item') + ] == [ + ( + 'A', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + ), + ( + 'B', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + ), + ( + 'C', 'cccccccccccccccccccccccccccccccccccccccc…', + ), + ( + 'D', 'ddddddddddddddddddddddddddddddddddddddd…', + ), + ] + + def test_choose_template_can_pass_through_an_initial_state_to_templates_and_folders_selection_form( client_request, mock_get_template_folders,