From 94237bd9b61b575c74b678639936dd17ead67e16 Mon Sep 17 00:00:00 2001 From: Danhia Date: Fri, 30 Sep 2022 16:20:17 +0200 Subject: [PATCH] Added command to prune users who never logged in --- .../management/commands/prune_users.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/accounts/management/commands/prune_users.py diff --git a/src/accounts/management/commands/prune_users.py b/src/accounts/management/commands/prune_users.py new file mode 100644 index 0000000..6fcdace --- /dev/null +++ b/src/accounts/management/commands/prune_users.py @@ -0,0 +1,24 @@ +from collections import defaultdict +from django.core.management.base import BaseCommand, CommandError +from accounts import models as acc_models +from django.contrib.auth import models as auth_models +from django.contrib.auth.models import timezone +from datetime import timedelta + +class Command(BaseCommand): + help = 'Remove all users who never logged in' + + def handle(self, *args, **options): + all_users = acc_models.UserProfileInfo.objects.filter(score=0).select_related() + to_remove = [] + for elem in all_users: + user = elem.user + if user.last_login is None and user.date_joined < timezone.now() - timedelta(hours=72): + to_remove.append(user) + print("You are going to remove {} users.".format(len(to_remove))) + answer = input("Continue ? [y/N] ") + + if answer.lower() in ["y","yes"]: + for elem in to_remove: + elem.delete() + print("Users have been successfully pruned.")