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

@@ -27,7 +27,7 @@ from app import db, create_uuid
from app.aws.s3 import remove_s3_object, get_s3_bucket_objects
from app.dao.dao_utils import transactional
from app.errors import InvalidRequest
from app.letters.utils import LETTERS_PDF_FILE_LOCATION_STRUCTURE
from app.letters.utils import get_letter_pdf_filename
from app.models import (
Notification,
NotificationHistory,
@@ -402,17 +402,13 @@ def _delete_letters_from_s3(
).limit(query_limit).all()
for letter in letters_to_delete_from_s3:
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
# I don't think we need this anymore, we should update the query to get letters sent 7 days ago
if letter.sent_at:
sent_at = str(letter.sent_at.date())
prefix = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
folder=sent_at + "/",
reference=letter.reference,
duplex="D",
letter_class="2",
colour="C",
crown="C" if letter.service.crown else "N",
date=''
).upper()[:-5]
prefix = get_letter_pdf_filename(reference=letter.reference,
crown=letter.service.crown,
sending_date=letter.created_at,
is_scan_letter=letter.key_type == KEY_TYPE_TEST,
postage=letter.postage)
s3_objects = get_s3_bucket_objects(bucket_name=bucket_name, subfolder=prefix)
for s3_object in s3_objects:
try:

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
)

View File

@@ -178,8 +178,9 @@ def send_pdf_letter_notification(service_id, post_data):
)
upload_filename = get_letter_pdf_filename(
notification.reference,
notification.service.crown,
reference=notification.reference,
crown=notification.service.crown,
sending_date=notification.created_at,
is_scan_letter=False,
postage=notification.postage
)