From 703b48b157431c74f8d8f4b091c28f0b74534c8d Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 15 Jun 2017 16:20:07 +0100 Subject: [PATCH] [WIP] Page and form to persist the inbound api data for a service. --- app/main/forms.py | 11 ++++++ app/main/views/service_settings.py | 24 ++++++++++++- app/notify_client/service_api_client.py | 8 +++++ app/templates/views/service-settings.html | 7 ++++ .../service-settings/set-inbound-api.html | 35 +++++++++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 app/templates/views/service-settings/set-inbound-api.html diff --git a/app/main/forms.py b/app/main/forms.py index f8db6e577..e233b37a5 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -651,6 +651,17 @@ class PlaceholderForm(Form): pass +class ServiceInboundApiForm(Form): + url = StringField("Inbound sms url", + validators=[DataRequired(message='Can’t be empty'), + Regexp(regex="^https.*", + message='Must be a valid https url')] + ) + bearer_token = StringField("Bearer token", + validators=[DataRequired(message='Can’t be empty'), + Length(min=5, message='Must be at least 10 characters')]) + + def get_placeholder_form_instance( placeholder_name, dict_to_populate_from, diff --git a/app/main/views/service_settings.py b/app/main/views/service_settings.py index d1667bfa7..7f3762a8c 100644 --- a/app/main/views/service_settings.py +++ b/app/main/views/service_settings.py @@ -30,7 +30,7 @@ from app.main.forms import ( ServiceLetterContactBlock, ServiceBrandingOrg, LetterBranding, -) + ServiceInboundApiForm) from app import user_api_client, current_service, organisations_client @@ -38,6 +38,7 @@ from app import user_api_client, current_service, organisations_client @login_required @user_has_permissions('manage_settings', admin_override=True) def service_settings(service_id): + print(current_service) letter_branding_organisations = organisations_client.get_letter_organisations() if current_service['organisation']: organisation = organisations_client.get_organisation(current_service['organisation'])['organisation'] @@ -410,3 +411,24 @@ def get_branding_as_dict(organisations): 'colour': organisation['colour'] } for organisation in organisations } + + +@main.route("/services//service-settings/set-inbound-api", methods=['GET', 'POST']) +@login_required +@user_has_permissions('manage_settings', admin_override=True) +def service_set_inbound_api(service_id): + form = ServiceInboundApiForm() + + if form.validate_on_submit(): + service_api_client.update_service_inbound_api( + service_id, + url=form.url.data, + bearer_token=form.bearer_token.data, + user_id=current_user.id + ) + return redirect(url_for('.service_settings', service_id=service_id)) + + return render_template( + 'views/service-settings/set-inbound-api.html', + form=form, + ) diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index bbc4fdf81..6c30c3e8a 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -266,6 +266,14 @@ class ServiceAPIClient(NotifyAdminAPIClient): '/service/{}/inbound-sms/summary'.format(service_id) ) + def update_service_inbound_api(self, service_id, url, bearer_token, user_id): + data = { + "url":url, + "bearer_token": bearer_token, + "updated_by_id": user_id + } + return self.post("/service/{}/inbound-api".format(service_id), data) + class ServicesBrowsableItem(BrowsableItem): @property diff --git a/app/templates/views/service-settings.html b/app/templates/views/service-settings.html index 60fcf1f3e..0eb557a0e 100644 --- a/app/templates/views/service-settings.html +++ b/app/templates/views/service-settings.html @@ -72,6 +72,13 @@ {{ edit_field('Change', url_for('.service_set_letter_contact_block', service_id=current_service.id)) }} {% endcall %} {% endif %} + + {% call row() %} + {{ text_field('Inbound API') }} + {{ boolean_field(current_service.can_send_letters) }} + {{ edit_field('Change', url_for('.service_set_inbound_api', service_id=current_service.id)) }} + {% endcall %} + {% endcall %} diff --git a/app/templates/views/service-settings/set-inbound-api.html b/app/templates/views/service-settings/set-inbound-api.html new file mode 100644 index 000000000..e858e20a2 --- /dev/null +++ b/app/templates/views/service-settings/set-inbound-api.html @@ -0,0 +1,35 @@ +{% extends "withnav_template.html" %} +{% from "components/textbox.html" import textbox %} +{% from "components/page-footer.html" import page_footer %} + +{% block service_page_title %} + Inbound api +{% endblock %} + +{% block maincolumn_content %} + +

Inbound API

+

+ This is the https url that the inbound SMS messages will be posted to + and the bearer token used in the authorisation header of the request. +

+ +
+ {{ textbox( + form.url, + width='1-4', + hint='Valid https url' + ) }} + {{ textbox( + form.bearer_token, + width='1-4', + hint='At least 10 characters' + ) }} + {{ page_footer( + 'Save', + back_link=url_for('.service_settings', service_id=current_service.id), + back_link_text='Back to settings' + ) }} +
+ +{% endblock %} \ No newline at end of file