Example #1
0
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()))
Example #2
0
def add_expirations():
    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
                if workdays.between(branch.last_action.deadline_date,
                                    local_today()) < 30:
                    continue
                # Last action obligee deadline was missed at least 30 workdays ago. 30 workdays is
                # half of EXPIRATION deadline.
                filtered.append(branch)
            except Exception:
                cron_logger.error(
                    u'Checking if expiration action should be added failed: %s\n%s'
                    % (repr(branch), traceback.format_exc()))

    for branch in filtered:
        try:
            with transaction.atomic():
                branch.add_expiration_if_expired()
                cron_logger.info(u'Added expiration action: %s' % repr(branch))
        except Exception:
            cron_logger.error(u'Adding expiration action failed: %s\n%s' %
                              (repr(branch), traceback.format_exc()))
Example #3
0
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:
                action = branch.last_action
                if action.deadline and action.deadline.snooze_calendar_days_behind < 100:
                    break
            else:
                # Every branch that has a deadline have been missed for at least 100 WD.
                filtered.append(inforequest)
        except Exception:
            msg = u'Checking if inforequest should be closed failed: {}\n{}'
            trace = unicode(traceback.format_exc(), u'utf-8')
            cron_logger.error(msg.format(inforequest, trace))

    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'])
                cron_logger.info(u'Closed inforequest: {}'.format(inforequest))
        except Exception:
            msg = u'Closing inforequest failed: {}\n{}'
            trace = unicode(traceback.format_exc(), u'utf-8')
            cron_logger.error(msg.format(inforequest, trace))
Example #4
0
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()))
Example #5
0
def add_expirations():
    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:
                action = branch.last_action
                if not action.has_obligee_deadline_snooze_missed:
                    continue
                if action.deadline.calendar_days_behind <= 8:
                    continue
                # The last action obligee deadline was missed more than 8 calendar days ago. The
                # applicant may snooze for at most 8 calendar days. So it's safe to add expiration
                # now. The expiration action has 15 calendar days deadline of which about half is
                # still left.
                filtered.append(branch)
            except Exception:
                msg = u'Checking if expiration action should be added failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(branch, trace))

    for branch in filtered:
        try:
            with transaction.atomic():
                branch.add_expiration_if_expired()
                cron_logger.info(u'Added expiration action: {}'.format(branch))
        except Exception:
            msg = u'Adding expiration action failed: {}\n{}'
            trace = unicode(traceback.format_exc(), u'utf-8')
            cron_logger.error(msg.format(branch, trace))
Example #6
0
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:
                action = branch.last_action
                if action.deadline and action.deadline.snooze_calendar_days_behind < 100:
                    break
            else:
                # Every branch that has a deadline have been missed for at least 100 WD.
                filtered.append(inforequest)
        except Exception:
            msg = u'Checking if inforequest should be closed failed: {}\n{}'
            trace = unicode(traceback.format_exc(), u'utf-8')
            cron_logger.error(msg.format(inforequest, trace))

    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'])
                cron_logger.info(u'Closed inforequest: {}'.format(inforequest))
        except Exception:
            msg = u'Closing inforequest failed: {}\n{}'
            trace = unicode(traceback.format_exc(), u'utf-8')
            cron_logger.error(msg.format(inforequest, trace))
Example #7
0
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()))
Example #8
0
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()))
Example #9
0
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:
                action = branch.last_action
                try:
                    if not action.has_obligee_deadline_snooze_missed:
                        continue
                    # The last reminder was sent after the applicant snoozed for the last time iff
                    # the snooze was missed before the reminder was sent. We don't want to send any
                    # more reminders if the last reminder was sent after the last snooze.
                    last = action.last_deadline_reminder
                    last_date = local_date(last) if last else None
                    if last and action.deadline.is_snooze_missed_at(last_date):
                        continue
                    filtered.append(branch)
                except Exception:
                    msg = u'Checking if obligee deadline reminder should be sent failed: {}\n{}'
                    trace = unicode(traceback.format_exc(), u'utf-8')
                    cron_logger.error(msg.format(action, trace))

        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)
                    msg = u'Sent obligee deadline reminder: {}'
                    cron_logger.info(msg.format(branch.last_action))
            except Exception:
                msg = u'Sending obligee deadline reminder failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(branch.last_action, trace))
Example #10
0
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()))
Example #11
0
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:
                action = branch.last_action
                try:
                    if not action.has_applicant_deadline:
                        continue
                    # The reminder is sent 2 CD before the deadline is missed.
                    if action.deadline.calendar_days_remaining > 2:
                        continue
                    # Applicant may not snooze his deadlines, so we send at most one applicant
                    # deadline reminder for the action.
                    if action.last_deadline_reminder:
                        continue
                    filtered.append(branch)
                except Exception:
                    msg = u'Checking if applicant deadline reminder should be sent failed: {}\n{}'
                    trace = unicode(traceback.format_exc(), u'utf-8')
                    cron_logger.error(msg.format(action, trace))

        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)
                    msg = u'Sent applicant deadline reminder: {}'
                    cron_logger.info(msg.format(branch.last_action))
            except Exception:
                msg = u'Sending applicant deadline reminder failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(branch.last_action, trace))
