Skip to content

Commit

Permalink
Merge pull request #115 from Harvard-University-iCommons/task/haydn90…
Browse files Browse the repository at this point in the history
…00/tlt-4803/handle-500-server-error

Update to handle 500 server errors when roles not found
  • Loading branch information
haydn9000 authored Oct 17, 2024
2 parents 87c5443 + 0d2be03 commit 5beb829
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions manage_people/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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
Expand All @@ -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 = []
Expand All @@ -203,19 +206,20 @@ 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']
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']})
{'canvas_role_label': enrollment_role.role_name})
found_ids[sis_user_id].append(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()
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()
Expand Down

0 comments on commit 5beb829

Please sign in to comment.