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.

67 lines
2.2 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 pause(self):
  17. time.sleep(1)
  18. def handle(self, *args, **options):
  19. year_ago = now() - timedelta(days=365)
  20. q = Profile.objects.order_by("id")
  21. # Exclude accounts with logins in the last year
  22. q = q.exclude(user__last_login__gt=year_ago)
  23. # Exclude accounts less than a year old
  24. q = q.exclude(user__date_joined__gt=year_ago)
  25. # Exclude accounts with the deletion notice already sent
  26. q = q.exclude(deletion_notice_date__gt=year_ago)
  27. # Exclude accounts with activity in the last year
  28. q = q.exclude(last_active_date__gt=year_ago)
  29. # Exclude paid accounts
  30. q = q.exclude(sms_limit__gt=5)
  31. sent = 0
  32. for profile in q:
  33. members = Member.objects.filter(project__owner_id=profile.user_id)
  34. if members.exists():
  35. self.stdout.write("Skipping %s, has team members" % profile)
  36. continue
  37. pings = Ping.objects
  38. pings = pings.filter(owner__project__owner_id=profile.user_id)
  39. pings = pings.filter(created__gt=year_ago)
  40. if pings.exists():
  41. self.stdout.write("Skipping %s, has pings in last year" % profile)
  42. continue
  43. self.stdout.write("Sending notice to %s" % profile.user.email)
  44. profile.deletion_notice_date = now()
  45. profile.save()
  46. ctx = {"email": profile.user.email, "support_email": settings.SUPPORT_EMAIL}
  47. emails.deletion_notice(profile.user.email, ctx)
  48. # Throttle so we don't send too many emails at once:
  49. self.pause()
  50. sent += 1
  51. return "Done! Sent %d notices" % sent