Example #12
0
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:
                action = branch.last_action
                try:
                    if not action.has_obligee_deadline_snooze_missed:
                        continue
                    # The last reminder was sent after the applicant snoozed for the last time iff
                    # the snooze was missed before the reminder was sent. We don't want to send any
                    # more reminders if the last reminder was sent after the last snooze.
                    last = action.last_deadline_reminder
                    last_date = local_date(last) if last else None
                    if last and action.deadline.is_snooze_missed_at(last_date):
                        continue
                    filtered.append(branch)
                except Exception:
                    msg = u'Checking if obligee deadline reminder should be sent failed: {}\n{}'
                    trace = unicode(traceback.format_exc(), u'utf-8')
                    cron_logger.error(msg.format(action, trace))

        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)
                    msg = u'Sent obligee deadline reminder: {}'
                    cron_logger.info(msg.format(branch.last_action))
            except Exception:
                msg = u'Sending obligee deadline reminder failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(branch.last_action, trace))
Example #13
0
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:
                action = branch.last_action
                try:
                    if not action.has_applicant_deadline:
                        continue
                    # The reminder is sent 2 CD before the deadline is missed.
                    if action.deadline.calendar_days_remaining > 2:
                        continue
                    # Applicant may not snooze his deadlines, so we send at most one applicant
                    # deadline reminder for the action.
                    if action.last_deadline_reminder:
                        continue
                    filtered.append(branch)
                except Exception:
                    msg = u'Checking if applicant deadline reminder should be sent failed: {}\n{}'
                    trace = unicode(traceback.format_exc(), u'utf-8')
                    cron_logger.error(msg.format(action, trace))

        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)
                    msg = u'Sent applicant deadline reminder: {}'
                    cron_logger.info(msg.format(branch.last_action))
            except Exception:
                msg = u'Sending applicant deadline reminder failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(branch.last_action, trace))
Example #14
0
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
                filtered.append(inforequest)
            except Exception:
                msg = u'Checking if undecided email reminder should be sent failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(inforequest, trace))

        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()
                    cron_logger.info(u'Sent undecided email reminder: {}'.format(inforequest))
            except Exception:
                msg = u'Sending undecided email reminder failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(inforequest, trace))
Example #15
0
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()))
Example #16
0
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
                filtered.append(inforequest)
            except Exception:
                msg = u'Checking if undecided email reminder should be sent failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(inforequest, trace))

        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()
                    cron_logger.info(
                        u'Sent undecided email reminder: {}'.format(
                            inforequest))
            except Exception:
                msg = u'Sending undecided email reminder failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(inforequest, trace))
Example #17
0
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()))
Example #18
0
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()))
Example #19
0
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()))
Example #20
0
def add_expirations():
    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:
                action = branch.last_action
                if not action.has_obligee_deadline_snooze_missed:
                    continue
                if action.deadline.calendar_days_behind <= 8:
                    continue
                # The last action obligee deadline was missed more than 8 calendar days ago. The
                # applicant may snooze for at most 8 calendar days. So it's safe to add expiration
                # now. The expiration action has 15 calendar days deadline of which about half is
                # still left.
                filtered.append(branch)
            except Exception:
                msg = u'Checking if expiration action should be added failed: {}\n{}'
                trace = unicode(traceback.format_exc(), u'utf-8')
                cron_logger.error(msg.format(branch, trace))

    for branch in filtered:
        try:
            with transaction.atomic():
                branch.add_expiration_if_expired()
                cron_logger.info(u'Added expiration action: {}'.format(branch))
        except Exception:
            msg = u'Adding expiration action failed: {}\n{}'
            trace = unicode(traceback.format_exc(), u'utf-8')
            cron_logger.error(msg.format(branch, trace))
Example #21
0
def clear_expired_sessions():
    call_command(u'clearsessions')
    cron_logger.info(u'Cleared expired sessions.')
Example #22
0
def datacheck():
    issues = registry.run_checks(superficial=True)
    for issue in issues:
        cron_logger.log(issue.level, u'%s', issue)
    cron_logger.info(u'Data check identified %s issues.', len(issues))
Example #23
0
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))
Example #24
0
def clear_old_cronlogs():
    threshold = utc_now() - timedelta(days=7)
    CronJobLog.objects.filter(start_time__lt=threshold).delete()
    cron_logger.info(u'Cleared old cron logs.')
Example #25
0
def datacheck():
    issues = registry.run_checks(superficial=True)
    for issue in issues:
        cron_logger.log(issue.level, u'%s', issue)
    cron_logger.info(u'Data check identified %s issues.', len(issues))
Example #26
0
def clear_old_cronlogs():
    threshold = utc_now() - timedelta(days=7)
    CronJobLog.objects.filter(start_time__lt=threshold).delete()
    cron_logger.info(u'Cleared old cron logs.')
Example #27
0
def clear_expired_sessions():
    call_command(u'clearsessions')
    cron_logger.info(u'Cleared expired sessions.')