Add URL converters for template and file types

Sometimes we manually check that a URL parameter is in a required set.
Sometimes we don’t bother.

This commit adds a URL converter to do this so that:
- we don’t have to re-write the same code every time
- it’s easier to apply this check to other endpoints

This means endpoints that previously allowed a `template_type` or
`message_type` of `None` now 404. So I’ve had to add new routes for
with URLs that don’t include such parameters.

So this…:
```
/services/128b91b6-2996-4107-bb65-51b7c24a728d/notifications/sms.csv
/services/128b91b6-2996-4107-bb65-51b7c24a728d/notifications/None.csv
```

…becomes:
```
/services/128b91b6-2996-4107-bb65-51b7c24a728d/notifications/sms.csv
/services/128b91b6-2996-4107-bb65-51b7c24a728d/notifications.csv
```

This matches what we do for the HTML-responding equivalent (see
265931d217/app/main/views/jobs.py (L215-L216))
This commit is contained in:
Chris Hill-Scott
2019-11-04 11:20:08 +00:00
parent ef335e7601
commit 545b485d86
5 changed files with 28 additions and 19 deletions

View File

@@ -213,7 +213,7 @@ def view_job_updates(service_id, job_id):
@main.route('/services/<uuid:service_id>/notifications', methods=['GET', 'POST'])
@main.route('/services/<uuid:service_id>/notifications/<message_type>', methods=['GET', 'POST'])
@main.route('/services/<uuid:service_id>/notifications/<template_type:message_type>', methods=['GET', 'POST'])
@user_has_permissions()
def view_notifications(service_id, message_type=None):
return render_template(
@@ -237,7 +237,7 @@ def view_notifications(service_id, message_type=None):
@main.route('/services/<uuid:service_id>/notifications.json', methods=['GET', 'POST'])
@main.route('/services/<uuid:service_id>/notifications/<message_type>.json', methods=['GET', 'POST'])
@main.route('/services/<uuid:service_id>/notifications/<template_type:message_type>.json', methods=['GET', 'POST'])
@user_has_permissions()
def get_notifications_as_json(service_id, message_type=None):
return jsonify(get_notifications(
@@ -245,15 +245,14 @@ def get_notifications_as_json(service_id, message_type=None):
))
@main.route('/services/<uuid:service_id>/notifications/<message_type>.csv', endpoint="view_notifications_csv")
@main.route('/services/<uuid:service_id>/notifications.csv', endpoint="view_notifications_csv")
@main.route('/services/<uuid:service_id>/notifications/<template_type:message_type>.csv', endpoint="view_notifications_csv")
@user_has_permissions()
def get_notifications(service_id, message_type, status_override=None):
# TODO get the api to return count of pages as well.
page = get_page_from_request()
if page is None:
abort(404, "Invalid page argument ({}).".format(request.args.get('page')))
if message_type not in ['email', 'sms', 'letter', None]:
abort(404)
filter_args = parse_filter_args(request.args)
filter_args['status'] = set_status_filters(filter_args)
service_data_retention_days = None

View File

@@ -8,7 +8,6 @@ from datetime import datetime
from dateutil import parser
from flask import (
Response,
abort,
flash,
jsonify,
redirect,
@@ -180,15 +179,11 @@ def get_preview_error_image():
return file.read()
@main.route("/services/<uuid:service_id>/notification/<uuid:notification_id>.<filetype>")
@main.route("/services/<uuid:service_id>/notification/<uuid:notification_id>.<letter_file_extension:filetype>")
@user_has_permissions('view_activity')
def view_letter_notification_as_preview(
service_id, notification_id, filetype, with_metadata=False
):
if filetype not in ('pdf', 'png'):
abort(404)
try:
preview = notification_api_client.get_notification_letter_preview(
service_id,

View File

@@ -106,8 +106,8 @@ def start_tour(service_id, template_id):
@main.route("/services/<uuid:service_id>/templates", methods=['GET', 'POST'])
@main.route("/services/<uuid:service_id>/templates/folders/<uuid:template_folder_id>", methods=['GET', 'POST'])
@main.route("/services/<uuid:service_id>/templates/<template_type>", methods=['GET', 'POST'])
@main.route("/services/<uuid:service_id>/templates/<template_type>/folders/<uuid:template_folder_id>", methods=['GET', 'POST'])
@main.route("/services/<uuid:service_id>/templates/<template_type:template_type>", methods=['GET', 'POST'])
@main.route("/services/<uuid:service_id>/templates/<template_type:template_type>/folders/<uuid:template_folder_id>", methods=['GET', 'POST'])
@user_has_permissions()
def choose_template(service_id, template_type='all', template_folder_id=None):
template_folder = current_service.get_template_folder(template_folder_id)
@@ -406,11 +406,11 @@ def _get_template_copy_name(template, existing_templates):
@main.route((
'/services/<uuid:service_id>/templates/action-blocked/'
'<notification_type>'
'<template_type:notification_type>'
))
@main.route((
'/services/<uuid:service_id>/templates/action-blocked/'
'<notification_type>/<return_to>/<uuid:template_id>'
'<template_type:notification_type>/<return_to>/<uuid:template_id>'
))
@user_has_permissions('manage_templates')
def action_blocked(service_id, notification_type, return_to='add_new_template', template_id=None):
@@ -505,18 +505,16 @@ def delete_template_folder(service_id, template_folder_id):
@main.route(
"/services/<uuid:service_id>/templates/add-<template_type>",
"/services/<uuid:service_id>/templates/add-<template_type:template_type>",
methods=['GET', 'POST'],
)
@main.route(
"/services/<uuid:service_id>/templates/folders/<uuid:template_folder_id>/add-<template_type>",
"/services/<uuid:service_id>/templates/folders/<uuid:template_folder_id>/add-<template_type:template_type>",
methods=['GET', 'POST'],
)
@user_has_permissions('manage_templates')
def add_service_template(service_id, template_type, template_folder_id=None):
if template_type not in ['sms', 'email', 'letter']:
abort(404)
if not current_service.has_permission('letter') and template_type == 'letter':
abort(403)