Commit 3c6f1493 authored by I.K Seneviratne's avatar I.K Seneviratne

Committing the modifications of saving student video with proper serilization validation.

parent 903fb6f8
...@@ -165,13 +165,17 @@ class LectureVideoViewSet(APIView): ...@@ -165,13 +165,17 @@ class LectureVideoViewSet(APIView):
# #
# }, status=status.HTTP_201_CREATED) # }, status=status.HTTP_201_CREATED)
# serializer = LectureVideoSerializer(data=request.data, many=True)
serializer = LectureVideoSerializer(data=request.data) serializer = LectureVideoSerializer(data=request.data)
# serializer.create(validated_data=request.data)
if serializer.is_valid(raise_exception=ValueError):
print('valid')
serializer.create(validated_data=request.data) serializer.create(validated_data=request.data)
return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_201_CREATED)
# if serializer.is_valid(raise_exception=ValueError):
# serializer.create(validated_data=request.data)
# return Response(serializer.error_messages, # return Response(serializer.error_messages,
# status=status.HTTP_400_BAD_REQUEST) # status=status.HTTP_400_BAD_REQUEST)
......
...@@ -1007,6 +1007,7 @@ def get_gaze_correlations(individual_lec_gaze, lec_recorded_activity_data): ...@@ -1007,6 +1007,7 @@ def get_gaze_correlations(individual_lec_gaze, lec_recorded_activity_data):
# this variable will be used to store the correlations # this variable will be used to store the correlations
correlations = [] correlations = []
# limit = 10 # limit = 10
limit = len(individual_lec_gaze) limit = len(individual_lec_gaze)
......
...@@ -194,6 +194,47 @@ class LectureVideoSerializer(serializers.ModelSerializer): ...@@ -194,6 +194,47 @@ class LectureVideoSerializer(serializers.ModelSerializer):
model = LectureVideo model = LectureVideo
fields = '__all__' fields = '__all__'
# this method will validate the input data
def to_internal_value(self, data):
lecturer = None
subject = None
lecturer_data = data.get('lecturer')
subject_data = data.get('subject')
# serialize the lecturer data
lecturer = Lecturer.objects.filter(id=lecturer_data)
subject = Subject.objects.filter(id=subject_data)
lecturer_ser_data = LecturerSerializer(lecturer, many=True).data[0]
subject_ser_data = SubjectSerializer(subject, many=True).data[0]
# retrieve the last lecture video details
last_lec_video = LectureVideo.objects.order_by('lecture_video_id').last()
# create the next lecture video id
new_lecture_video_id = ig.generate_new_id(last_lec_video.lecture_video_id)
# if both subject and lecturer details are available
if len(lecturer) == 1 & len(subject) == 1:
str_video_length = data.get('video_length')
video_length_parts = str_video_length.split(':')
video_length = datetime.timedelta(minutes=int(video_length_parts[0]), seconds=int(video_length_parts[1]),
milliseconds=int(video_length_parts[2]))
# this data will be passed as validated data
validated_data = {
'lecture_video_id': new_lecture_video_id,
'lecturer': lecturer_ser_data,
'subject': subject_ser_data,
'date': data.get('date'),
'video_name': data.get('video_name'),
'video_length': video_length
}
return super(LectureVideoSerializer, self).to_internal_value(validated_data)
# this method will override the 'create' method # this method will override the 'create' method
...@@ -202,6 +243,8 @@ class LectureVideoSerializer(serializers.ModelSerializer): ...@@ -202,6 +243,8 @@ class LectureVideoSerializer(serializers.ModelSerializer):
lecturer = None lecturer = None
subject = None subject = None
lecturer_data = validated_data.pop('lecturer') lecturer_data = validated_data.pop('lecturer')
subject_data = validated_data.pop('subject') subject_data = validated_data.pop('subject')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment