diff --git a/src/back/tests/migrations/__pycache__/0006_testresult_question_alter_testresult_answer.cpython-310.pyc b/src/back/tests/migrations/__pycache__/0006_testresult_question_alter_testresult_answer.cpython-310.pyc index 44a4ecacf..9ce07a06f 100644 Binary files a/src/back/tests/migrations/__pycache__/0006_testresult_question_alter_testresult_answer.cpython-310.pyc and b/src/back/tests/migrations/__pycache__/0006_testresult_question_alter_testresult_answer.cpython-310.pyc differ diff --git a/src/back/tests/serializers.py b/src/back/tests/serializers.py index 7f2108df9..cf6dfd780 100644 --- a/src/back/tests/serializers.py +++ b/src/back/tests/serializers.py @@ -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: diff --git a/src/back/tests/views.py b/src/back/tests/views.py index 61e5f3e74..0039871b5 100644 --- a/src/back/tests/views.py +++ b/src/back/tests/views.py @@ -418,23 +418,50 @@ 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.""" diff --git a/src/front/src/components/testing/CatalogTest.vue b/src/front/src/components/testing/CatalogTest.vue index c08617bce..1ed17fb9c 100644 --- a/src/front/src/components/testing/CatalogTest.vue +++ b/src/front/src/components/testing/CatalogTest.vue @@ -67,10 +67,17 @@ - - + + - Данные не найдены + + Данные не найдены + diff --git a/src/front/src/layouts/default/DefaultPortal.vue b/src/front/src/layouts/default/DefaultPortal.vue index 1f89d506f..0886e054a 100644 --- a/src/front/src/layouts/default/DefaultPortal.vue +++ b/src/front/src/layouts/default/DefaultPortal.vue @@ -36,7 +36,7 @@