def process_returned_letters_list(notification_references): updated, updated_history = dao_update_notifications_by_reference( notification_references, {"status": NOTIFICATION_RETURNED_LETTER}) current_app.logger.info( "Updated {} letter notifications ({} history notifications, from {} references) to returned-letter" .format(updated, updated_history, len(notification_references)))
def update_letter_pdf_status(reference, status): return dao_update_notifications_by_reference( references=[reference], update_dict={ 'status': status, 'updated_at': datetime.utcnow() })
def update_letter_pdf_status(reference, status, billable_units): return dao_update_notifications_by_reference( references=[reference], update_dict={ "status": status, "billable_units": billable_units, "updated_at": datetime.utcnow(), }, )[0]
def update_letter_notifications_to_error(self, notification_references): # This task will be called by the FTP app to update notifications as sent to DVLA updated_count, _ = dao_update_notifications_by_reference( notification_references, { 'status': NOTIFICATION_TECHNICAL_FAILURE, 'updated_at': datetime.utcnow() }) message = "Updated {} letter notifications to technical-failure with references {}".format( updated_count, notification_references) raise NotificationTechnicalFailureException(message)
def update_letter_notifications_to_sent_to_dvla(self, notification_references): # This task will be called by the FTP app to update notifications as sent to DVLA provider = get_current_provider(LETTER_TYPE) updated_count, _ = dao_update_notifications_by_reference( notification_references, { 'status': NOTIFICATION_SENDING, 'sent_by': provider.identifier, 'sent_at': datetime.utcnow(), 'updated_at': datetime.utcnow() }) current_app.logger.info( "Updated {} letter notifications to sending".format(updated_count))
def update_letter_pdf_status(reference, status, billable_units, recipient_address=None): update_dict = { 'status': status, 'billable_units': billable_units, 'updated_at': datetime.utcnow() } if recipient_address: update_dict['to'] = recipient_address return dao_update_notifications_by_reference(references=[reference], update_dict=update_dict)[0]
def update_letter_pdf_status(reference, status, billable_units, recipient_address=None): postage = None if recipient_address: # fix allow_international_letters postage = PostalAddress(raw_address=recipient_address.replace(',', '\n'), allow_international_letters=True ).postage postage = postage if postage in INTERNATIONAL_POSTAGE_TYPES else None update_dict = {'status': status, 'billable_units': billable_units, 'updated_at': datetime.utcnow()} if postage: update_dict.update({'postage': postage, 'international': True}) if recipient_address: update_dict['to'] = recipient_address return dao_update_notifications_by_reference( references=[reference], update_dict=update_dict)[0]
def update_letter_notification(filename, temporary_failures, update): if update.status == DVLA_RESPONSE_STATUS_SENT: status = NOTIFICATION_DELIVERED else: status = NOTIFICATION_TEMPORARY_FAILURE temporary_failures.append(update.reference) updated_count, _ = dao_update_notifications_by_reference( references=[update.reference], update_dict={ "status": status, "updated_at": datetime.utcnow() }) if not updated_count: msg = "Update letter notification file {filename} failed: notification either not found " \ "or already updated from delivered. Status {status} for notification reference {reference}".format( filename=filename, status=status, reference=update.reference) current_app.logger.info(msg)
def process_ses_results(self, response): try: ses_message = json.loads(response['Message']) notification_type = ses_message['notificationType'] bounce_message = None if notification_type == 'Bounce': notification_type, bounce_message = determine_notification_bounce_type(notification_type, ses_message) elif notification_type == 'Complaint': _check_and_queue_complaint_callback_task(*handle_complaint(ses_message)) return True aws_response_dict = get_aws_responses(notification_type) notification_status = aws_response_dict['notification_status'] reference = ses_message['mail']['messageId'] try: notification = notifications_dao.dao_get_notification_or_history_by_reference(reference=reference) except NoResultFound: message_time = iso8601.parse_date(ses_message['mail']['timestamp']).replace(tzinfo=None) if datetime.utcnow() - message_time < timedelta(minutes=5): current_app.logger.info( f"notification not found for reference: {reference} (update to {notification_status}). " f"Callback may have arrived before notification was persisted to the DB. Adding task to retry queue" ) self.retry(queue=QueueNames.RETRY) else: current_app.logger.warning( f"notification not found for reference: {reference} (update to {notification_status})" ) return if bounce_message: current_app.logger.info(f"SES bounce for notification ID {notification.id}: {bounce_message}") if notification.status not in [NOTIFICATION_SENDING, NOTIFICATION_PENDING]: notifications_dao._duplicate_update_warning( notification=notification, status=notification_status ) return else: notifications_dao.dao_update_notifications_by_reference( references=[reference], update_dict={'status': notification_status} ) statsd_client.incr('callback.ses.{}'.format(notification_status)) if notification.sent_at: statsd_client.timing_with_dates('callback.ses.elapsed-time', datetime.utcnow(), notification.sent_at) _check_and_queue_callback_task(notification) return True except Retry: raise except Exception as e: current_app.logger.exception('Error processing SES results: {}'.format(type(e))) self.retry(queue=QueueNames.RETRY)