Show recipient when about to send uploaded letter

The recipient of the letter now displays at the bottom of the page when
previewing a valid letter. The template preview `/precompiled/sanitise`
endpoint returns the address, but we format it to display on a single
line with commas between each line. We also need to convert the
recipient address to ASCII so that it can be stored as S3 metadata.
This commit is contained in:
Katie Smith
2019-11-08 09:56:59 +00:00
parent 21a59598b2
commit a542047581
5 changed files with 75 additions and 10 deletions

View File

@@ -3,13 +3,24 @@ import json
from boto3 import resource
from flask import current_app
from notifications_utils.s3 import s3upload as utils_s3upload
from notifications_utils.sanitise_text import SanitiseASCII
def get_transient_letter_file_location(service_id, upload_id):
return 'service-{}/{}.pdf'.format(service_id, upload_id)
def upload_letter_to_s3(data, *, file_location, status, page_count, filename, message=None, invalid_pages=None):
def upload_letter_to_s3(
data,
*,
file_location,
status,
page_count,
filename,
message=None,
invalid_pages=None,
recipient=None
):
metadata = {
'status': status,
'page_count': str(page_count),
@@ -19,6 +30,8 @@ def upload_letter_to_s3(data, *, file_location, status, page_count, filename, me
metadata['message'] = message
if invalid_pages:
metadata['invalid_pages'] = json.dumps(invalid_pages)
if recipient:
metadata['recipient'] = format_recipient(recipient)
utils_s3upload(
filedata=data,
@@ -46,3 +59,20 @@ def get_letter_metadata(service_id, file_id):
s3_object = s3.Object(current_app.config['TRANSIENT_UPLOADED_LETTERS'], file_location).get()
return s3_object['Metadata']
def format_recipient(address):
'''
To format the recipient we need to:
- remove new line characters
- remove whitespace around the lines
- join the address lines, separated by a comma
- convert the string to ASCII (S3 metadata must be stored as ASCII)
'''
stripped_address_lines_no_trailing_commas = [
line.lstrip().rstrip(' ,')
for line in address.splitlines() if line
]
one_line_address = ', '.join(stripped_address_lines_no_trailing_commas)
return SanitiseASCII.encode(one_line_address)