From eb13f2f5db1c74f0ab3d94ce844c3672c2a2a4e0 Mon Sep 17 00:00:00 2001 From: awais qureshi Date: Tue, 10 Sep 2024 15:23:33 +0500 Subject: [PATCH 1/3] feat!: upgrading simple api with DRF. --- lms/djangoapps/instructor/tests/test_api.py | 9 +++++ lms/djangoapps/instructor/views/api.py | 38 ++++++++++++++++++- lms/djangoapps/instructor/views/api_urls.py | 2 +- lms/djangoapps/instructor/views/serializer.py | 28 ++++++++------ 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/lms/djangoapps/instructor/tests/test_api.py b/lms/djangoapps/instructor/tests/test_api.py index 65d35221d4f5..c9c34faf302f 100644 --- a/lms/djangoapps/instructor/tests/test_api.py +++ b/lms/djangoapps/instructor/tests/test_api.py @@ -3449,6 +3449,15 @@ def test_list_entrance_exam_instructor_with_invalid_exam_key(self): }) assert response.status_code == 400 + def test_skip_entrance_exam_student_with_invalid_student(self): + """ Test skip entrance exam api for non existing user. """ + # create a re-score entrance exam task + url = reverse('mark_student_can_skip_entrance_exam', kwargs={'course_id': str(self.course.id)}) + response = self.client.post(url, { + 'unique_student_identifier': 'test', + }) + assert response.status_code == 400 + def test_skip_entrance_exam_student(self): """ Test skip entrance exam api for student. """ # create a re-score entrance exam task diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index c6db96b3ca60..6634f6734c1b 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -108,7 +108,7 @@ from lms.djangoapps.instructor_task.models import ReportStore from lms.djangoapps.instructor.views.serializer import ( AccessSerializer, RoleNameSerializer, ShowStudentExtensionSerializer, - UserSerializer, SendEmailSerializer + UserSerializer, SendEmailSerializer, UniqueStudentIdentifierSerializer ) from openedx.core.djangoapps.content.course_overviews.models import CourseOverview from openedx.core.djangoapps.course_groups.cohorts import add_user_to_cohort, is_course_cohorted @@ -3153,6 +3153,42 @@ def mark_student_can_skip_entrance_exam(request, course_id): return JsonResponse(response_payload) +@method_decorator(cache_control(no_cache=True, no_store=True, must_revalidate=True), name='dispatch') +class MarkStudentCanSkipEntranceExam(APIView): + """ + Mark a student to skip entrance exam. + """ + permission_classes = (IsAuthenticated, permissions.InstructorPermission) + permission_name = permissions.ALLOW_STUDENT_TO_BYPASS_ENTRANCE_EXAM + + @method_decorator(ensure_csrf_cookie) + def post(self, request, course_id): + """ + Takes `unique_student_identifier` as required POST parameter. + """ + course_id = CourseKey.from_string(course_id) + student_identifier = request.data.get("unique_student_identifier") + + serializer_data = UniqueStudentIdentifierSerializer(data=request.data) + if not serializer_data.is_valid(): + return HttpResponseBadRequest(reason=serializer_data.errors) + + student = serializer_data.validated_data.get('unique_student_identifier') + if not student: + response_payload = f'Could not find student matching : {student_identifier}' + return JsonResponse({'error': response_payload}, status=400) + + __, created = EntranceExamConfiguration.objects.get_or_create(user=student, course_id=course_id) + if created: + message = _('This student (%s) will skip the entrance exam.') % student_identifier + else: + message = _('This student (%s) is already allowed to skip the entrance exam.') % student_identifier + response_payload = { + 'message': message, + } + return JsonResponse(response_payload) + + @transaction.non_atomic_requests @ensure_csrf_cookie @cache_control(no_cache=True, no_store=True, must_revalidate=True) diff --git a/lms/djangoapps/instructor/views/api_urls.py b/lms/djangoapps/instructor/views/api_urls.py index d4719b90b4b0..a98c4020557f 100644 --- a/lms/djangoapps/instructor/views/api_urls.py +++ b/lms/djangoapps/instructor/views/api_urls.py @@ -42,7 +42,7 @@ path('rescore_entrance_exam', api.rescore_entrance_exam, name='rescore_entrance_exam'), path('list_entrance_exam_instructor_tasks', api.ListEntranceExamInstructorTasks.as_view(), name='list_entrance_exam_instructor_tasks'), - path('mark_student_can_skip_entrance_exam', api.mark_student_can_skip_entrance_exam, + path('mark_student_can_skip_entrance_exam', api.MarkStudentCanSkipEntranceExam.as_view(), name='mark_student_can_skip_entrance_exam'), path('list_instructor_tasks', api.list_instructor_tasks, name='list_instructor_tasks'), path('list_background_email_tasks', api.list_background_email_tasks, name='list_background_email_tasks'), diff --git a/lms/djangoapps/instructor/views/serializer.py b/lms/djangoapps/instructor/views/serializer.py index ff79124d026d..06bb6f078fc0 100644 --- a/lms/djangoapps/instructor/views/serializer.py +++ b/lms/djangoapps/instructor/views/serializer.py @@ -31,23 +31,14 @@ class Meta: fields = ['username', 'email', 'first_name', 'last_name'] -class AccessSerializer(serializers.Serializer): +class UniqueStudentIdentifierSerializer(serializers.Serializer): """ - Serializer for managing user access changes. - This serializer validates and processes the data required to modify - user access within a system. + Serializer for identifying unique_student. """ unique_student_identifier = serializers.CharField( max_length=255, help_text="Email or username of user to change access" ) - rolename = serializers.CharField( - help_text="Role name to assign to the user" - ) - action = serializers.ChoiceField( - choices=['allow', 'revoke'], - help_text="Action to perform on the user's access" - ) def validate_unique_student_identifier(self, value): """ @@ -61,6 +52,21 @@ def validate_unique_student_identifier(self, value): return user +class AccessSerializer(UniqueStudentIdentifierSerializer): + """ + Serializer for managing user access changes. + This serializer validates and processes the data required to modify + user access within a system. + """ + rolename = serializers.CharField( + help_text="Role name to assign to the user" + ) + action = serializers.ChoiceField( + choices=['allow', 'revoke'], + help_text="Action to perform on the user's access" + ) + + class ShowStudentExtensionSerializer(serializers.Serializer): """ Serializer for validating and processing the student identifier. From eb6ae224ad9b605f032243a4920ac1b5b4200fc8 Mon Sep 17 00:00:00 2001 From: awais qureshi Date: Fri, 20 Sep 2024 12:50:39 +0500 Subject: [PATCH 2/3] feat!: fixing quality. --- lms/djangoapps/instructor/views/api.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index 488cefa8d110..0bb2a4a8da9b 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -3141,30 +3141,6 @@ def enable_certificate_generation(request, course_id=None): return redirect(_instructor_dash_url(course_key, section='certificates')) -@ensure_csrf_cookie -@cache_control(no_cache=True, no_store=True, must_revalidate=True) -@require_course_permission(permissions.ALLOW_STUDENT_TO_BYPASS_ENTRANCE_EXAM) -@require_POST -def mark_student_can_skip_entrance_exam(request, course_id): - """ - Mark a student to skip entrance exam. - Takes `unique_student_identifier` as required POST parameter. - """ - course_id = CourseKey.from_string(course_id) - student_identifier = request.POST.get('unique_student_identifier') - student = get_student_from_identifier(student_identifier) - - __, created = EntranceExamConfiguration.objects.get_or_create(user=student, course_id=course_id) - if created: - message = _('This student (%s) will skip the entrance exam.') % student_identifier - else: - message = _('This student (%s) is already allowed to skip the entrance exam.') % student_identifier - response_payload = { - 'message': message, - } - return JsonResponse(response_payload) - - @method_decorator(cache_control(no_cache=True, no_store=True, must_revalidate=True), name='dispatch') class MarkStudentCanSkipEntranceExam(APIView): """ From 8708c516de9781cb6f3899a596bf7614ff7e762d Mon Sep 17 00:00:00 2001 From: Awais Qureshi Date: Fri, 20 Sep 2024 23:30:35 +0500 Subject: [PATCH 3/3] chore: Update serializer.py --- lms/djangoapps/instructor/views/serializer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lms/djangoapps/instructor/views/serializer.py b/lms/djangoapps/instructor/views/serializer.py index 28b25561b5f5..59ac66ab838b 100644 --- a/lms/djangoapps/instructor/views/serializer.py +++ b/lms/djangoapps/instructor/views/serializer.py @@ -64,7 +64,7 @@ class AccessSerializer(UniqueStudentIdentifierSerializer): action = serializers.ChoiceField( choices=['allow', 'revoke'], help_text="Action to perform on the user's access" - ) + ) class ListInstructorTaskInputSerializer(serializers.Serializer): # pylint: disable=abstract-method