diff --git a/hc/api/management/commands/sendreports.py b/hc/api/management/commands/sendreports.py index cb513d62..0979909e 100644 --- a/hc/api/management/commands/sendreports.py +++ b/hc/api/management/commands/sendreports.py @@ -30,6 +30,7 @@ class Command(BaseCommand): def handle_one_run(self): now = timezone.now() month_before = now - timedelta(days=30) + month_after = now + timedelta(days=30) report_due = Q(next_report_date__lt=now) report_not_scheduled = Q(next_report_date__isnull=True) @@ -37,12 +38,25 @@ class Command(BaseCommand): q = Profile.objects.filter(report_due | report_not_scheduled) q = q.filter(reports_allowed=True) q = q.filter(user__date_joined__lt=month_before) + profiles = list(q) + sent = 0 - for profile in q: - if num_pinged_checks(profile) > 0: - self.stdout.write(self.tmpl % profile.user.email) - profile.send_report() - sent += 1 + for profile in profiles: + qq = Profile.objects + qq = qq.filter(id=profile.id, + next_report_date=profile.next_report_date) + + num_updated = qq.update(next_report_date=month_after) + if num_updated != 1: + # Was updated elsewhere, skipping + continue + + if num_pinged_checks(profile) == 0: + continue + + self.stdout.write(self.tmpl % profile.user.email) + profile.send_report() + sent += 1 return sent