From 08a701b70cca75f3e4f60a6f5cd60551f5b2be0e Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 20 May 2020 10:17:48 +0100 Subject: [PATCH] Refactor to get metadata before validating form Because we no longer need the form to get the `file_id`, we can get the metadata before building the form. This will, in subsequent commits, let us build the form differently based on the recipient metadata. Also removed some variables that were assigned to then only used once and reformatted arguments for readability. --- app/main/views/uploads.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app/main/views/uploads.py b/app/main/views/uploads.py index af375dc8f..8a4a25034 100644 --- a/app/main/views/uploads.py +++ b/app/main/views/uploads.py @@ -344,20 +344,25 @@ def send_uploaded_letter(service_id, file_id=None): if not (current_service.has_permission('letter') and current_service.has_permission('upload_letters')): abort(403) + metadata = get_letter_metadata(service_id, file_id) + + if metadata.get('status') != 'valid': + abort(403) + + recipient_address = metadata.get('recipient') + form = LetterUploadPostageForm() if not form.validate_on_submit(): return uploaded_letter_preview(service_id, file_id) - postage = form.postage.data - metadata = get_letter_metadata(service_id, file_id) - filename = metadata.get('filename') - recipient_address = metadata.get('recipient') - - if metadata.get('status') != 'valid': - abort(403) - - notification_api_client.send_precompiled_letter(service_id, filename, file_id, postage, recipient_address) + notification_api_client.send_precompiled_letter( + service_id, + metadata.get('filename'), + file_id, + form.postage.data, + recipient_address, + ) return redirect(url_for( '.view_notification',