示例#1
0
    def handle_one_monthly_report(self):
        report_due = Q(next_report_date__lt=timezone.now())
        report_not_scheduled = Q(next_report_date__isnull=True)

        q = Profile.objects.filter(report_due | report_not_scheduled)
        q = q.filter(reports_allowed=True)
        profile = q.first()

        if profile is None:
            # No matching profiles found – nothing to do right now.
            return False

        # A sort of optimistic lock. Will try to update next_report_date,
        # and if does get modified, we're in drivers seat:
        qq = Profile.objects.filter(id=profile.id,
                                    next_report_date=profile.next_report_date)

        # Next report date is currently not scheduled: schedule it and move on.
        if profile.next_report_date is None:
            qq.update(next_report_date=choose_next_report_date())
            return True

        num_updated = qq.update(next_report_date=choose_next_report_date())
        if num_updated != 1:
            # next_report_date was already updated elsewhere, skipping
            return True

        if profile.send_report():
            self.stdout.write(self.tmpl % profile.user.email)
            # Pause before next report to avoid hitting sending quota
            self.pause()

        return True
示例#2
0
def notifications(request):
    profile = request.profile

    ctx = {"status": "default", "page": "profile", "profile": profile}

    if request.method == "POST":
        form = ReportSettingsForm(request.POST)
        if form.is_valid():
            if profile.reports_allowed != form.cleaned_data["reports_allowed"]:
                profile.reports_allowed = form.cleaned_data["reports_allowed"]
                if profile.reports_allowed:
                    profile.next_report_date = choose_next_report_date()
                else:
                    profile.next_report_date = None

            if profile.nag_period != form.cleaned_data["nag_period"]:
                # Set the new nag period
                profile.nag_period = form.cleaned_data["nag_period"]
                # and schedule next_nag_date:
                if profile.nag_period:
                    profile.next_nag_date = now() + profile.nag_period
                else:
                    profile.next_nag_date = None

            profile.save()
            ctx["status"] = "info"

    return render(request, "accounts/notifications.html", ctx)
示例#3
0
    def test_it_works(self):
        # October
        nao = dt(year=2019, month=10, day=15, hour=6)
        result = choose_next_report_date(nao)
        self.assertEqual(result.year, 2019)
        self.assertEqual(result.month, 11)
        self.assertEqual(result.day, 1)
        self.assertTrue(result.hour >= 12)

        # December
        nao = dt(year=2019, month=12, day=15, hour=6)
        result = choose_next_report_date(nao)
        self.assertEqual(result.year, 2020)
        self.assertEqual(result.month, 1)
        self.assertEqual(result.day, 1)
        self.assertTrue(result.hour >= 12)