From 827c6ccc99a3908bae9e9744b2f591f25b8f93f6 Mon Sep 17 00:00:00 2001 From: Ken Tsang Date: Tue, 20 Jun 2017 13:24:01 +0100 Subject: [PATCH] Refactor inbound api html and processing --- app/main/views/service_settings.py | 55 ++++++++++++++++++----- app/templates/views/service-settings.html | 17 ++++--- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/app/main/views/service_settings.py b/app/main/views/service_settings.py index c14ddfb10..37034f93f 100644 --- a/app/main/views/service_settings.py +++ b/app/main/views/service_settings.py @@ -1,3 +1,5 @@ +from urllib.parse import urlparse + import requests from flask import ( render_template, @@ -34,6 +36,14 @@ from app.main.forms import ( from app import user_api_client, current_service, organisations_client +def get_inbound_api(): + if current_service['inbound_api']: + return service_api_client.get_service_inbound_api( + current_service['id'], + current_service.get('inbound_api')[0] + ) + + @main.route("/services//service-settings") @login_required @user_has_permissions('manage_settings', admin_override=True) @@ -43,6 +53,15 @@ def service_settings(service_id): organisation = organisations_client.get_organisation(current_service['organisation'])['organisation'] else: organisation = None + + inbound_api = get_inbound_api() + if inbound_api: + parsed_url = urlparse(inbound_api.get('url')) if inbound_api else '' + inbound_api_url = '{uri.scheme}://{uri.netloc}{elide_token}'.format( + uri=parsed_url, elide_token='...' if parsed_url.path else '') + else: + inbound_api_url = '' + return render_template( 'views/service-settings.html', organisation=organisation, @@ -50,6 +69,7 @@ def service_settings(service_id): current_service.get('dvla_organisation', '001') ), can_receive_inbound=('inbound_sms' in current_service['permissions']), + inbound_api_url=inbound_api_url, letter_contact_block=Field(current_service['letter_contact_block'], html='escape') ) @@ -268,19 +288,25 @@ def service_set_reply_to_email(service_id): @user_has_permissions('manage_settings', admin_override=True) def service_set_sms_sender(service_id): form = ServiceSmsSender() + + def update_service(permissions, sms_sender): + service_api_client.update_service_with_properties( + current_service['id'], + {'permissions': permissions, 'sms_sender': sms_sender} + ) + + set_inbound_sms = request.args.get('set_inbound_sms') + if set_inbound_sms == 'True': + if 'inbound_sms' in current_service['permissions']: + current_service['permissions'].remove('inbound_sms') + update_service(current_service['permissions'], current_service['sms_sender']) + return redirect(url_for('.service_settings', service_id=service_id)) + if form.validate_on_submit(): - set_inbound_sms = request.args.get('set_inbound_sms', False) if set_inbound_sms == 'True': permissions = current_service['permissions'] - if 'inbound_sms' in permissions: - permissions.remove('inbound_sms') - else: - permissions.append('inbound_sms') - service_api_client.update_service_with_properties( - current_service['id'], - {'permissions': permissions, - 'sms_sender': form.sms_sender.data or None} - ) + permissions.append('inbound_sms') + update_service(permissions, form.sms_sender.data) else: service_api_client.update_service( current_service['id'], @@ -416,14 +442,19 @@ def get_branding_as_dict(organisations): @login_required @user_has_permissions('manage_settings', admin_override=True) def service_set_inbound_api(service_id): - form = ServiceInboundApiForm() + if 'inbound_sms' not in current_service['permissions']: + abort(403) + + inbound_api = get_inbound_api() + form = ServiceInboundApiForm(url=inbound_api.get('url') if inbound_api else '') 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 + user_id=current_user.id, + inbound_api_id=inbound_api.get('id') if inbound_api else '' ) return redirect(url_for('.service_settings', service_id=service_id)) diff --git a/app/templates/views/service-settings.html b/app/templates/views/service-settings.html index 0eb557a0e..762126ee0 100644 --- a/app/templates/views/service-settings.html +++ b/app/templates/views/service-settings.html @@ -57,6 +57,17 @@ {{ edit_field('Change', url_for('.service_set_inbound_sms', service_id=current_service.id)) }} {% endcall %} + {% if 'inbound_sms' in current_service.permissions %} + {% call row() %} + {{ text_field('API endpoint for received text messages') }} + {{ text_field( + 'None' if not inbound_api_url else inbound_api_url, + status='' if inbound_api_url else 'default' + ) }} + {{ edit_field('Change', url_for('.service_set_inbound_api', service_id=current_service.id)) }} + {% endcall %} + {% endif %} + {% call row() %} {{ text_field('Letters') }} {{ boolean_field(current_service.can_send_letters) }} @@ -73,12 +84,6 @@ {% 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 %}