TestResultView post
This commit is contained in:
1 parent
254994b929
commit
fdb1deb97f
2 files changed
+49
-16
No files matched your search
@@ -93,7 +93,14 @@ class TestAttemptSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
class TestResultSerializer(serializers.ModelSerializer):
|
class TestResultSerializer(serializers.ModelSerializer):
|
||||||
attempt = TestAttemptSerializer(read_only=True)
|
attempt = TestAttemptSerializer(read_only=True)
|
||||||
answer = serializers.PrimaryKeyRelatedField(queryset=TestAnswer.objects.all(), many=False)
|
question = serializers.PrimaryKeyRelatedField(
|
||||||
|
queryset=TestQuestion.objects.all(),
|
||||||
|
allow_null=True
|
||||||
|
)
|
||||||
|
answer = serializers.PrimaryKeyRelatedField(
|
||||||
|
queryset=TestAnswer.objects.all(),
|
||||||
|
allow_null=True,
|
||||||
|
many=False)
|
||||||
comment = serializers.CharField(allow_null=True, required=False)
|
comment = serializers.CharField(allow_null=True, required=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
+41
-15
@@ -418,23 +418,49 @@ class TestResultView(generics.ListCreateAPIView):
|
|||||||
)
|
)
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
"""Saves the results of a new test attempt."""
|
"""Saves the results of a new test attempt."""
|
||||||
user = request.user
|
try:
|
||||||
# user = User.objects.get(username = "9966-00-125")
|
user = request.user
|
||||||
status = request.query_params.get('status')
|
# user = User.objects.get(username = "9966-00-125")
|
||||||
test_id = request.query_params.get('id')
|
status = request.query_params.get('status')
|
||||||
answer_ids = [item['answer'] for item in request.data]
|
test_id = request.query_params.get('id')
|
||||||
test_info = self.get_test_info(answer_ids, test_id)
|
answer_ids = []
|
||||||
if status == 'testing':
|
prepared_data = []
|
||||||
test_attempt, testing = self.create_test_attempt(user, test_info)
|
for data in request.data:
|
||||||
|
question_id = data.get('question_id')
|
||||||
|
answers_id = data.get('answers_id', [])
|
||||||
|
for sublist in answers_id:
|
||||||
|
if isinstance(sublist, list):
|
||||||
|
answer_ids.extend(sublist)
|
||||||
|
else:
|
||||||
|
answer_ids.append(sublist)
|
||||||
|
if answers_id:
|
||||||
|
for answer_id in answers_id:
|
||||||
|
prepared_data.append({
|
||||||
|
'question': question_id,
|
||||||
|
'answer': answer_id,
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
prepared_data.append({
|
||||||
|
'question': question_id,
|
||||||
|
'answer': None,
|
||||||
|
})
|
||||||
|
# answer_ids = [item['answer'] for item in request.data]
|
||||||
|
test_info = self.get_test_info(answer_ids, test_id)
|
||||||
|
if status == 'testing':
|
||||||
|
test_attempt, testing = self.create_test_attempt(user, test_info)
|
||||||
|
|
||||||
serializer = self.get_serializer(data=request.data, many=True)
|
# serializer = self.get_serializer(data=request.data, many=True)
|
||||||
if serializer.is_valid():
|
serializer = self.get_serializer(data=prepared_data, many=True)
|
||||||
serializer.save(attempt=test_attempt)
|
if serializer.is_valid():
|
||||||
self.update_testing(user, test_info, testing)
|
serializer.save(attempt=test_attempt)
|
||||||
|
self.update_testing(user, test_info, testing)
|
||||||
|
|
||||||
return Response(data={'score': test_info['score'],
|
return Response(data={'score': test_info['score'],
|
||||||
'answered_questions_total': test_info['answered_questions_total'],
|
'answered_questions_total': test_info['answered_questions_total'],
|
||||||
'is_passed': test_info['is_passed']})
|
'is_passed': test_info['is_passed']})
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return Response({"status": "error", "message": str(e)}, status=400)
|
||||||
|
|
||||||
def create_test_attempt(self, user, test_info):
|
def create_test_attempt(self, user, test_info):
|
||||||
"""Creates a new test attempt for the user."""
|
"""Creates a new test attempt for the user."""
|
||||||
|
|||||||
Reference in new issue
Block a user