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.

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