Browse Source

Management command to send monthly reports.

pull/20/head
Pēteris Caune 9 years ago
parent
commit
4da8ebec88
1 changed files with 42 additions and 0 deletions
  1. +42
    -0
      hc/api/management/commands/sendreports.py

+ 42
- 0
hc/api/management/commands/sendreports.py View File

@ -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)

Loading…
Cancel
Save