diff --git a/lms/djangoapps/verify_student/services.py b/lms/djangoapps/verify_student/services.py index 7059102152..06acd68eaf 100644 --- a/lms/djangoapps/verify_student/services.py +++ b/lms/djangoapps/verify_student/services.py @@ -116,18 +116,11 @@ class IDVerificationService: 'status__in': statuses, } - sort_by = 'updated_at' + photo_id_verifications = SoftwareSecurePhotoVerification.objects.filter(**filter_kwargs) + sso_id_verifications = SSOVerification.objects.filter(**filter_kwargs) + manual_id_verifications = ManualVerification.objects.filter(**filter_kwargs) - photo_id_verifications = SoftwareSecurePhotoVerification.objects.filter(**filter_kwargs).order_by('-' + sort_by) - sso_id_verifications = SSOVerification.objects.filter(**filter_kwargs).order_by('-' + sort_by) - manual_id_verifications = ManualVerification.objects.filter(**filter_kwargs).order_by('-' + sort_by) - - attempt = most_recent_verification( - photo_id_verifications, - sso_id_verifications, - manual_id_verifications, - sort_by - ) + attempt = most_recent_verification((photo_id_verifications, sso_id_verifications, manual_id_verifications)) return attempt and attempt.expiration_datetime @classmethod diff --git a/lms/djangoapps/verify_student/tests/test_utils.py b/lms/djangoapps/verify_student/tests/test_utils.py index b3cb348e99..b2f0edf334 100644 --- a/lms/djangoapps/verify_student/tests/test_utils.py +++ b/lms/djangoapps/verify_student/tests/test_utils.py @@ -129,14 +129,16 @@ class TestVerifyStudentUtils(unittest.TestCase): sso_verification = SSOVerification.objects.create(user=user) photo_verification = SoftwareSecurePhotoVerification.objects.create(user=user) else: + #last first = manual expected = photo case adds some duplicates for complexity + ManualVerification.objects.create(user=user) + SoftwareSecurePhotoVerification.objects.create(user=user) manual_verification = ManualVerification.objects.create(user=user) photo_verification = SoftwareSecurePhotoVerification.objects.create(user=user) most_recent = most_recent_verification( - SoftwareSecurePhotoVerification.objects.all(), - SSOVerification.objects.all(), - ManualVerification.objects.all(), - 'created_at' + (SoftwareSecurePhotoVerification.objects.all(), + SSOVerification.objects.all(), + ManualVerification.objects.all()) ) if not expected_verification: diff --git a/lms/djangoapps/verify_student/utils.py b/lms/djangoapps/verify_student/utils.py index 41d4f1ec4b..3dcde96d92 100644 --- a/lms/djangoapps/verify_student/utils.py +++ b/lms/djangoapps/verify_student/utils.py @@ -105,35 +105,23 @@ def verification_for_datetime(deadline, candidates): return None -def most_recent_verification(photo_id_verifications, sso_id_verifications, manual_id_verifications, most_recent_key): +def most_recent_verification(verification_sets): """ - Return the most recent verification given querysets for photo, sso and manual verifications. - - This function creates a map of the latest verification of all types and then returns the earliest - verification using the max of the map values. + Return the most recent verification (by updated date) given querysets for multiple types of verifications. + Photo, sso and manual are the current use. Arguments: - photo_id_verifications: Queryset containing photo verifications - sso_id_verifications: Queryset containing sso verifications - manual_id_verifications: Queryset containing manual verifications - most_recent_key: Either 'updated_at' or 'created_at' + tuple or other iterable of verification sets Returns: The most recent verification. """ - photo_id_verification = photo_id_verifications and photo_id_verifications.first() - sso_id_verification = sso_id_verifications and sso_id_verifications.first() - manual_id_verification = manual_id_verifications and manual_id_verifications.first() - - verifications = [photo_id_verification, sso_id_verification, manual_id_verification] - - verifications_map = { - verification: getattr(verification, most_recent_key) - for verification in verifications - if getattr(verification, most_recent_key, False) - } - - return max(verifications_map, key=lambda k: verifications_map[k]) if verifications_map else None + most_recent = None + for s in verification_sets: + for v in s: + if not most_recent or v.updated_at > most_recent.updated_at: + most_recent = v + return most_recent def auto_verify_for_testing_enabled(override=None):