From 8e7b0edc4d0816c9ad3de59a69c2cb1ea81c3e38 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 15 Mar 2016 06:53:06 +0000 Subject: [PATCH] Add page explaining how to send template using API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Would like to test something like this and see how well it works. Intention of having this page is so: - template IDs are discoverable (https://www.pivotaltracker.com/story/show/115404593) - it’s obvious there’s an ‘automated’ way to send messages, as well as the CSV way (we’ve seen people oblivious to this in research) --- app/assets/stylesheets/app.scss | 1 + .../stylesheets/components/sms-message.scss | 6 -- app/main/views/send.py | 24 +++++++ app/templates/views/choose-template.html | 25 ++++++-- app/templates/views/documentation.html | 2 +- app/templates/views/send-from-api.html | 62 +++++++++++++++++++ app/templates/views/send.html | 2 +- tests/app/main/views/test_send.py | 21 +++++++ 8 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 app/templates/views/send-from-api.html diff --git a/app/assets/stylesheets/app.scss b/app/assets/stylesheets/app.scss index 57b5db45a..d7eeacb62 100644 --- a/app/assets/stylesheets/app.scss +++ b/app/assets/stylesheets/app.scss @@ -65,6 +65,7 @@ a { .highlight { font-family: monospace; overflow-x: scroll; + padding: 10px 0 10px 10px; } .inline { diff --git a/app/assets/stylesheets/components/sms-message.scss b/app/assets/stylesheets/components/sms-message.scss index cb26b296f..d86d6a48d 100644 --- a/app/assets/stylesheets/components/sms-message.scss +++ b/app/assets/stylesheets/components/sms-message.scss @@ -43,14 +43,8 @@ margin-top: 52px; a { - display: block; margin-bottom: 5px; - - &:first-child { - @include bold-19; - } - } } diff --git a/app/main/views/send.py b/app/main/views/send.py index 72ee60a8c..e95de47a2 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -1,5 +1,6 @@ import csv import io +import json import uuid from contextlib import suppress @@ -197,6 +198,29 @@ def send_message_to_self(service_id, template_id): upload_id=upload_id)) +@main.route("/services//send//from-api", methods=['GET']) +@login_required +@user_has_permissions('manage_api_keys', 'access_developer_docs') +def send_from_api(service_id, template_id): + template = Template( + templates_dao.get_service_template_or_404(service_id, template_id)['data'] + ) + payload = { + "to": current_user.mobile_number, + "template": template.id, + "personalisation": { + placeholder: "{} 1".format(placeholder) for placeholder in template.placeholders + } + } + return render_template( + 'views/send-from-api.html', + template=template, + payload=json.dumps(payload, indent=4), + api_host=current_app.config['API_HOST_NAME'], + service_id=service_id + ) + + @main.route("/services//check/", methods=['GET']) @login_required @user_has_permissions('send_texts', 'send_emails', 'send_letters') diff --git a/app/templates/views/choose-template.html b/app/templates/views/choose-template.html index e3d11ed02..cc6781caa 100644 --- a/app/templates/views/choose-template.html +++ b/app/templates/views/choose-template.html @@ -49,20 +49,33 @@ {{ email_message( template.subject, template.formatted_as_markup, - name=template.name + name=template.name, + edit_link=( + url_for(".edit_service_template", service_id=service_id, template_id=template.id) + if current_user.has_permissions(['manage_templates']) else + None + ) ) }} {% elif 'sms' == template_type %} - {{ sms_message(template.formatted_as_markup, name=template.name) }} + {{ sms_message( + template.formatted_as_markup, + name=template.name, + edit_link=( + url_for(".edit_service_template", service_id=service_id, template_id=template.id) + if current_user.has_permissions(['manage_templates']) else + None + ) + ) }} {% endif %}
diff --git a/app/templates/views/documentation.html b/app/templates/views/documentation.html index 3512ae423..073d94e33 100644 --- a/app/templates/views/documentation.html +++ b/app/templates/views/documentation.html @@ -9,7 +9,7 @@ {% block maincolumn_content %}

- Developer documentation + Set up API integration

diff --git a/app/templates/views/send-from-api.html b/app/templates/views/send-from-api.html new file mode 100644 index 000000000..213490ac2 --- /dev/null +++ b/app/templates/views/send-from-api.html @@ -0,0 +1,62 @@ +{% extends "withnav_template.html" %} +{% from "components/email-message.html" import email_message %} +{% from "components/sms-message.html" import sms_message %} + +{% block page_title %} + Developer documentation – GOV.UK Notify +{% endblock %} + +{% block maincolumn_content %} + +

+ API integration +

+ +
+
+ + {% if 'email' == template.template_type %} + {{ email_message( + template.subject, + template.formatted_as_markup, + ) }} + {% elif 'sms' == template.template_type %} + {{ sms_message( + template.formatted_as_markup, + ) }} + {% endif %} + +

+ You can send this message from your existing apps or systems by + connecting them to GOV.UK Notify. +

+

+ You’ll need to work with a developer to do this. +

+ +
+
+ +

+ Example payload for this template +

+ {{payload|syntax_highlight_json}} + +

+ Endpoint +

+ +
+
+      {{- 'POST {}/notifications/{}'.format(
+        api_host,
+        template.template_type
+      ) -}}
+    
+
+ +

+
See the developer documentation for full details. +

+ +{% endblock %} diff --git a/app/templates/views/send.html b/app/templates/views/send.html index 2d62b9f2a..6bfdfd596 100644 --- a/app/templates/views/send.html +++ b/app/templates/views/send.html @@ -11,7 +11,7 @@ {% block maincolumn_content %} -

Add recipients

+

Send a batch

{% if 'sms' == template.template_type %}
diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index 44fc9a1ee..8cd8a9ca5 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -90,6 +90,27 @@ def test_send_test_message_to_self( mock_s3_upload.assert_called_with(ANY, '12345', expected_data, 'eu-west-1') +def test_send_test_message_from_api_page( + app_, + mocker, + api_user_active, + mock_login, + mock_get_service, + mock_get_service_email_template, + mock_s3_upload, + mock_has_permissions +): + with app_.test_request_context(): + with app_.test_client() as client: + client.login(api_user_active) + response = client.get( + url_for('main.send_from_api', service_id=12345, template_id=54321), + follow_redirects=True + ) + assert response.status_code == 200 + assert 'API integration' in response.get_data(as_text=True) + + def test_download_example_csv( app_, mocker,