From a4b338febc800a14e719b0d51b09d45dda7e041d Mon Sep 17 00:00:00 2001 From: Thomas Tracy Date: Tue, 13 Apr 2021 11:02:56 -0400 Subject: [PATCH] refactor: reformat the manual verifications command to allow for single user verification (#27305) * refactor: reformat the manual verifications command to allow for single user verifications * reformat error message * linting * linting: add doc string * whitespace --- .../commands/manual_verifications.py | 53 ++++++++++++++----- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/lms/djangoapps/verify_student/management/commands/manual_verifications.py b/lms/djangoapps/verify_student/management/commands/manual_verifications.py index 5cc3a78ea7..603d654a24 100644 --- a/lms/djangoapps/verify_student/management/commands/manual_verifications.py +++ b/lms/djangoapps/verify_student/management/commands/manual_verifications.py @@ -32,11 +32,24 @@ class Command(BaseCommand): default=None, help='Path of the file to read email id from.', type=str, - required=True + ) + parser.add_argument( + '--email', + default=None, + help='Single email to verify one user', + type=str, ) def handle(self, *args, **options): + single_email = options['email'] + + if single_email: + successfully_verified = self._add_user_to_manual_verification(single_email) + if successfully_verified is False: + log.error(f'Manual verification of {single_email} failed') + return + email_ids_file = options['email_ids_file'] if email_ids_file: @@ -72,17 +85,31 @@ class Command(BaseCommand): total_emails = len(email_ids) log.info(f'Creating manual verification for {total_emails} emails.') for email_id in email_ids: - try: - email_id = email_id.strip() - user = User.objects.get(email=email_id) - ManualVerification.objects.get_or_create( - user=user, - status='approved', - created_at__gte=earliest_allowed_verification_date(), - defaults={'name': user.profile.name}, - ) - except User.DoesNotExist: + successfully_verified = self._add_user_to_manual_verification(email_id) + if successfully_verified is False: failed_emails.append(email_id) - err_msg = 'Tried to verify email {}, but user not found' - log.error(err_msg.format(email_id)) return total_emails, failed_emails + + def _add_user_to_manual_verification(self, email_id): + """ + Generates a single verification for a user. + + Arguments: + email_id (str): email of the user to be verified + + Returns: + (success): boolean to show if the user has been successfully verified. + """ + try: + email_id = email_id.strip() + user = User.objects.get(email=email_id) + ManualVerification.objects.get_or_create( + user=user, + status='approved', + created_at__gte=earliest_allowed_verification_date(), + defaults={'name': user.profile.name}, + ) + return True + except User.DoesNotExist: + log.error(f'Tried to verify email {email_id}, but user not found') + return False