chore [P-01]: подтянул бэк

This commit is contained in:
Denis Voinovich committed 2025-06-03 13:30:31 +03:00
commit 421b7cbacd
2 files changed
+49 -16

No files matched your search

+8 -1
View File
@@ -93,7 +93,14 @@ class TestAttemptSerializer(serializers.ModelSerializer):
class TestResultSerializer(serializers.ModelSerializer):
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)
class Meta:
+41 -15
View File
@@ -418,23 +418,49 @@ class TestResultView(generics.ListCreateAPIView):
)
def post(self, request, *args, **kwargs):
"""Saves the results of a new test attempt."""
user = request.user
# user = User.objects.get(username = "9966-00-125")
status = request.query_params.get('status')
test_id = request.query_params.get('id')
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)
try:
user = request.user
# user = User.objects.get(username = "9966-00-125")
status = request.query_params.get('status')
test_id = request.query_params.get('id')
answer_ids = []
prepared_data = []
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)
if serializer.is_valid():
serializer.save(attempt=test_attempt)
self.update_testing(user, test_info, testing)
# serializer = self.get_serializer(data=request.data, many=True)
serializer = self.get_serializer(data=prepared_data, many=True)
if serializer.is_valid():
serializer.save(attempt=test_attempt)
self.update_testing(user, test_info, testing)
return Response(data={'score': test_info['score'],
'answered_questions_total': test_info['answered_questions_total'],
'is_passed': test_info['is_passed']})
return Response(data={'score': test_info['score'],
'answered_questions_total': test_info['answered_questions_total'],
'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):
"""Creates a new test attempt for the user."""