Commit 8d1d579c by David Ormsbee

Bypass REST framework serializer validation for assessment part creation (we…

Bypass REST framework serializer validation for assessment part creation (we validate before anyway).
parent 0dae41a5
...@@ -14,8 +14,8 @@ from django.db import DatabaseError ...@@ -14,8 +14,8 @@ from django.db import DatabaseError
from django.db.models import Q from django.db.models import Q
from openassessment.assessment.models import ( from openassessment.assessment.models import (
Assessment, InvalidOptionSelection, PeerWorkflow, PeerWorkflowItem, Assessment, AssessmentFeedback, AssessmentPart,
AssessmentFeedback InvalidOptionSelection, PeerWorkflow, PeerWorkflowItem,
) )
from openassessment.assessment.serializers import ( from openassessment.assessment.serializers import (
AssessmentSerializer, rubric_from_dict, AssessmentFeedbackSerializer, AssessmentSerializer, rubric_from_dict, AssessmentFeedbackSerializer,
...@@ -183,7 +183,6 @@ def create_assessment( ...@@ -183,7 +183,6 @@ def create_assessment(
"submission_uuid": submission.uuid, "submission_uuid": submission.uuid,
"score_type": PEER_TYPE, "score_type": PEER_TYPE,
"feedback": feedback, "feedback": feedback,
"parts": [{"option": option_id} for option_id in option_ids]
} }
if scored_at is not None: if scored_at is not None:
...@@ -193,8 +192,17 @@ def create_assessment( ...@@ -193,8 +192,17 @@ def create_assessment(
if not peer_serializer.is_valid(): if not peer_serializer.is_valid():
raise PeerAssessmentRequestError(peer_serializer.errors) raise PeerAssessmentRequestError(peer_serializer.errors)
assessment = peer_serializer.save() assessment = peer_serializer.save()
# We do this to do a run around django-rest-framework serializer
# validation, which would otherwise require two DB queries per
# option to do validation. We already validated these options above.
AssessmentPart.objects.bulk_create([
AssessmentPart(assessment=assessment, option_id=option_id)
for option_id in option_ids
])
student_item = submission.student_item student_item = submission.student_item
student_item_dict = StudentItemSerializer(student_item).data student_item_dict = StudentItemSerializer(student_item).data
......
...@@ -9,7 +9,9 @@ from submissions.api import ( ...@@ -9,7 +9,9 @@ from submissions.api import (
from openassessment.assessment.serializers import ( from openassessment.assessment.serializers import (
rubric_from_dict, AssessmentSerializer, full_assessment_dict, InvalidRubric rubric_from_dict, AssessmentSerializer, full_assessment_dict, InvalidRubric
) )
from openassessment.assessment.models import Assessment, InvalidOptionSelection from openassessment.assessment.models import (
Assessment, AssessmentPart, InvalidOptionSelection
)
# Assessments are tagged as "self-evaluation" # Assessments are tagged as "self-evaluation"
...@@ -74,7 +76,6 @@ def create_assessment(submission_uuid, user_id, options_selected, rubric_dict, s ...@@ -74,7 +76,6 @@ def create_assessment(submission_uuid, user_id, options_selected, rubric_dict, s
"submission_uuid": submission_uuid, "submission_uuid": submission_uuid,
"score_type": SELF_TYPE, "score_type": SELF_TYPE,
"feedback": u"", "feedback": u"",
"parts": [{"option": option_id} for option_id in option_ids],
} }
if scored_at is not None: if scored_at is not None:
...@@ -86,7 +87,15 @@ def create_assessment(submission_uuid, user_id, options_selected, rubric_dict, s ...@@ -86,7 +87,15 @@ def create_assessment(submission_uuid, user_id, options_selected, rubric_dict, s
msg = _("Could not create self assessment: {errors}").format(errors=serializer.errors) msg = _("Could not create self assessment: {errors}").format(errors=serializer.errors)
raise SelfAssessmentRequestError(msg) raise SelfAssessmentRequestError(msg)
serializer.save() assessment = serializer.save()
# We do this to do a run around django-rest-framework serializer
# validation, which would otherwise require two DB queries per
# option to do validation. We already validated these options above.
AssessmentPart.objects.bulk_create([
AssessmentPart(assessment=assessment, option_id=option_id)
for option_id in option_ids
])
# Return the serialized assessment # Return the serialized assessment
return serializer.data return serializer.data
......
...@@ -109,7 +109,7 @@ class AssessmentPartSerializer(serializers.ModelSerializer): ...@@ -109,7 +109,7 @@ class AssessmentPartSerializer(serializers.ModelSerializer):
class AssessmentSerializer(serializers.ModelSerializer): class AssessmentSerializer(serializers.ModelSerializer):
"""Serializer for :class:`Assessment`.""" """Serializer for :class:`Assessment`."""
parts = AssessmentPartSerializer(required=True, many=True) parts = AssessmentPartSerializer(many=True, read_only=True)
points_earned = serializers.Field(source='points_earned') points_earned = serializers.Field(source='points_earned')
points_possible = serializers.Field(source='points_possible') points_possible = serializers.Field(source='points_possible')
......
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