Refactor inbound api html and processing

This commit is contained in:
Ken Tsang
2017-06-20 13:24:01 +01:00
parent 5d5bb69f0e
commit 827c6ccc99
2 changed files with 54 additions and 18 deletions

View File

@@ -1,3 +1,5 @@
from urllib.parse import urlparse
import requests import requests
from flask import ( from flask import (
render_template, render_template,
@@ -34,6 +36,14 @@ from app.main.forms import (
from app import user_api_client, current_service, organisations_client 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_id>/service-settings") @main.route("/services/<service_id>/service-settings")
@login_required @login_required
@user_has_permissions('manage_settings', admin_override=True) @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'] organisation = organisations_client.get_organisation(current_service['organisation'])['organisation']
else: else:
organisation = None 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( return render_template(
'views/service-settings.html', 'views/service-settings.html',
organisation=organisation, organisation=organisation,
@@ -50,6 +69,7 @@ def service_settings(service_id):
current_service.get('dvla_organisation', '001') current_service.get('dvla_organisation', '001')
), ),
can_receive_inbound=('inbound_sms' in current_service['permissions']), 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') 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) @user_has_permissions('manage_settings', admin_override=True)
def service_set_sms_sender(service_id): def service_set_sms_sender(service_id):
form = ServiceSmsSender() 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(): if form.validate_on_submit():
set_inbound_sms = request.args.get('set_inbound_sms', False)
if set_inbound_sms == 'True': if set_inbound_sms == 'True':
permissions = current_service['permissions'] permissions = current_service['permissions']
if 'inbound_sms' in permissions: permissions.append('inbound_sms')
permissions.remove('inbound_sms') update_service(permissions, form.sms_sender.data)
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}
)
else: else:
service_api_client.update_service( service_api_client.update_service(
current_service['id'], current_service['id'],
@@ -416,14 +442,19 @@ def get_branding_as_dict(organisations):
@login_required @login_required
@user_has_permissions('manage_settings', admin_override=True) @user_has_permissions('manage_settings', admin_override=True)
def service_set_inbound_api(service_id): 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(): if form.validate_on_submit():
service_api_client.update_service_inbound_api( service_api_client.update_service_inbound_api(
service_id, service_id,
url=form.url.data, url=form.url.data,
bearer_token=form.bearer_token.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)) return redirect(url_for('.service_settings', service_id=service_id))

View File

@@ -57,6 +57,17 @@
{{ edit_field('Change', url_for('.service_set_inbound_sms', service_id=current_service.id)) }} {{ edit_field('Change', url_for('.service_set_inbound_sms', service_id=current_service.id)) }}
{% endcall %} {% 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() %} {% call row() %}
{{ text_field('Letters') }} {{ text_field('Letters') }}
{{ boolean_field(current_service.can_send_letters) }} {{ boolean_field(current_service.can_send_letters) }}
@@ -73,12 +84,6 @@
{% endcall %} {% endcall %}
{% endif %} {% 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 %} {% endcall %}
</div> </div>