deleteinactiveusers.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from datetime import timedelta
  2. from django.contrib.auth import get_user_model
  3. from django.core.management.base import BaseCommand
  4. from django.utils import timezone
  5. from misago.conf import settings
  6. from misago.core.pgutils import chunk_queryset
  7. UserModel = get_user_model()
  8. class Command(BaseCommand):
  9. help = "Deletes inactive user accounts older than set time."
  10. def handle(self, *args, **options):
  11. if not settings.MISAGO_DELETE_NEW_INACTIVE_USERS_OLDER_THAN_DAYS:
  12. self.stdout.write(
  13. "Automatic deletion of inactive users is currently disabled."
  14. )
  15. return
  16. users_deleted = 0
  17. joined_on_cutoff = timezone.now() - timedelta(
  18. days=settings.MISAGO_DELETE_NEW_INACTIVE_USERS_OLDER_THAN_DAYS
  19. )
  20. queryset = UserModel.objects.filter(
  21. requires_activation__gt=UserModel.ACTIVATION_NONE,
  22. joined_on__lt=joined_on_cutoff,
  23. )
  24. for user in chunk_queryset(queryset):
  25. user.delete()
  26. users_deleted += 1
  27. self.stdout.write("Deleted users: %s" % users_deleted)