Use json schema to validate post request to send PDF letter

This approach was suggested as the way to do validation things on
Notify.
This commit is contained in:
David McDonald
2019-10-25 11:27:28 +01:00
parent 7f708153d2
commit d1195dfd5a
5 changed files with 35 additions and 28 deletions

View File

@@ -93,6 +93,7 @@ from app.models import (
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
from app.schema_validation import validate
from app.service import statistics
from app.service.send_pdf_letter_schema import send_pdf_letter_request
from app.service.service_data_retention_schema import (
add_service_data_retention_request,
update_service_data_retention_request
@@ -650,7 +651,8 @@ def create_one_off_notification(service_id):
@service_blueprint.route('/<uuid:service_id>/send-pdf-letter', methods=['POST'])
def create_pdf_letter(service_id):
resp = send_pdf_letter_notification(service_id, request.get_json())
data = validate(request.get_json(), send_pdf_letter_request)
resp = send_pdf_letter_notification(service_id, data)
return jsonify(resp), 201

View File

@@ -23,7 +23,6 @@ from app.models import (
SMS_TYPE,
EMAIL_TYPE,
LETTER_TYPE,
POSTAGE_TYPES,
NOTIFICATION_DELIVERED,
UPLOAD_LETTERS,
)
@@ -149,11 +148,6 @@ def send_pdf_letter_notification(service_id, post_data):
allow_whitelisted_recipients=False,
)
postage = post_data.get('postage')
if postage not in POSTAGE_TYPES:
message = "postage must be set as 'first' or 'second'"
raise BadRequestError(message=message)
template = get_precompiled_letter_template(service.id)
file_location = 'service-{}/{}.pdf'.format(service.id, post_data['file_id'])
@@ -188,7 +182,7 @@ def send_pdf_letter_notification(service_id, post_data):
client_reference=post_data['filename'],
created_by_id=post_data['created_by'],
billable_units=billable_units,
postage=postage,
postage=post_data['postage'],
)
upload_filename = get_letter_pdf_filename(

View File

@@ -0,0 +1,14 @@
send_pdf_letter_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST send uploaded pdf letter",
"type": "object",
"title": "Send an uploaded pdf letter",
"properties": {
"postage": {"enum": ["first", "second"]},
"filename": {"type": "string"},
"created_by": {"type": "string"},
"file_id": {"type": "string"},
},
"required": ["postage", "filename", "created_by", "file_id"]
}