From d5e13195b7256a01bab45e4b4b22f819d36f281c Mon Sep 17 00:00:00 2001 From: Asad Iqbal <7334669+asadiqbal08@users.noreply.github.com> Date: Wed, 31 Mar 2021 17:46:55 +0500 Subject: [PATCH] Fix: Find the submission history using learner's email address along with username (#25642) Co-authored-by: asadiqbal Co-authored-by: asadiqbal08 --- lms/djangoapps/courseware/tests/test_views.py | 10 +++---- lms/djangoapps/courseware/views/views.py | 26 +++++++++++++------ lms/templates/courseware/xqa_interface.html | 4 +-- lms/templates/staff_problem_info.html | 4 +-- lms/urls.py | 2 +- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lms/djangoapps/courseware/tests/test_views.py b/lms/djangoapps/courseware/tests/test_views.py index 77f8f3059f..c34c03563a 100644 --- a/lms/djangoapps/courseware/tests/test_views.py +++ b/lms/djangoapps/courseware/tests/test_views.py @@ -612,7 +612,7 @@ class ViewsTestCase(BaseViewsTestCase): url = reverse('submission_history', kwargs={ 'course_id': str(self.course_key), - 'student_username': 'dummy', + 'learner_identifier': 'dummy', 'location': str(self.problem.location), }) response = self.client.get(url) @@ -628,7 +628,7 @@ class ViewsTestCase(BaseViewsTestCase): # try it with an existing user and a malicious location url = reverse('submission_history', kwargs={ 'course_id': str(self.course_key), - 'student_username': 'dummy', + 'learner_identifier': 'dummy', 'location': '' }) response = self.client.get(url) @@ -637,7 +637,7 @@ class ViewsTestCase(BaseViewsTestCase): # try it with a malicious user and a non-existent location url = reverse('submission_history', kwargs={ 'course_id': str(self.course_key), - 'student_username': '', + 'learner_identifier': '', 'location': 'dummy' }) response = self.client.get(url) @@ -670,7 +670,7 @@ class ViewsTestCase(BaseViewsTestCase): url = reverse('submission_history', kwargs={ 'course_id': str(self.course_key), - 'student_username': admin.username, + 'learner_identifier': admin.email, 'location': str(usage_key), }) response = self.client.get(url) @@ -711,7 +711,7 @@ class ViewsTestCase(BaseViewsTestCase): ) url = reverse('submission_history', kwargs={ 'course_id': str(course_key), - 'student_username': admin.username, + 'learner_identifier': admin.username, 'location': str(usage_key), }) response = client.get(url) diff --git a/lms/djangoapps/courseware/views/views.py b/lms/djangoapps/courseware/views/views.py index 638a514ad1..b8d0ed0005 100644 --- a/lms/djangoapps/courseware/views/views.py +++ b/lms/djangoapps/courseware/views/views.py @@ -1340,12 +1340,15 @@ def _course_home_redirect_enabled(): @login_required @ensure_valid_course_key -def submission_history(request, course_id, student_username, location): +def submission_history(request, course_id, learner_identifier, location): """Render an HTML fragment (meant for inclusion elsewhere) that renders a history of all state changes made by this user for this problem location. Right now this only works for problems because that's all StudentModuleHistory records. """ + found_user_name = get_learner_username(learner_identifier) + if not found_user_name: + return HttpResponse(escape(_('User does not exist.'))) course_key = CourseKey.from_string(course_id) @@ -1359,15 +1362,15 @@ def submission_history(request, course_id, student_username, location): # Permission Denied if they don't have staff access and are trying to see # somebody else's submission history. - if (student_username != request.user.username) and (not staff_access): + if (found_user_name != request.user.username) and (not staff_access): raise PermissionDenied user_state_client = DjangoXBlockUserStateClient() try: - history_entries = list(user_state_client.get_history(student_username, usage_key)) + history_entries = list(user_state_client.get_history(found_user_name, usage_key)) except DjangoXBlockUserStateClient.DoesNotExist: - return HttpResponse(escape(_('User {username} has never accessed problem {location}').format( - username=student_username, + return HttpResponse(escape(_(u'User {username} has never accessed problem {location}').format( + username=found_user_name, location=location ))) @@ -1375,7 +1378,7 @@ def submission_history(request, course_id, student_username, location): # the scores instead, it will have to do. csm = StudentModule.objects.filter( module_state_key=usage_key, - student__username=student_username, + student__username=found_user_name, course_id=course_key) scores = BaseStudentModuleHistory.get_history(csm) @@ -1387,7 +1390,7 @@ def submission_history(request, course_id, student_username, location): "%d scores were found, and %d history entries were found. " "Matching scores to history entries by date for display.", course_id, - student_username, + found_user_name, location, len(scores), len(history_entries), @@ -1404,7 +1407,7 @@ def submission_history(request, course_id, student_username, location): context = { 'history_entries': history_entries, 'scores': scores, - 'username': student_username, + 'username': found_user_name, 'location': location, 'course_id': str(course_key) } @@ -2094,3 +2097,10 @@ def get_financial_aid_courses(user): ) return financial_aid_courses + + +def get_learner_username(learner_identifier): + """ Return the username """ + learner = User.objects.filter(Q(username=learner_identifier) | Q(email=learner_identifier)).first() + if learner: + return learner.username diff --git a/lms/templates/courseware/xqa_interface.html b/lms/templates/courseware/xqa_interface.html index 891ea10de3..ade9eae965 100644 --- a/lms/templates/courseware/xqa_interface.html +++ b/lms/templates/courseware/xqa_interface.html @@ -25,11 +25,11 @@ function setup_debug(element_id, edit_link, staff_context){ $('#' + element_id + '_history_form').submit( function () { - var username = $("#" + element_id + "_history_student_username").val(); + var username_or_email = $("#" + element_id + "_history_student_username").val(); var location = $("#" + element_id + "_history_location").val(); // xss-lint: disable=mako-invalid-js-filter $("#" + element_id + "_history_text").load('/courses/' + "${six.text_type(getattr(course,'id','')) | u}" + - "/submission_history/" + username + "/" + location); + "/submission_history/" + username_or_email + "/" + location); return false; } ); diff --git a/lms/templates/staff_problem_info.html b/lms/templates/staff_problem_info.html index 1e24df7edb..a4b7e0825d 100644 --- a/lms/templates/staff_problem_info.html +++ b/lms/templates/staff_problem_info.html @@ -128,8 +128,8 @@ ${block_content | n, decode.utf8}

${_("Submission History Viewer")}

- - + +
diff --git a/lms/urls.py b/lms/urls.py index 35a113ee33..8b047acdee 100644 --- a/lms/urls.py +++ b/lms/urls.py @@ -779,7 +779,7 @@ urlpatterns += [ if settings.FEATURES.get('ENABLE_STUDENT_HISTORY_VIEW'): urlpatterns += [ url( - r'^courses/{}/submission_history/(?P[^/]*)/(?P.*?)$'.format( + r'^courses/{}/submission_history/(?P[^/]*)/(?P.*?)$'.format( settings.COURSE_ID_PATTERN ), courseware_views.submission_history,