diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index ae6dd88f8bf0..fffd70d326d0 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -106,7 +106,9 @@ from lms.djangoapps.instructor_task.api_helper import AlreadyRunningError, QueueConnectionError from lms.djangoapps.instructor_task.data import InstructorTaskTypes from lms.djangoapps.instructor_task.models import ReportStore -from lms.djangoapps.instructor.views.serializer import RoleNameSerializer, UserSerializer +from lms.djangoapps.instructor.views.serializer import ( + RoleNameSerializer, UserSerializer, ShowStudentExtensionSerializer +) 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 from openedx.core.djangoapps.course_groups.models import CourseUserGroup @@ -2961,20 +2963,6 @@ def show_unit_extensions(request, course_id): return JsonResponse(dump_block_extensions(course, unit)) -class ShowStudentExtensionSerializer(serializers.Serializer): - """ - Serializer for validating and processing the student identifier. - """ - student = serializers.CharField(write_only=True, required=True) - def validate(self, data): - """ - Validates the student identifier and converts it to a student object. - """ - student_identifier = data.get('student') - data['student'] = require_student_from_identifier(student_identifier) - return data - - @method_decorator(cache_control(no_cache=True, no_store=True, must_revalidate=True), name='dispatch') # @method_decorator(handle_dashboard_error) class ShowStudentExtensions(APIView): @@ -2982,11 +2970,6 @@ class ShowStudentExtensions(APIView): Shows all of the due date extensions granted to a particular student in a particular course. """ - authentication_classes = ( - JwtAuthentication, - BearerAuthenticationAllowInactiveUser, - SessionAuthenticationAllowInactiveUser, - ) permission_classes = (IsAuthenticated, permissions.InstructorPermission) serializer_class = ShowStudentExtensionSerializer permission_name = permissions.GIVE_STUDENT_EXTENSION @@ -2998,6 +2981,7 @@ def post(self, request, course_id): 'student': request.data.get('student') } serializer = self.serializer_class(data=data) + serializer.is_valid(raise_exception=True) student = serializer.validated_data['student'] course = get_course_by_id(CourseKey.from_string(course_id)) diff --git a/lms/djangoapps/instructor/views/serializer.py b/lms/djangoapps/instructor/views/serializer.py index b4f6f7626013..6b47a6d06cd0 100644 --- a/lms/djangoapps/instructor/views/serializer.py +++ b/lms/djangoapps/instructor/views/serializer.py @@ -7,6 +7,8 @@ from lms.djangoapps.instructor.access import ROLES +from .tools import get_student_from_identifier + class RoleNameSerializer(serializers.Serializer): # pylint: disable=abstract-method """ @@ -28,3 +30,24 @@ class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ['username', 'email', 'first_name', 'last_name'] + + +class ShowStudentExtensionSerializer(serializers.Serializer): + """ + Serializer for validating and processing the student identifier. + """ + student = serializers.CharField(write_only=True, required=True) + def validate_student(self, value): + """ + Validate that the student corresponds to an existing user. + """ + try: + user = get_student_from_identifier(value) + except User.DoesNotExist: + response_payload = { + 'student': value, + 'userDoesNotExist': True, + } + return response_payload + + return user