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:
commit
73cf94515d
|
@ -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.")
|
Loading…
Reference in New Issue