Skip to content

Commit

Permalink
feat: upgrading simple api to drf compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
awais786 committed Aug 12, 2024
1 parent a853ea8 commit 488c9cb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
24 changes: 4 additions & 20 deletions lms/djangoapps/instructor/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2961,32 +2963,13 @@ 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):
"""
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
Expand All @@ -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))
Expand Down
23 changes: 23 additions & 0 deletions lms/djangoapps/instructor/views/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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

0 comments on commit 488c9cb

Please sign in to comment.