def update_letter_notifications_statuses(self, filename): bucket_location = '{}-ftp'.format(current_app.config['NOTIFY_EMAIL_DOMAIN']) response_file_content = s3.get_s3_file(bucket_location, filename) sorted_letter_counts = defaultdict(int) try: notification_updates = process_updates_from_file(response_file_content) except TypeError: raise DVLAException('DVLA response file: {} has an invalid format'.format(filename)) else: temporary_failures = [] for update in notification_updates: check_billable_units(update) update_letter_notification(filename, temporary_failures, update) sorted_letter_counts[update.cost_threshold] += 1 try: if sorted_letter_counts.keys() - {'Unsorted', 'Sorted'}: unknown_status = sorted_letter_counts.keys() - {'Unsorted', 'Sorted'} message = 'DVLA response file: {} contains unknown Sorted status {}'.format( filename, unknown_status ) raise DVLAException(message) billing_date = get_billing_date_in_aet_from_filename(filename) persist_daily_sorted_letter_counts(day=billing_date, file_name=filename, sorted_letter_counts=sorted_letter_counts) finally: if temporary_failures: # This will alert Notify that DVLA was unable to deliver the letters, we need to investigate message = "DVLA response file: {filename} has failed letters with notification.reference {failures}" \ .format(filename=filename, failures=temporary_failures) raise DVLAException(message)
def parse_dvla_file(filename): bucket_location = '{}-ftp'.format(current_app.config['NOTIFY_EMAIL_DOMAIN']) response_file_content = s3.get_s3_file(bucket_location, filename) try: return process_updates_from_file(response_file_content) except TypeError: raise DVLAException('DVLA response file: {} has an invalid format'.format(filename))
def check_billable_units(notification_update): notification = dao_get_notification_or_history_by_reference(notification_update.reference) if int(notification_update.page_count) != notification.billable_units: msg = 'Notification with id {} has {} billable_units but DVLA says page count is {}'.format( notification.id, notification.billable_units, notification_update.page_count) try: raise DVLAException(msg) except DVLAException: current_app.logger.exception(msg)
def update_letter_notifications_statuses(self, filename): notification_updates = parse_dvla_file(filename) temporary_failures = [] for update in notification_updates: check_billable_units(update) update_letter_notification(filename, temporary_failures, update) if temporary_failures: # This will alert Notify that DVLA was unable to deliver the letters, we need to investigate message = "DVLA response file: {filename} has failed letters with notification.reference {failures}" \ .format(filename=filename, failures=temporary_failures) raise DVLAException(message)
def record_daily_sorted_counts(self, filename): sorted_letter_counts = defaultdict(int) notification_updates = parse_dvla_file(filename) for update in notification_updates: sorted_letter_counts[update.cost_threshold.lower()] += 1 unknown_status = sorted_letter_counts.keys() - {'unsorted', 'sorted'} if unknown_status: message = 'DVLA response file: {} contains unknown Sorted status {}'.format( filename, unknown_status.__repr__()) raise DVLAException(message) billing_date = get_billing_date_in_est_from_filename(filename) persist_daily_sorted_letter_counts( day=billing_date, file_name=filename, sorted_letter_counts=sorted_letter_counts)
def check_billable_units(notification_update): try: # This try except is necessary because in test keys and research mode does not create notification history. # Otherwise we could just search for the NotificationHistory object notification = dao_get_notification_by_reference( notification_update.reference) except NoResultFound: notification = dao_get_notification_history_by_reference( notification_update.reference) if int(notification_update.page_count) != notification.billable_units: msg = 'Notification with id {} had {} billable_units but a page count of {}'.format( notification.id, notification.billable_units, notification_update.page_count) try: raise DVLAException(msg) except DVLAException: current_app.logger.exception(msg)