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 @@ ...@@ -27,85 +27,109 @@
############################################################################### ###############################################################################
from django.core.management.base import BaseCommand, CommandError import sys
from datetime import date
import datetime import datetime
from django.contrib.auth.models import User from django.core.management.base import BaseCommand
from django.conf import settings from django.conf import settings
from ....ui.registration.models import RegistrationProfile
from ...models import SupervisionTrack from ...models import SupervisionTrack
from ...models import Profile from ...models import Profile
from ....ui.registration.models import RegistrationProfile
import sys
import random
class Command(BaseCommand): class Command(BaseCommand):
help = 'Cleanup outdated invalid users' help = "Cleanup outdated invalid users"
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('--noinput', action='store_false', dest='interactive', default=False, parser.add_argument(
help=('Tells Django to NOT prompt the user for input of any kind.')) "--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): def handle(self, *args, **options):
clean = True
if options['interactive']: if options["interactive"]:
try: 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: except KeyboardInterrupt:
self.stderr.write("\nOperation canceled.") self.stderr.write("\nOperation canceled.")
sys.exit(1) sys.exit(1)
if answer != 'y': if answer != "y":
self.stdout.write('Cleanup canceled') self.stdout.write("Cleanup canceled")
sys.exit(1) sys.exit(0)
if clean: invalid_userprofiles_new_users = Profile.objects.filter(status=Profile.NEWUSER)
invalid_userprofiles_new_users = Profile.objects.filter(status=Profile.NEWUSER) invalid_userprofiles_waiting_validation = Profile.objects.filter(
invalid_userprofiles_waiting_validation = Profile.objects.filter(status=Profile.WAITINGVALIDATION) status=Profile.WAITINGVALIDATION
count = 0 )
for invalid_profile in invalid_userprofiles_new_users: count = 0
user = invalid_profile.user expiration_date_delta_new_users = datetime.timedelta(
supervisiontrack = SupervisionTrack.objects.get(supervision_key=invalid_profile.supervision_key) days=settings.ACCOUNT_ACTIVATION_DAYS
registration_profile = RegistrationProfile.objects.get(user=invalid_profile.user) )
expiration_date_delta_waiting_validation = datetime.timedelta(
expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS) days=settings.ACCOUNT_ACTIVATION_DAYS_FROM_SUPERVISOR
if user.profile.registration_date + expiration_date <= datetime.datetime.now(): )
count += 1
user.delete() count += self.check_invalid_profiles(
invalid_profile.delete() invalid_userprofiles_new_users, expiration_date_delta_new_users
supervisiontrack.delete() )
registration_profile.delete() count += self.check_invalid_profiles(
invalid_userprofiles_waiting_validation,
for invalid_profile in invalid_userprofiles_waiting_validation: expiration_date_delta_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) self.stdout.write(
"{} Invalid user(s) successfully cleaned/".format(count)
expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS_FROM_SUPERVISOR) + "{} Total user(s) checked".format(
if user.profile.registration_date + expiration_date <= datetime.datetime.now(): invalid_userprofiles_new_users.count()
count += 1 + invalid_userprofiles_waiting_validation.count()
user.delete() )
invalid_profile.delete() )
supervisiontrack.delete()
registration_profile.delete() def check_invalid_profiles(self, invalid_profiles_list, expiration_date_delta):
"""
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())) 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): def get_input_data(self, message, default=None):
""" """
Override this method if you want to customize data inputs or Override this method if you want to customize data inputs or
validation exceptions. validation exceptions.
""" """
raw_value = raw_input(message) raw_value = input(message)
if default and raw_value == '': if default and raw_value == "":
raw_value = default raw_value = default
return raw_value 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