def close_inforequests(): inforequests = (Inforequest.objects .not_closed() .prefetch_related(Inforequest.prefetch_branches()) .prefetch_related(Branch.prefetch_last_action(u'branches')) ) filtered = [] for inforequest in inforequests: try: for branch in inforequest.branches: if branch.last_action.has_deadline and branch.last_action.deadline_remaining > -100: break else: nop() # To let tests raise testing exception here. filtered.append(inforequest) # Every branch that has a deadline have been missed for at least 100 WD. except Exception: cron_logger.error(u'Checking if inforequest should be closed failed: %s\n%s' % (repr(inforequest), traceback.format_exc())) for inforequest in filtered: try: with transaction.atomic(): for branch in inforequest.branches: branch.add_expiration_if_expired() inforequest.closed = True inforequest.save(update_fields=[u'closed']) nop() # To let tests raise testing exception here. cron_logger.info(u'Closed inforequest: %s' % repr(inforequest)) # pragma: no branch except Exception: cron_logger.error(u'Closing inforequest failed: %s\n%s' % (repr(inforequest), traceback.format_exc()))
def close_inforequests(): inforequests = (Inforequest.objects.not_closed().prefetch_related( Inforequest.prefetch_branches()).prefetch_related( Branch.prefetch_last_action(u'branches'))) filtered = [] for inforequest in inforequests: try: for branch in inforequest.branches: if branch.last_action.has_deadline and branch.last_action.deadline_remaining > -100: break else: nop() # To let tests raise testing exception here. filtered.append( inforequest ) # Every branch that has a deadline have been missed for at least 100 WD. except Exception: cron_logger.error( u'Checking if inforequest should be closed failed: %s\n%s' % (repr(inforequest), traceback.format_exc())) for inforequest in filtered: try: with transaction.atomic(): for branch in inforequest.branches: branch.add_expiration_if_expired() inforequest.closed = True inforequest.save(update_fields=[u'closed']) nop() # To let tests raise testing exception here. cron_logger.info(u'Closed inforequest: %s' % repr(inforequest)) # pragma: no branch except Exception: cron_logger.error(u'Closing inforequest failed: %s\n%s' % (repr(inforequest), traceback.format_exc()))
def obligee_deadline_reminder(): with translation(settings.LANGUAGE_CODE): inforequests = (Inforequest.objects.not_closed(). without_undecided_email().prefetch_related( Inforequest.prefetch_branches()).prefetch_related( Branch.prefetch_last_action(u'branches'))) filtered = [] for inforequest in inforequests: for branch in inforequest.branches: try: if not branch.last_action.has_obligee_deadline: continue if not branch.last_action.deadline_missed: continue # The last reminder was sent after the deadline was extended for the last time iff # the extended deadline was missed before the reminder was sent. We don't want to # send any more reminders if the last reminder was sent after the deadline was # extended for the last time. last = branch.last_action.last_deadline_reminder last_date = local_date(last) if last else None if last and branch.last_action.deadline_missed_at( last_date): continue nop() # To let tests raise testing exception here. filtered.append(branch) except Exception: cron_logger.error( u'Checking if obligee deadline reminder should be sent failed: %s\n%s' % (repr(branch.last_action), traceback.format_exc())) if not filtered: return filtered = (Branch.objects.select_related(u'inforequest__applicant'). select_related(u'historicalobligee').prefetch_related( Branch.prefetch_last_action()).filter( pk__in=(o.pk for o in filtered))) for branch in filtered: try: with transaction.atomic(): branch.inforequest.send_obligee_deadline_reminder( branch.last_action) nop() # To let tests raise testing exception here. cron_logger.info( u'Sent obligee deadline reminder: %s' % repr(branch.last_action)) # pragma: no branch except Exception: cron_logger.error( u'Sending obligee deadline reminder failed: %s\n%s' % (repr(branch.last_action), traceback.format_exc()))
def applicant_deadline_reminder(): with translation(settings.LANGUAGE_CODE): inforequests = (Inforequest.objects.not_closed(). without_undecided_email().prefetch_related( Inforequest.prefetch_branches()).prefetch_related( Branch.prefetch_last_action(u'branches'))) filtered = [] for inforequest in inforequests: for branch in inforequest.branches: try: if not branch.last_action.has_applicant_deadline: continue # Although Advancement has an applicant deadline, we don't send reminders for it. if branch.last_action.type == Action.TYPES.ADVANCEMENT: continue # The reminder is sent 2 WDs before the deadline is missed. if branch.last_action.deadline_remaining > 2: continue # Applicant deadlines may not be extended, so we send at most one applicant # deadline reminder for the action. if branch.last_action.last_deadline_reminder: continue nop() # To let tests raise testing exception here. filtered.append(branch) except Exception: cron_logger.error( u'Checking if applicant deadline reminder should be sent failed: %s\n%s' % (repr(branch.last_action), traceback.format_exc())) if not filtered: return filtered = (Branch.objects.select_related( u'inforequest__applicant').prefetch_related( Branch.prefetch_last_action()).filter( pk__in=(o.pk for o in filtered))) for branch in filtered: try: with transaction.atomic(): branch.inforequest.send_applicant_deadline_reminder( branch.last_action) nop() # To let tests raise testing exception here. cron_logger.info( u'Sent applicant deadline reminder: %s' % repr(branch.last_action)) # pragma: no branch except Exception: cron_logger.error( u'Sending applicant deadline reminder failed: %s\n%s' % (repr(branch.last_action), traceback.format_exc()))
def obligee_deadline_reminder(): with translation(settings.LANGUAGE_CODE): inforequests = (Inforequest.objects .not_closed() .without_undecided_email() .prefetch_related(Inforequest.prefetch_branches()) .prefetch_related(Branch.prefetch_last_action(u'branches')) ) filtered = [] for inforequest in inforequests: for branch in inforequest.branches: try: if not branch.last_action.has_obligee_deadline: continue if not branch.last_action.deadline_missed: continue # The last reminder was sent after the deadline was extended for the last time iff # the extended deadline was missed before the reminder was sent. We don't want to # send any more reminders if the last reminder was sent after the deadline was # extended for the last time. last = branch.last_action.last_deadline_reminder last_date = local_date(last) if last else None if last and branch.last_action.deadline_missed_at(last_date): continue nop() # To let tests raise testing exception here. filtered.append(branch) except Exception: cron_logger.error(u'Checking if obligee deadline reminder should be sent failed: %s\n%s' % (repr(branch.last_action), traceback.format_exc())) if not filtered: return filtered = (Branch.objects .select_related(u'inforequest__applicant') .select_related(u'historicalobligee') .prefetch_related(Branch.prefetch_last_action()) .filter(pk__in=(o.pk for o in filtered)) ) for branch in filtered: try: with transaction.atomic(): branch.inforequest.send_obligee_deadline_reminder(branch.last_action) nop() # To let tests raise testing exception here. cron_logger.info(u'Sent obligee deadline reminder: %s' % repr(branch.last_action)) # pragma: no branch except Exception: cron_logger.error(u'Sending obligee deadline reminder failed: %s\n%s' % (repr(branch.last_action), traceback.format_exc()))
def undecided_email_reminder(): with translation(settings.LANGUAGE_CODE): inforequests = (Inforequest.objects.not_closed().with_undecided_email( ).prefetch_related(Inforequest.prefetch_newest_undecided_email())) filtered = [] for inforequest in inforequests: try: email = inforequest.newest_undecided_email last = inforequest.last_undecided_email_reminder if last and last > email.processed: continue days = workdays.between(local_date(email.processed), local_today()) if days < 5: continue nop() # To let tests raise testing exception here. filtered.append(inforequest) except Exception: cron_logger.error( u'Checking if undecided email reminder should be sent failed: %s\n%s' % (repr(inforequest), traceback.format_exc())) if not filtered: return filtered = (Inforequest.objects.select_related( u'applicant').select_undecided_emails_count().prefetch_related( Inforequest.prefetch_main_branch( None, Branch.objects.select_related(u'historicalobligee')) ).prefetch_related( Inforequest.prefetch_newest_undecided_email()).filter( pk__in=(o.pk for o in filtered))) for inforequest in filtered: try: with transaction.atomic(): inforequest.send_undecided_email_reminder() nop() # To let tests raise testing exception here. cron_logger.info(u'Sent undecided email reminder: %s' % repr(inforequest)) # pragma: no branch except Exception: cron_logger.error( u'Sending undecided email reminder failed: %s\n%s' % (repr(inforequest), traceback.format_exc()))
def applicant_deadline_reminder(): with translation(settings.LANGUAGE_CODE): inforequests = (Inforequest.objects .not_closed() .without_undecided_email() .prefetch_related(Inforequest.prefetch_branches()) .prefetch_related(Branch.prefetch_last_action(u'branches')) ) filtered = [] for inforequest in inforequests: for branch in inforequest.branches: try: if not branch.last_action.has_applicant_deadline: continue # The reminder is sent 2 WDs before the deadline is missed. if branch.last_action.deadline_remaining > 2: continue # Applicant deadlines may not be extended, so we send at most one applicant # deadline reminder for the action. if branch.last_action.last_deadline_reminder: continue nop() # To let tests raise testing exception here. filtered.append(branch) except Exception: cron_logger.error(u'Checking if applicant deadline reminder should be sent failed: %s\n%s' % (repr(branch.last_action), traceback.format_exc())) if not filtered: return filtered = (Branch.objects .select_related(u'inforequest__applicant') .prefetch_related(Branch.prefetch_last_action()) .filter(pk__in=(o.pk for o in filtered)) ) for branch in filtered: try: with transaction.atomic(): branch.inforequest.send_applicant_deadline_reminder(branch.last_action) nop() # To let tests raise testing exception here. cron_logger.info(u'Sent applicant deadline reminder: %s' % repr(branch.last_action)) # pragma: no branch except Exception: cron_logger.error(u'Sending applicant deadline reminder failed: %s\n%s' % (repr(branch.last_action), traceback.format_exc()))
def undecided_email_reminder(): with translation(settings.LANGUAGE_CODE): inforequests = (Inforequest.objects .not_closed() .with_undecided_email() .prefetch_related(Inforequest.prefetch_newest_undecided_email()) ) filtered = [] for inforequest in inforequests: try: email = inforequest.newest_undecided_email last = inforequest.last_undecided_email_reminder if last and last > email.processed: continue days = workdays.between(local_date(email.processed), local_today()) if days < 5: continue nop() # To let tests raise testing exception here. filtered.append(inforequest) except Exception: cron_logger.error(u'Checking if undecided email reminder should be sent failed: %s\n%s' % (repr(inforequest), traceback.format_exc())) if not filtered: return filtered = (Inforequest.objects .select_related(u'applicant') .select_undecided_emails_count() .prefetch_related(Inforequest.prefetch_main_branch(None, Branch.objects.select_related(u'historicalobligee'))) .prefetch_related(Inforequest.prefetch_newest_undecided_email()) .filter(pk__in=(o.pk for o in filtered)) ) for inforequest in filtered: try: with transaction.atomic(): inforequest.send_undecided_email_reminder() nop() # To let tests raise testing exception here. cron_logger.info(u'Sent undecided email reminder: %s' % repr(inforequest)) # pragma: no branch except Exception: cron_logger.error(u'Sending undecided email reminder failed: %s\n%s' % (repr(inforequest), traceback.format_exc()))
def mail(): # Get inbound mail path = getattr(settings, u'EMAIL_INBOUND_TRANSPORT', None) if path: klass = import_by_path(path) with klass() as transport: messages = transport.get_messages() while True: try: with transaction.atomic(): message = next(messages) nop() # To let tests raise testing exception here. cron_logger.info(u'Received email: %s' % repr(message)) except StopIteration: break except Exception: cron_logger.error(u'Receiving emails failed:\n%s' % traceback.format_exc()) break # Process inbound mail; At most 10 messages in one batch messages = (Message.objects.inbound().not_processed().order_by_pk(). prefetch_related(Message.prefetch_recipients()))[:10] for message in messages: try: with transaction.atomic(): message.processed = utc_now() message.save(update_fields=[u'processed']) message_received.send(sender=None, message=message) nop() # To let tests raise testing exception here. cron_logger.info(u'Processed received email: %s' % repr(message)) except Exception: cron_logger.error(u'Processing received email failed: %s\n%s' % (repr(message), traceback.format_exc())) # Send outbound mail; At most 10 messages in one batch path = getattr(settings, u'EMAIL_OUTBOUND_TRANSPORT', None) if path: messages = (Message.objects.outbound().not_processed().order_by_pk( ).prefetch_related(Message.prefetch_recipients()).prefetch_related( Message.prefetch_attachments()))[:10] if messages: klass = import_by_path(path) with klass() as transport: for message in messages: try: with transaction.atomic(): transport.send_message(message) message.processed = utc_now() message.save(update_fields=[u'processed']) message_sent.send(sender=None, message=message) nop() # To let tests raise testing exception here. cron_logger.info(u'Sent email: %s' % repr(message)) except Exception: cron_logger.error( u'Seding email failed: %s\n%s' % (repr(message), traceback.format_exc()))
def mail(): # Get inbound mail path = getattr(settings, u'EMAIL_INBOUND_TRANSPORT', None) if path: klass = import_by_path(path) with klass() as transport: messages = transport.get_messages() while True: try: with transaction.atomic(): message = next(messages) nop() # To let tests raise testing exception here. cron_logger.info(u'Received email: {}'.format(message)) except StopIteration: break except Exception: trace = unicode(traceback.format_exc(), u'utf-8') cron_logger.error(u'Receiving emails failed:\n{}'.format(trace)) break # Process inbound mail; At most 10 messages in one batch messages = (Message.objects .inbound() .not_processed() .order_by_pk() .prefetch_related(Message.prefetch_recipients()) )[:10] for message in messages: try: with transaction.atomic(): message.processed = utc_now() message.save(update_fields=[u'processed']) message_received.send(sender=None, message=message) nop() # To let tests raise testing exception here. cron_logger.info(u'Processed received email: {}'.format(message)) except Exception: trace = unicode(traceback.format_exc(), u'utf-8') cron_logger.error(u'Processing received email failed: {}\n{}'.format(message, trace)) # Send outbound mail; At most 10 messages in one batch path = getattr(settings, u'EMAIL_OUTBOUND_TRANSPORT', None) if path: messages = (Message.objects .outbound() .not_processed() .order_by_pk() .prefetch_related(Message.prefetch_recipients()) .prefetch_related(Message.prefetch_attachments()) )[:10] if messages: klass = import_by_path(path) with klass() as transport: for message in messages: try: with transaction.atomic(): transport.send_message(message) message.processed = utc_now() message.save(update_fields=[u'processed']) message_sent.send(sender=None, message=message) nop() # To let tests raise testing exception here. cron_logger.info(u'Sent email: {}'.format(message)) except Exception: trace = unicode(traceback.format_exc(), u'utf-8') cron_logger.error(u'Seding email failed: {}\n{}'.format(message, trace))