Commit feab7d86 authored by Flavio TARSETTI's avatar Flavio TARSETTI Committed by Flavio Tarsetti

[accounts] code refactoring for clean_invalid_users command

parent c9682f3c
Pipeline #35986 passed with stage
in 14 minutes and 53 seconds
......@@ -27,12 +27,10 @@
###############################################################################
from django.core.management.base import BaseCommand, CommandError
from django.core.management.base import BaseCommand
from datetime import date
import datetime
from django.contrib.auth.models import User
from django.conf import settings
from ...models import SupervisionTrack
......@@ -40,72 +38,106 @@ from ...models import Profile
from ....ui.registration.models import RegistrationProfile
import sys
import random
class Command(BaseCommand):
help = 'Cleanup outdated invalid users'
help = "Cleanup outdated invalid users"
def add_arguments(self, parser):
parser.add_argument('--noinput', action='store_false', dest='interactive', default=False,
help=('Tells Django to NOT prompt the user for input of any kind.'))
parser.add_argument(
"--noinput",
action="store_false",
dest="interactive",
default=False,
help=("Tells Django to NOT prompt the user for input of any kind."),
)
def handle(self, *args, **options):
clean = True
if options['interactive']:
if options["interactive"]:
try:
answer = self.get_input_data('Delete user(s) that have not been validated by a supervisor? (y/n)? ', 'y').lower()
answer = self.get_input_data(
"Delete user(s) that have not been validated by a supervisor? (y/n)? ",
"y",
).lower()
except KeyboardInterrupt:
self.stderr.write("\nOperation canceled.")
sys.exit(1)
if answer != 'y':
self.stdout.write('Cleanup canceled')
sys.exit(1)
if answer != "y":
self.stdout.write("Cleanup canceled")
sys.exit(0)
if clean:
invalid_userprofiles_new_users = Profile.objects.filter(status=Profile.NEWUSER)
invalid_userprofiles_waiting_validation = Profile.objects.filter(status=Profile.WAITINGVALIDATION)
invalid_userprofiles_new_users = Profile.objects.filter(
status=Profile.NEWUSER
)
invalid_userprofiles_waiting_validation = Profile.objects.filter(
status=Profile.WAITINGVALIDATION
)
count = 0
for invalid_profile in invalid_userprofiles_new_users:
user = invalid_profile.user
registration_profile = RegistrationProfile.objects.get(user=invalid_profile.user)
expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
if user.profile.registration_date + expiration_date <= datetime.datetime.now():
registration_profile = RegistrationProfile.objects.get(
user=invalid_profile.user
)
expiration_date = datetime.timedelta(
days=settings.ACCOUNT_ACTIVATION_DAYS
)
if (
user.profile.registration_date + expiration_date
<= datetime.datetime.now()
):
count += 1
user.delete()
invalid_profile.delete()
# New user should not have a supervision track - Delete if any
SupervisionTrack.objects.filter(supervision_key=invalid_profile.supervision_key).delete()
SupervisionTrack.objects.filter(
supervision_key=invalid_profile.supervision_key
).delete()
registration_profile.delete()
for invalid_profile in invalid_userprofiles_waiting_validation:
user = invalid_profile.user
registration_profile = RegistrationProfile.objects.get(user=invalid_profile.user)
expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS_FROM_SUPERVISOR)
if user.profile.registration_date + expiration_date <= datetime.datetime.now():
registration_profile = RegistrationProfile.objects.get(
user=invalid_profile.user
)
expiration_date = datetime.timedelta(
days=settings.ACCOUNT_ACTIVATION_DAYS_FROM_SUPERVISOR
)
if (
user.profile.registration_date + expiration_date
<= datetime.datetime.now()
):
count += 1
user.delete()
invalid_profile.delete()
# User waiting validation should not have a supervision track - Delete if any
SupervisionTrack.objects.filter(supervision_key=invalid_profile.supervision_key).delete()
SupervisionTrack.objects.filter(
supervision_key=invalid_profile.supervision_key
).delete()
registration_profile.delete()
self.stdout.write('{} Invalid user(s) successfully cleaned/'.format(count) + '{} Total user(s) checked'.format(invalid_userprofiles_new_users.count()+invalid_userprofiles_waiting_validation.count()))
self.stdout.write(
"{} Invalid user(s) successfully cleaned/".format(count)
+ "{} Total user(s) checked".format(
invalid_userprofiles_new_users.count()
+ invalid_userprofiles_waiting_validation.count()
)
)
def get_input_data(self, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = raw_input(message)
raw_value = input(message)
if default and raw_value == '':
if default and raw_value == "":
raw_value = default
return raw_value
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment