Move recipient decoding into LetterMetadata class

This is for consistency with how we do it for filenames in the previous
commit and moves the decoding into the `LetterMetadata` class for
abstracting this behaviour.

Small refactor of the LetterMetadata class needed to handle None case as
recipient can be None.
This commit is contained in:
David McDonald
2020-05-18 15:44:20 +01:00
parent 7923760345
commit 387fcfda3f
3 changed files with 13 additions and 13 deletions

View File

@@ -1,7 +1,6 @@
import base64
import itertools
import json
import urllib
import uuid
from io import BytesIO
from zipfile import BadZipFile
@@ -192,14 +191,12 @@ def _get_error_from_upload_form(form_errors):
def format_recipient(address):
'''
To format the recipient we need to:
- decode, address is url encoded
- remove new line characters
- remove whitespace around the lines
- join the address lines, separated by a comma
'''
if not address:
return address
address = urllib.parse.unquote(address)
stripped_address_lines_no_trailing_commas = [
line.lstrip().rstrip(' ,')
for line in address.splitlines() if line

View File

@@ -45,15 +45,16 @@ def upload_letter_to_s3(
class LetterMetadata:
KEYS_TO_DECODE = ["filename"]
KEYS_TO_DECODE = ["filename", "recipient"]
def __init__(self, metadata):
self._metadata = metadata
def get(self, key, default=None):
if key in self.KEYS_TO_DECODE:
return urllib.parse.unquote(self._metadata.get(key, default))
return self._metadata.get(key, default)
value = self._metadata.get(key, default)
if value and key in self.KEYS_TO_DECODE:
value = urllib.parse.unquote(value)
return value
def get_letter_pdf_and_metadata(service_id, file_id):