diff --git a/app/main/views/send.py b/app/main/views/send.py index 2ace356f7..065862ce2 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -498,9 +498,18 @@ def send_test_preview(service_id, template_id, filetype): ) @user_has_permissions('send_messages') def choose_from_contact_list(service_id, template_id): + db_template = current_service.get_template_with_user_permission_or_403( + template_id, current_user + ) + template = get_template( + db_template, current_service, + ) return render_template( 'views/send-contact-list.html', - contact_lists=ContactLists(current_service.id), + contact_lists=ContactLists( + current_service.id, + template_type=template.template_type, + ), template=current_service.get_template(template_id), ) diff --git a/app/models/contact_list.py b/app/models/contact_list.py index b617a4463..c781df97f 100644 --- a/app/models/contact_list.py +++ b/app/models/contact_list.py @@ -125,5 +125,13 @@ class ContactList(JSONModel): class ContactLists(ModelList): + client_method = contact_list_api_client.get_contact_lists model = ContactList + + def __init__(self, service_id, template_type=None): + super().__init__(service_id) + self.items = [ + item for item in self.items + if template_type in {item['template_type'], None} + ] diff --git a/app/templates/views/send-contact-list.html b/app/templates/views/send-contact-list.html index 6bf5c9e01..1827485a0 100644 --- a/app/templates/views/send-contact-list.html +++ b/app/templates/views/send-contact-list.html @@ -15,39 +15,48 @@ back_link=url_for('.send_one_off', service_id=current_service.id, template_id=template.id) ) }} -
- {% call(item, row_number) list_table( - contact_lists, - caption="Existing contact lists", - caption_visible=False, - empty_message=( - 'You don’t have any contact lists yet' - ), - field_headings=[ - 'File', - 'Status' - ], - field_headings_visible=False - ) %} - {% call row_heading() %} -
- {{ item.original_file_name }} - - Uploaded {{ item.created_at|format_datetime_relative }} - -
- {% endcall %} - {% call field() %} - {{ big_number( - item.row_count, - smallest=True, - label=recipient_count_label( + {% if contact_lists %} +
+ {% call(item, row_number) list_table( + contact_lists, + caption="Existing contact lists", + caption_visible=False, + empty_message=( + 'You don’t have any contact lists yet' + ), + field_headings=[ + 'File', + 'Status' + ], + field_headings_visible=False + ) %} + {% call row_heading() %} +
+ {{ item.original_file_name }} + + Uploaded {{ item.created_at|format_datetime_relative }} + +
+ {% endcall %} + {% call field() %} + {{ big_number( item.row_count, - item.template_type - ) - ) }} + smallest=True, + label=recipient_count_label( + item.row_count, + item.template_type + ) + ) }} + {% endcall %} {% endcall %} - {% endcall %} -
+
+ {% else %} +

+ You have not saved any lists of {{ recipient_count_label(99, template.template_type) }} yet. +

+

+ To upload and save a new contact list, go to the uploads page. +

+ {% endif %} {% endblock %} diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index 01eae7d22..db80d72d6 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -3839,37 +3839,96 @@ def test_redirects_to_template_if_job_exists_already( ) +@pytest.mark.parametrize(( + 'template_type, ' + 'expected_list_id, ' + 'expected_filename, ' + 'expected_time, ' + 'expected_count' +), ( + ( + 'email', + '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6', + 'EmergencyContactList.xls', + 'Uploaded today at 10:59am', + '100 email addresses', + ), + ( + 'sms', + 'd7b0bd1a-d1c7-4621-be5c-3c1b4278a2ad', + 'phone number list.csv', + 'Uploaded today at 1:00pm', + '123 phone numbers', + ), +)) @freeze_time('2020-03-13 13:00') def test_choose_from_contact_list( mocker, client_request, - mock_get_service_template, mock_get_contact_lists, fake_uuid, + template_type, + expected_list_id, + expected_filename, + expected_time, + expected_count, ): + template = create_template(template_type=template_type) + mocker.patch( + 'app.service_api_client.get_service_template', + return_value={'data': template}, + ) page = client_request.get( 'main.choose_from_contact_list', service_id=SERVICE_ONE_ID, template_id=fake_uuid, ) - assert len(page.select('.file-list-filename-large')) == 2 + assert len(page.select('.file-list-filename-large')) == 1 assert normalize_spaces(page.select_one('.file-list-filename-large').text) == ( - 'EmergencyContactList.xls' + expected_filename ) assert page.select_one('a.file-list-filename-large')['href'] == url_for( 'main.send_from_contact_list', service_id=SERVICE_ONE_ID, - template_id=fake_uuid, - contact_list_id=fake_uuid, + template_id=template['id'], + contact_list_id=expected_list_id, ) assert normalize_spaces(page.select_one('.file-list-hint').text) == ( - 'Uploaded today at 10:59am' + expected_time ) assert normalize_spaces(page.select_one('.big-number-smallest').text) == ( - '100 email addresses' + expected_count ) +def test_choose_from_contact_list_with_no_lists( + mocker, + client_request, + mock_get_service_template, + fake_uuid, +): + mocker.patch( + 'app.models.contact_list.ContactLists.client_method', + return_value=[], + ) + page = client_request.get( + 'main.choose_from_contact_list', + service_id=SERVICE_ONE_ID, + template_id=fake_uuid, + ) + assert [ + normalize_spaces(p.text) for p in page.select('main p') + ] == [ + 'You have not saved any lists of phone numbers yet.', + 'To upload and save a new contact list, go to the uploads page.', + ] + assert page.select_one('main p a')['href'] == url_for( + 'main.uploads', + service_id=SERVICE_ONE_ID, + ) + assert not page.select('table') + + def test_send_from_contact_list( mocker, client_request, diff --git a/tests/conftest.py b/tests/conftest.py index ca0dac789..308a78a2a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1844,7 +1844,7 @@ def mock_create_contact_list(mocker, api_user_active): @pytest.fixture(scope='function') def mock_get_contact_lists(mocker, api_user_active, fake_uuid): - def _get(service_id): + def _get(service_id, template_type=None): return [{ 'created_at': '2020-03-13 10:59:56', 'created_by': 'Test User', @@ -1856,8 +1856,8 @@ def mock_get_contact_lists(mocker, api_user_active, fake_uuid): }, { 'created_at': '2020-03-13 13:00:00', 'created_by': 'Test User', - 'id': uuid4(), - 'original_file_name': 'another ist.csv', + 'id': 'd7b0bd1a-d1c7-4621-be5c-3c1b4278a2ad', + 'original_file_name': 'phone number list.csv', 'row_count': 123, 'service_id': service_id, 'template_type': 'sms',