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.

63 lines
2.1 KiB

  1. from datetime import timedelta
  2. import time
  3. from django.conf import settings
  4. from django.core.management.base import BaseCommand
  5. from django.utils.timezone import now
  6. from hc.accounts.models import Profile, Member
  7. from hc.api.models import Ping
  8. from hc.lib import emails
  9. class Command(BaseCommand):
  10. help = """Send deletion notices to inactive user accounts.
  11. Conditions for sending the notice:
  12. - deletion notice has not been sent recently
  13. - last login more than a year ago
  14. - none of the owned projects has invited team members
  15. """
  16. def handle(self, *args, **options):
  17. year_ago = now() - timedelta(days=365)
  18. q = Profile.objects.order_by("id")
  19. # Exclude accounts with logins in the last year_ago
  20. q = q.exclude(user__last_login__gt=year_ago)
  21. # Exclude accounts less than a year_ago old
  22. q = q.exclude(user__date_joined__gt=year_ago)
  23. # Exclude accounts with the deletion notice already sent
  24. q = q.exclude(deletion_notice_date__gt=year_ago)
  25. # Exclude paid accounts
  26. q = q.exclude(sms_limit__gt=0)
  27. sent = 0
  28. for profile in q:
  29. members = Member.objects.filter(project__owner_id=profile.user_id)
  30. if members.exists():
  31. print("Skipping %s, has team members" % profile)
  32. continue
  33. pings = Ping.objects
  34. pings = pings.filter(owner__project__owner_id=profile.user_id)
  35. pings = pings.filter(created__gt=year_ago)
  36. if pings.exists():
  37. print("Skipping %s, has pings in last year" % profile)
  38. continue
  39. self.stdout.write("Sending notice to %s" % profile.user.email)
  40. profile.deletion_notice_date = now()
  41. profile.save()
  42. ctx = {
  43. "email": profile.user.email,
  44. "support_email": settings.SUPPORT_EMAIL
  45. }
  46. emails.deletion_notice(profile.user.email, ctx)
  47. # Throttle so we don't send too many emails at once:
  48. time.sleep(1)
  49. sent += 1
  50. return "Done! Sent %d notices" % sent