From 4da8ebec88ee6a28bbb0f3cf8495e3ab62182df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 18 Dec 2015 03:53:00 +0200 Subject: [PATCH] Management command to send monthly reports. --- hc/api/management/commands/sendreports.py | 42 +++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 hc/api/management/commands/sendreports.py diff --git a/hc/api/management/commands/sendreports.py b/hc/api/management/commands/sendreports.py new file mode 100644 index 00000000..0a2ac688 --- /dev/null +++ b/hc/api/management/commands/sendreports.py @@ -0,0 +1,42 @@ +from datetime import timedelta + +from django.contrib.auth.models import User +from django.core.management.base import BaseCommand +from django.db.models import Q +from django.utils import timezone +from hc.accounts.models import Profile +from hc.api.models import Check + + +def num_pinged_checks(profile): + q = Check.objects.filter(user_id=profile.user.id,) + q = q.filter(last_ping__isnull=False) + return q.count() + + +class Command(BaseCommand): + help = 'Send due monthly reports' + + def handle(self, *args, **options): + # Create any missing profiles + for u in User.objects.filter(profile__isnull=True): + print("Creating profile for %s" % u.email) + Profile.objects.for_user(u) + + now = timezone.now() + month_before = now - timedelta(days=30) + + report_due = Q(next_report_date__lt=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) + q = q.filter(user__date_joined__lt=month_before) + sent = 0 + for profile in q: + if num_pinged_checks(profile) > 0: + print("Sending monthly report to %s" % profile.user.email) + profile.send_report() + sent += 1 + + print("Sent %d reports" % sent)