Commit 6d6ed06e authored by Flavio TARSETTI's avatar Flavio TARSETTI Committed by Samuel GAIST

[accounts] patch the cleaning of invalid users

Supervision key does not exist in Supervision tracks table for
new users and waiting validation users.

Fixes #539
parent 959035a9
......@@ -27,85 +27,109 @@
###############################################################################
from django.core.management.base import BaseCommand, CommandError
from datetime import date
import sys
import datetime
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from django.conf import settings
from ....ui.registration.models import RegistrationProfile
from ...models import SupervisionTrack
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 clean:
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
supervisiontrack = SupervisionTrack.objects.get(supervision_key=invalid_profile.supervision_key)
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()
supervisiontrack.delete()
registration_profile.delete()
for invalid_profile in invalid_userprofiles_waiting_validation:
user = invalid_profile.user
supervisiontrack = SupervisionTrack.objects.get(supervision_key=invalid_profile.supervision_key)
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()
supervisiontrack.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()))
if answer != "y":
self.stdout.write("Cleanup canceled")
sys.exit(0)
invalid_userprofiles_new_users = Profile.objects.filter(status=Profile.NEWUSER)
invalid_userprofiles_waiting_validation = Profile.objects.filter(
status=Profile.WAITINGVALIDATION
)
count = 0
expiration_date_delta_new_users = datetime.timedelta(
days=settings.ACCOUNT_ACTIVATION_DAYS
)
expiration_date_delta_waiting_validation = datetime.timedelta(
days=settings.ACCOUNT_ACTIVATION_DAYS_FROM_SUPERVISOR
)
count += self.check_invalid_profiles(
invalid_userprofiles_new_users, expiration_date_delta_new_users
)
count += self.check_invalid_profiles(
invalid_userprofiles_waiting_validation,
expiration_date_delta_waiting_validation,
)
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 check_invalid_profiles(self, invalid_profiles_list, expiration_date_delta):
"""
Cleanup invalid profiles
"""
count_updated_users = 0
for invalid_profile in invalid_profiles_list:
user = invalid_profile.user
registration_profile = RegistrationProfile.objects.get(
user=invalid_profile.user
)
if (
user.profile.registration_date + expiration_date_delta
<= datetime.datetime.now()
):
count_updated_users += 1
user.delete()
invalid_profile.delete()
# User should not have a supervision track - Delete if any
SupervisionTrack.objects.filter(
supervision_key=invalid_profile.supervision_key
).delete()
registration_profile.delete()
return count_updated_users
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