Skip to content

Commit

Permalink
feat!: upgrading mark_student_can_skip_entrance_exam api with DRF ( 2…
Browse files Browse the repository at this point in the history
…1 ) (#35460)

* feat!: upgrading simple api with DRF.
  • Loading branch information
awais786 authored Oct 2, 2024
1 parent 38cddab commit 8c978c2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
9 changes: 9 additions & 0 deletions lms/djangoapps/instructor/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3455,6 +3455,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
Expand Down
48 changes: 30 additions & 18 deletions lms/djangoapps/instructor/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3203,28 +3203,40 @@ 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):
@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.
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)
permission_classes = (IsAuthenticated, permissions.InstructorPermission)
permission_name = permissions.ALLOW_STUDENT_TO_BYPASS_ENTRANCE_EXAM

__, 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(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
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/instructor/views/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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.ListInstructorTasks.as_view(), name='list_instructor_tasks'),
path('list_background_email_tasks', api.list_background_email_tasks, name='list_background_email_tasks'),
Expand Down

0 comments on commit 8c978c2

Please sign in to comment.