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.

29 lines
966 B

  1. from datetime import timedelta
  2. from django.contrib.auth.models import User
  3. from django.core.management.base import BaseCommand
  4. from django.db.models import Count
  5. from django.utils import timezone
  6. class Command(BaseCommand):
  7. help = """Prune old, inactive user accounts.
  8. Conditions for removing an user account:
  9. - created 1 month ago and never logged in. Does not belong
  10. to any team.
  11. Use case: visitor types in their email at the website but
  12. never follows through with login.
  13. """
  14. def handle(self, *args, **options):
  15. cutoff = timezone.now() - timedelta(days=30)
  16. # Old accounts, never logged in, no team memberships
  17. q = User.objects.order_by("id")
  18. q = q.annotate(n_teams=Count("memberships"))
  19. q = q.filter(date_joined__lt=cutoff, last_login=None, n_teams=0)
  20. n, summary = q.delete()
  21. return "Done! Pruned %d user accounts." % summary.get("auth.User", 0)