Merge pull request 'Added command to prune users who never logged in' (#80) from Danhia/website:management/prune-users into main

Well maybe it will remove all users from the db but we can't know until we try it, can we ?
This commit is contained in:
Danhia 2022-09-30 16:24:18 +02:00
commit 73cf94515d
1 changed files with 24 additions and 0 deletions

View File

@ -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.")