Refactor the code that figures out what folder and filename to use for the letter pdf files.

Now we consistently use the created_at date, so we can always get the right file location and name.

The previous updates to this code were trying to solve the problem if a pdf being created at 17:29, but not ready to upload until 17:31 after the antivirus and validation check.
But in those cases we would have trouble finding the file.
This commit is contained in:
Rebecca Law
2019-09-16 14:20:40 +01:00
committed by Leo Hemsted
parent a10aaddbcc
commit 702d8fa85f
5 changed files with 45 additions and 42 deletions

View File

@@ -36,19 +36,16 @@ def get_folder_name(_now, is_test_or_scan_letter=False):
return folder_name
def get_letter_pdf_filename(reference, crown, is_scan_letter=False, postage=SECOND_CLASS):
now = datetime.utcnow()
def get_letter_pdf_filename(reference, crown, sending_date, is_scan_letter=False, postage=SECOND_CLASS):
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
folder=get_folder_name(now, is_scan_letter),
folder=get_folder_name(sending_date, is_scan_letter),
reference=reference,
duplex="D",
letter_class=RESOLVE_POSTAGE_FOR_FILE_NAME[postage],
colour="C",
crown="C" if crown else "N",
date=now.strftime('%Y%m%d%H%M%S')
date=sending_date.strftime('%Y%m%d%H%M%S')
).upper()
return upload_file_name
@@ -60,12 +57,7 @@ def get_bucket_name_and_prefix_for_notification(notification):
bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME']
else:
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
if notification.sent_at:
folder = "{}/".format(notification.sent_at.date())
elif notification.updated_at:
folder = get_folder_name(notification.updated_at, False)
else:
folder = get_folder_name(notification.created_at, False)
folder = get_folder_name(notification.created_at, False)
upload_file_name = PRECOMPILED_BUCKET_PREFIX.format(
folder=folder,
@@ -86,8 +78,9 @@ def upload_letter_pdf(notification, pdf_data, precompiled=False):
notification.id, notification.reference, notification.created_at, len(pdf_data)))
upload_file_name = get_letter_pdf_filename(
notification.reference,
notification.service.crown,
reference=notification.reference,
crown=notification.service.crown,
sending_date=notification.created_at,
is_scan_letter=precompiled or notification.key_type == KEY_TYPE_TEST,
postage=notification.postage
)