Move the condition outside the method, remove the notification_type variable.

This commit is contained in:
Rebecca Law
2018-08-10 13:11:23 +01:00
parent d0e9ab4972
commit f844a39ea6

View File

@@ -322,7 +322,8 @@ def delete_notifications_created_more_than_a_week_ago_by_type(notification_type)
query = db.session.query(Notification).filter( query = db.session.query(Notification).filter(
func.date(Notification.created_at) < days_of_retention, func.date(Notification.created_at) < days_of_retention,
Notification.notification_type == f.notification_type, Notification.service_id == f.service_id) Notification.notification_type == f.notification_type, Notification.service_id == f.service_id)
_delete_letters_from_s3(notification_type, query) if notification_type == LETTER_TYPE:
_delete_letters_from_s3(query)
deleted += query.delete(synchronize_session='fetch') deleted += query.delete(synchronize_session='fetch')
seven_days_ago = convert_utc_to_bst(datetime.utcnow()).date() - timedelta(days=7) seven_days_ago = convert_utc_to_bst(datetime.utcnow()).date() - timedelta(days=7)
@@ -331,29 +332,29 @@ def delete_notifications_created_more_than_a_week_ago_by_type(notification_type)
Notification.notification_type == notification_type, Notification.notification_type == notification_type,
Notification.service_id.notin_( Notification.service_id.notin_(
services_with_data_retention)) services_with_data_retention))
_delete_letters_from_s3(notification_type=notification_type, query=query) if notification_type == LETTER_TYPE:
_delete_letters_from_s3(query=query)
deleted = query.delete(synchronize_session='fetch') deleted = query.delete(synchronize_session='fetch')
return deleted return deleted
def _delete_letters_from_s3(notification_type, query): def _delete_letters_from_s3(query):
if notification_type == LETTER_TYPE: letters_to_delete_from_s3 = query.all()
letters_to_delete_from_s3 = query.all() for letter in letters_to_delete_from_s3:
for letter in letters_to_delete_from_s3: bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME'] sent_at = str(letter.sent_at.date())
sent_at = str(letter.sent_at.date()) prefix = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
prefix = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format( folder=sent_at,
folder=sent_at, reference=letter.reference,
reference=letter.reference, duplex="D",
duplex="D", letter_class="2",
letter_class="2", colour="C",
colour="C", crown="C" if letter.service.crown else "N",
crown="C" if letter.service.crown else "N", date=''
date='' ).upper()[:-5]
).upper()[:-5] s3_objects = get_s3_object_by_prefix(bucket_name=bucket_name, prefix=prefix)
s3_objects = get_s3_object_by_prefix(bucket_name=bucket_name, prefix=prefix) for s3_object in s3_objects:
for s3_object in s3_objects: s3_object.delete()
s3_object.delete()
@statsd(namespace="dao") @statsd(namespace="dao")