From f3d17018580c0b2289f92c253a6b2fc9cd79a966 Mon Sep 17 00:00:00 2001 From: Haydn Date: Mon, 7 Oct 2024 23:56:35 -0400 Subject: [PATCH 1/3] Update to handle 500 server errors when roles not found --- manage_people/views.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/manage_people/views.py b/manage_people/views.py index 6d194db..217ae08 100644 --- a/manage_people/views.py +++ b/manage_people/views.py @@ -138,7 +138,7 @@ def results_list(request): # Find all users enrolled in the course according to Canvas with roles that # are deletable via Manage People. available_roles = get_available_roles(course_instance_id) - enrolled_roles_by_id = get_enrolled_roles_for_user_ids( + enrolled_roles_by_id, error_msgs = get_enrolled_roles_for_user_ids( canvas_course_id, list(search_results.keys())) user_ids_with_enrollments = list(enrolled_roles_by_id.keys()) @@ -160,6 +160,7 @@ def results_list(request): if user_id not in user_ids_with_enrollments } + error_messages.extend(error_msgs) return render(request, 'manage_people/results_list.html', { 'available_roles': available_roles, 'enrolled_roles_by_id': dict(enrolled_roles_by_id), # templates don't @@ -180,6 +181,8 @@ def get_enrolled_roles_for_user_ids(canvas_course_id, search_results_user_ids): are already enrolled in the course and to display Canvas role names. Do not match XIDs. """ + error_messages = [] + t0 = time.perf_counter() logger.debug(f'search_results_user_ids: {search_results_user_ids}') canvas_enrollments = [] @@ -204,18 +207,19 @@ def get_enrolled_roles_for_user_ids(canvas_course_id, search_results_user_ids): for enrollment in canvas_enrollments: try: sis_user_id = enrollment['user']['sis_user_id'] - except KeyError: - continue - else: if sis_user_id in search_results_user_ids: enrollment.update( {'canvas_role_label': canvas_roles_by_role_id[ enrollment['role_id']]['label']}) found_ids[sis_user_id].append(enrollment) + except KeyError as e: + logger.exception(f'Unable to retrieve role id {enrollment.get("role_id")} for sis_user_id {enrollment.get("user", {}).get("sis_user_id")} from the Canvas role list.', + extra={'enrollment': enrollment}) + error_messages.append(f'Some roles could not be retrieved for one or more users from the Canvas role list.') t3 = time.perf_counter() logger.debug(f'*** TIMING getting role labels took {t3 - t2} seconds') - return found_ids + return found_ids, error_messages def find_person(search_term): t0 = time.perf_counter() From 775ad493fc4415decfffeca7cde630e9b33e1515 Mon Sep 17 00:00:00 2001 From: Haydn Date: Tue, 8 Oct 2024 00:16:32 -0400 Subject: [PATCH 2/3] Update error message --- manage_people/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manage_people/views.py b/manage_people/views.py index 217ae08..636b22c 100644 --- a/manage_people/views.py +++ b/manage_people/views.py @@ -215,7 +215,7 @@ def get_enrolled_roles_for_user_ids(canvas_course_id, search_results_user_ids): except KeyError as e: logger.exception(f'Unable to retrieve role id {enrollment.get("role_id")} for sis_user_id {enrollment.get("user", {}).get("sis_user_id")} from the Canvas role list.', extra={'enrollment': enrollment}) - error_messages.append(f'Some roles could not be retrieved for one or more users from the Canvas role list.') + error_messages.append(f'One or more roles could not be retrieved for the user from the Canvas role list.') t3 = time.perf_counter() logger.debug(f'*** TIMING getting role labels took {t3 - t2} seconds') From 0d2be0376157979baa29d141b91df330f122bace Mon Sep 17 00:00:00 2001 From: Haydn Date: Tue, 8 Oct 2024 14:50:07 -0400 Subject: [PATCH 3/3] Refactor to handle 500 error and display proper message --- manage_people/views.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/manage_people/views.py b/manage_people/views.py index 636b22c..aea40af 100644 --- a/manage_people/views.py +++ b/manage_people/views.py @@ -206,15 +206,15 @@ def get_enrolled_roles_for_user_ids(canvas_course_id, search_results_user_ids): found_ids = defaultdict(list) for enrollment in canvas_enrollments: try: + enrollment_role = UserRole.objects.get(canvas_role_id=enrollment['role_id']) + sis_user_id = enrollment['user']['sis_user_id'] if sis_user_id in search_results_user_ids: enrollment.update( - {'canvas_role_label': canvas_roles_by_role_id[ - enrollment['role_id']]['label']}) + {'canvas_role_label': enrollment_role.role_name}) found_ids[sis_user_id].append(enrollment) - except KeyError as e: - logger.exception(f'Unable to retrieve role id {enrollment.get("role_id")} for sis_user_id {enrollment.get("user", {}).get("sis_user_id")} from the Canvas role list.', - extra={'enrollment': enrollment}) + except UserRole.DoesNotExist: + logger.exception(f'Error: Canvas role id {enrollment["role_id"]} does not exist in the UserRole table.') error_messages.append(f'One or more roles could not be retrieved for the user from the Canvas role list.') t3 = time.perf_counter()