You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.3 KiB

  1. from datetime import timedelta
  2. import time
  3. from django.core.management.base import BaseCommand
  4. from django.db.models import Q
  5. from django.utils import timezone
  6. from hc.accounts.models import NO_NAG, Profile
  7. from hc.api.models import Check
  8. def num_pinged_checks(profile):
  9. q = Check.objects.filter(user_id=profile.user.id,)
  10. q = q.filter(last_ping__isnull=False)
  11. return q.count()
  12. class Command(BaseCommand):
  13. help = 'Send due monthly reports and nags'
  14. tmpl = "Sent monthly report to %s"
  15. def add_arguments(self, parser):
  16. parser.add_argument(
  17. '--loop',
  18. action='store_true',
  19. dest='loop',
  20. default=False,
  21. help='Keep running indefinitely in a 300 second wait loop',
  22. )
  23. def handle_one_monthly_report(self):
  24. now = timezone.now()
  25. month_before = now - timedelta(days=30)
  26. month_after = now + timedelta(days=30)
  27. report_due = Q(next_report_date__lt=now)
  28. report_not_scheduled = Q(next_report_date__isnull=True)
  29. q = Profile.objects.filter(report_due | report_not_scheduled)
  30. q = q.filter(reports_allowed=True)
  31. q = q.filter(user__date_joined__lt=month_before)
  32. profile = q.first()
  33. if profile is None:
  34. return False
  35. # A sort of optimistic lock. Try to update next_report_date,
  36. # and if does get modified, we're in drivers seat:
  37. qq = Profile.objects.filter(id=profile.id,
  38. next_report_date=profile.next_report_date)
  39. num_updated = qq.update(next_report_date=month_after)
  40. if num_updated != 1:
  41. # next_report_date was already updated elsewhere, skipping
  42. return True
  43. if profile.send_report():
  44. self.stdout.write(self.tmpl % profile.user.email)
  45. # Pause before next report to avoid hitting sending quota
  46. time.sleep(1)
  47. return True
  48. def handle_one_nag(self):
  49. now = timezone.now()
  50. q = Profile.objects.filter(next_nag_date__lt=now)
  51. q = q.exclude(nag_period=NO_NAG)
  52. profile = q.first()
  53. if profile is None:
  54. return False
  55. qq = Profile.objects.filter(id=profile.id,
  56. next_nag_date=profile.next_nag_date)
  57. num_updated = qq.update(next_nag_date=now + profile.nag_period)
  58. if num_updated != 1:
  59. # next_rag_date was already updated elsewhere, skipping
  60. return True
  61. if profile.send_report(nag=True):
  62. self.stdout.write("Sent nag to %s" % profile.user.email)
  63. # Pause before next report to avoid hitting sending quota
  64. time.sleep(1)
  65. else:
  66. profile.next_nag_date = None
  67. profile.save()
  68. return True
  69. def handle(self, *args, **options):
  70. self.stdout.write("sendreports is now running")
  71. while True:
  72. # Monthly reports
  73. while self.handle_one_monthly_report():
  74. pass
  75. # Daily and hourly nags
  76. while self.handle_one_nag():
  77. pass
  78. if not options["loop"]:
  79. break
  80. formatted = timezone.now().isoformat()
  81. self.stdout.write("-- MARK %s --" % formatted)
  82. # Sleep for 1 minute before looking for more work
  83. time.sleep(60)