Refactor dates being passed around

I believe this way is nicer to read, we don't have to change between
datetimes and strings and back.
This commit is contained in:
David McDonald
2020-02-19 14:23:33 +00:00
parent 6226d9e122
commit 148a5ab456
3 changed files with 16 additions and 18 deletions

View File

@@ -128,15 +128,17 @@ def collate_letter_pdfs_for_day():
that have not yet been sent. that have not yet been sent.
If run after midnight, it will collect up letters created before 5:30pm the day before. If run after midnight, it will collect up letters created before 5:30pm the day before.
""" """
date = convert_utc_to_bst(datetime.utcnow()) print_run_date = convert_utc_to_bst(datetime.utcnow())
if date.time() < LETTER_PROCESSING_DEADLINE: if print_run_date.time() < LETTER_PROCESSING_DEADLINE:
date = date - timedelta(days=1) print_run_date = print_run_date - timedelta(days=1)
# Using the truncated date is ok because UTC to BST does not make a difference to the date, # We can truncate the datetime to a date and add our own time (5:30pm) because UTC to BST does not
# since it is triggered mid afternoon. # make a difference to the date since it is triggered mid afternoon.
print_run_date = date.strftime("%Y-%m-%d") print_run_deadline = print_run_date.replace(
hour=17, minute=30, second=0, microsecond=0
)
letters_to_print = get_key_and_size_of_letters_to_be_sent_to_print(print_run_date) letters_to_print = get_key_and_size_of_letters_to_be_sent_to_print(print_run_deadline)
for i, letters in enumerate(group_letters(letters_to_print)): for i, letters in enumerate(group_letters(letters_to_print)):
filenames = [letter['Key'] for letter in letters] filenames = [letter['Key'] for letter in letters]
@@ -144,7 +146,7 @@ def collate_letter_pdfs_for_day():
hash = urlsafe_b64encode(sha512(''.join(filenames).encode()).digest())[:20].decode() hash = urlsafe_b64encode(sha512(''.join(filenames).encode()).digest())[:20].decode()
# eg NOTIFY.2018-12-31.001.Wjrui5nAvObjPd-3GEL-.ZIP # eg NOTIFY.2018-12-31.001.Wjrui5nAvObjPd-3GEL-.ZIP
dvla_filename = 'NOTIFY.{date}.{num:03}.{hash}.ZIP'.format( dvla_filename = 'NOTIFY.{date}.{num:03}.{hash}.ZIP'.format(
date=print_run_date, date=print_run_deadline.strftime("%Y-%m-%d"),
num=i + 1, num=i + 1,
hash=hash hash=hash
) )
@@ -167,8 +169,8 @@ def collate_letter_pdfs_for_day():
) )
def get_key_and_size_of_letters_to_be_sent_to_print(print_run_date): def get_key_and_size_of_letters_to_be_sent_to_print(print_run_deadline):
letters_awaiting_sending = dao_get_letters_to_be_printed(print_run_date) letters_awaiting_sending = dao_get_letters_to_be_printed(print_run_deadline)
letter_pdfs = [] letter_pdfs = []
for letter in letters_awaiting_sending: for letter in letters_awaiting_sending:

View File

@@ -731,16 +731,12 @@ def notifications_not_yet_sent(should_be_sending_after_seconds, notification_typ
return notifications return notifications
def dao_get_letters_to_be_printed(print_run_date): def dao_get_letters_to_be_printed(print_run_deadline):
""" """
Given a date for a print run, Return all letters created before 5:30pm that day that have not yet been sent Return all letters created before the print run deadline that have not yet been sent
""" """
last_processing_deadline = datetime.strptime(print_run_date, "%Y-%m-%d").replace(
hour=17, minute=30, second=0, microsecond=0
)
notifications = Notification.query.filter( notifications = Notification.query.filter(
Notification.created_at < convert_bst_to_utc(last_processing_deadline), Notification.created_at < convert_bst_to_utc(print_run_deadline),
Notification.notification_type == LETTER_TYPE, Notification.notification_type == LETTER_TYPE,
Notification.status == NOTIFICATION_CREATED, Notification.status == NOTIFICATION_CREATED,
Notification.key_type == KEY_TYPE_NORMAL Notification.key_type == KEY_TYPE_NORMAL

View File

@@ -296,7 +296,7 @@ def test_get_key_and_size_of_letters_to_be_sent_to_print(notify_api, mocker, sam
{'ContentLength': 3}, {'ContentLength': 3},
]) ])
results = get_key_and_size_of_letters_to_be_sent_to_print('2020-02-17') results = get_key_and_size_of_letters_to_be_sent_to_print(datetime.now() - timedelta(minutes=30))
assert mock_s3.call_count == 3 assert mock_s3.call_count == 3
mock_s3.assert_has_calls( mock_s3.assert_has_calls(