diff --git a/src/back/tests/views.py b/src/back/tests/views.py index 4576616e5..40221984a 100644 --- a/src/back/tests/views.py +++ b/src/back/tests/views.py @@ -19,17 +19,37 @@ import base64 import hashlib from cryptography.fernet import Fernet from django.db.models import F +from drf_yasg import openapi +from drf_yasg.utils import swagger_auto_schema class GetTests(generics.ListAPIView): queryset = Test.objects.all() serializer_class = TestsSerializer + pagination_class = None + @swagger_auto_schema( + operation_description="""Получить список тестов по статусу + *Нужно авторизоваться!""", + manual_parameters=[ + openapi.Parameter( + 'status', + openapi.IN_QUERY, + description="Статус тестов (например, 'active', 'completed'). Пустое значение вернет все тесты", + type=openapi.TYPE_STRING, + required=False, + ), + ], + responses={ + 200: openapi.Response('Успех', TestsSerializer(many=True)), + 400: 'Неверный запрос', + } + ) def get (self, request, *args, **kwargs): user = request.user # user = User.objects.get(username='9966-00-125') status = request.query_params.get('status') tests = self.get_tests_by_status(status, datetime.now(), user) - if status != None: + if status is not None: self.set_attemps_number(user, tests) serialized_tests = self.get_serializer(tests, many=True).data @@ -76,23 +96,41 @@ class TestDetail(generics.RetrieveUpdateDestroyAPIView): return Response(test_serialized) -class GetTesting(APIView): +# class GetTesting(APIView): - def get(self, request): - user = request.user - tests = Testing.objects.filter(user=user) - test_names = {test.id: test.test.name for test in tests} - serializer = TestingSeriaizer(tests, many=True).data - for test in serializer: - if test['id'] in test_names: - test['test_name'] = test_names[test['id']] - return Response(serializer) +# def get(self, request): +# user = request.user +# tests = Testing.objects.filter(user=user) +# test_names = {test.id: test.test.name for test in tests} +# serializer = TestingSeriaizer(tests, many=True).data +# for test in serializer: +# if test['id'] in test_names: +# test['test_name'] = test_names[test['id']] +# return Response(serializer) class TestResultView(generics.ListCreateAPIView): queryset = TestResult.objects.all() serializer_class = TestResultSerializer + pagination_class = None + @swagger_auto_schema( + operation_description="""Получить результаты тестов + *Нужно авторизоваться!""", + manual_parameters=[ + openapi.Parameter( + 'id', + openapi.IN_QUERY, + description="ID теста", + type=openapi.TYPE_INTEGER, + required=True, + ), + ], + responses={ + 200: openapi.Response('Успех', TestsSerializer(many=True)), + 400: 'Неверный запрос', + } + ) def get(self, request, *args, **kwargs): user = request.user # user = User.objects.get(username = "9966-00-125") @@ -102,6 +140,32 @@ class TestResultView(generics.ListCreateAPIView): test_info = self.get_test_info(answer_ids) return Response(data={'score': test_info['score'], 'answered_questions_total': test_info['answered_questions_total']}) + @swagger_auto_schema( + operation_description=""" Сохранить результаты тестов + *Нужно авторизоваться!""", + manual_parameters=[ + openapi.Parameter( + 'status', + openapi.IN_QUERY, + description="Статус тестов (например, 'testing'). Пустое значение - режим тренировки", + type=openapi.TYPE_STRING, + required=False, + ), + ], + request_body=openapi.Schema( + type=openapi.TYPE_ARRAY, + items=openapi.Schema( + type=openapi.TYPE_OBJECT, + properties={ + 'answer': openapi.Schema(type=openapi.TYPE_INTEGER, description='ID ответа') + }, + ), + ), + responses={ + 200: openapi.Response('Успех', TestsSerializer(many=True)), + 400: 'Неверный запрос', + } + ) def post(self, request, *args, **kwargs): user = request.user # user = User.objects.get(username = "9966-00-125")