diff --git a/beat/web/accounts/management/commands/clean_invalid_users.py b/beat/web/accounts/management/commands/clean_invalid_users.py index 2f47b8a3f127c1a1b665c4b3a298bae818389a9d..f698b37f96919145bbc9502854121b4841cdcf6a 100644 --- a/beat/web/accounts/management/commands/clean_invalid_users.py +++ b/beat/web/accounts/management/commands/clean_invalid_users.py @@ -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