From 3a62946ecd2ab2982d479481f96bf1df487442f8 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 30 Oct 2018 14:24:50 +0000 Subject: [PATCH] Let people send one off letters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We didn’t used to allow this because it wasn’t really possible with the old DVLA set up and we didn’t think there’s a need. We think it’s possible now because, even though it’s cumbersome, it’s better than the manual process. --- app/main/views/send.py | 75 ++++++++++++++------ app/navigation.py | 4 ++ app/templates/views/templates/_template.html | 7 +- tests/app/main/views/test_send.py | 44 +++++++++--- 4 files changed, 90 insertions(+), 40 deletions(-) diff --git a/app/main/views/send.py b/app/main/views/send.py index 5992b4ac3..aec7e44e1 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -317,16 +317,13 @@ def send_test(service_id, template_id): def get_notification_check_endpoint(service_id, template): - if template.template_type == 'letter': - return make_and_upload_csv_file(service_id, template) - else: - return redirect(url_for( - 'main.check_notification', - service_id=service_id, - template_id=template.id, - # at check phase we should move to help stage 2 ("the template pulls in the data you provide") - help='2' if 'help' in request.args else None - )) + return redirect(url_for( + 'main.check_notification', + service_id=service_id, + template_id=template.id, + # at check phase we should move to help stage 2 ("the template pulls in the data you provide") + help='2' if 'help' in request.args else None + )) @main.route( @@ -669,6 +666,26 @@ def check_messages_preview(service_id, template_id, upload_id, filetype, row_ind return TemplatePreview.from_utils_template(template, filetype, page=page) +@main.route( + "/services///check.", + methods=['GET'], +) +@login_required +@user_has_permissions('send_messages') +def check_notification_preview(service_id, template_id, filetype): + if filetype == 'pdf': + page = None + elif filetype == 'png': + page = request.args.get('page', 1) + else: + abort(404) + + template = _check_notification( + service_id, template_id, + )['template'] + return TemplatePreview.from_utils_template(template, filetype, page=page) + + @main.route("/services//start-job/", methods=['POST']) @login_required @user_has_permissions('send_messages', restrict_admin_usage=True) @@ -762,8 +779,6 @@ def all_placeholders_in_session(placeholders): def get_send_test_page_title(template_type, help_argument, entering_recipient, name=None): if help_argument: return 'Example text message' - if template_type == 'letter': - return 'Print a test letter' if entering_recipient: return 'Send ‘{}’'.format(name) return 'Personalise this message' @@ -807,7 +822,10 @@ def get_back_link(service_id, template, step_index): @login_required @user_has_permissions('send_messages', restrict_admin_usage=True) def check_notification(service_id, template_id): - return _check_notification(service_id, template_id) + return render_template( + 'views/notifications/check.html', + **_check_notification(service_id, template_id), + ) def _check_notification(service_id, template_id, exception=None): @@ -823,25 +841,33 @@ def _check_notification(service_id, template_id, exception=None): current_service, show_recipient=True, email_reply_to=email_reply_to, - sms_sender=sms_sender + sms_sender=sms_sender, + letter_preview_url=url_for( + '.check_notification_preview', + service_id=service_id, + template_id=template_id, + filetype='png', + ), + page_count=get_page_count_for_letter(db_template), ) back_link = get_back_link(service_id, template, len(fields_to_fill_in(template))) if ( - not session.get('recipient') or - not all_placeholders_in_session(template.placeholders) + ( + not session.get('recipient') + and db_template['template_type'] != 'letter' + ) + or not all_placeholders_in_session(template.placeholders) ): - return redirect(back_link) + raise RequestRedirect(back_link) template.values = get_recipient_and_placeholders_from_session(template.template_type) - return render_template( - 'views/notifications/check.html', + return dict( template=template, back_link=back_link, help=get_help_argument(), - - **(get_template_error_dict(exception) if exception else {}) + **(get_template_error_dict(exception) if exception else {}), ) @@ -880,7 +906,7 @@ def send_notification(service_id, template_id): noti = notification_api_client.send_notification( service_id, template_id=template_id, - recipient=session['recipient'], + recipient=session['recipient'] or session['placeholders']['address line 1'], personalisation=session['placeholders'], sender_id=session['sender_id'] if 'sender_id' in session else None ) @@ -889,7 +915,10 @@ def send_notification(service_id, template_id): current_service.id, exception.message )) - return _check_notification(service_id, template_id, exception) + return render_template( + 'views/notifications/check.html', + **_check_notification(service_id, template_id, exception), + ) session.pop('placeholders') session.pop('recipient') diff --git a/app/navigation.py b/app/navigation.py index 148302750..cf893c5ca 100644 --- a/app/navigation.py +++ b/app/navigation.py @@ -125,6 +125,7 @@ class HeaderNavigation(Navigation): 'check_messages', 'check_messages_preview', 'check_notification', + 'check_notification_preview', 'choose_account', 'choose_service', 'choose_template', @@ -395,6 +396,7 @@ class MainNavigation(Navigation): 'check_and_resend_text_code', 'check_and_resend_verification_code', 'check_messages_preview', + 'check_notification_preview', 'choose_account', 'choose_service', 'confirm_edit_organisation_name', @@ -567,6 +569,7 @@ class CaseworkNavigation(Navigation): 'check_messages', 'check_messages_preview', 'check_notification', + 'check_notification_preview', 'choose_account', 'choose_service', 'choose_template_to_copy', @@ -801,6 +804,7 @@ class OrgNavigation(Navigation): 'check_messages', 'check_messages_preview', 'check_notification', + 'check_notification_preview', 'choose_account', 'choose_service', 'choose_template', diff --git a/app/templates/views/templates/_template.html b/app/templates/views/templates/_template.html index b6c19510d..333d35c1c 100644 --- a/app/templates/views/templates/_template.html +++ b/app/templates/views/templates/_template.html @@ -16,14 +16,9 @@
{% if template.template_type == 'letter' %} {% if current_user.has_permissions('send_messages', restrict_admin_usage=True) %} - {% endif %} diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index 7f570d3cb..3e8d321a8 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -1074,13 +1074,13 @@ def test_send_one_off_does_not_send_without_the_correct_permissions( ( mock_get_service_letter_template, partial(url_for, 'main.send_test'), - 'Print a test letter', + 'Send ‘Two week reminder’', False, ), ( mock_get_service_letter_template, partial(url_for, 'main.send_one_off'), - 'Print a test letter', + 'Send ‘Two week reminder’', False, ), ]) @@ -1595,10 +1595,9 @@ def test_send_test_letter_redirects_to_right_url( assert response.status_code == 302 assert response.location.startswith(url_for( - 'main.check_messages', + 'main.check_notification', service_id=SERVICE_ONE_ID, template_id=fake_uuid, - upload_id=fake_uuid, _external=True, )) @@ -2875,27 +2874,50 @@ def test_check_notification_shows_help( ) +@pytest.mark.parametrize('template, recipient, placeholders, expected_personalisation', ( + ( + mock_get_service_template, + '07700900001', + {'a': 'b'}, + {'a': 'b'}, + ), + ( + mock_get_service_email_template, + 'test@example.com', + {}, + {}, + ), + ( + mock_get_service_letter_template, + 'foo', + {}, + {}, + ), +)) def test_send_notification_submits_data( client_request, - service_one, fake_uuid, mock_send_notification, + template, + recipient, + placeholders, + expected_personalisation, ): with client_request.session_transaction() as session: - session['recipient'] = '07700900001' - session['placeholders'] = {'a': 'b'} + session['recipient'] = recipient + session['placeholders'] = placeholders client_request.post( 'main.send_notification', - service_id=service_one['id'], + service_id=SERVICE_ONE_ID, template_id=fake_uuid ) mock_send_notification.assert_called_once_with( - service_one['id'], + SERVICE_ONE_ID, template_id=fake_uuid, - recipient='07700900001', - personalisation={'a': 'b'}, + recipient=recipient, + personalisation=expected_personalisation, sender_id=None )