refactoring
This commit is contained in:
1 parent
ec0a3d37f8
commit
10493c1107
1 file changed
+37
-12
+37
-12
@@ -35,7 +35,9 @@ class GetTests(generics.ListAPIView):
|
|||||||
openapi.Parameter(
|
openapi.Parameter(
|
||||||
'status',
|
'status',
|
||||||
openapi.IN_QUERY,
|
openapi.IN_QUERY,
|
||||||
description="Статус тестов (например, 'active', 'completed'). Пустое значение вернет все тесты",
|
description=("Статус тестов (например, 'active', 'completed'). "
|
||||||
|
"Пустое значение вернет все тесты."
|
||||||
|
),
|
||||||
type=openapi.TYPE_STRING,
|
type=openapi.TYPE_STRING,
|
||||||
required=False,
|
required=False,
|
||||||
),
|
),
|
||||||
@@ -46,6 +48,7 @@ class GetTests(generics.ListAPIView):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
def get (self, request, *args, **kwargs):
|
def get (self, request, *args, **kwargs):
|
||||||
|
"""receives tests assigned to the user"""
|
||||||
user = request.user
|
user = request.user
|
||||||
# user = User.objects.get(username='9966-00-125')
|
# user = User.objects.get(username='9966-00-125')
|
||||||
status = request.query_params.get('status')
|
status = request.query_params.get('status')
|
||||||
@@ -57,6 +60,7 @@ class GetTests(generics.ListAPIView):
|
|||||||
return Response(serialized_tests)
|
return Response(serialized_tests)
|
||||||
|
|
||||||
def get_tests_by_status(self, status, now, user):
|
def get_tests_by_status(self, status, now, user):
|
||||||
|
"""Returns a QuerySet of tests filtered by status for a given user."""
|
||||||
print(status)
|
print(status)
|
||||||
if status is None:
|
if status is None:
|
||||||
return Test.objects.all().order_by('dateend')
|
return Test.objects.all().order_by('dateend')
|
||||||
@@ -72,23 +76,28 @@ class GetTests(generics.ListAPIView):
|
|||||||
testing__completion_date__lt=now
|
testing__completion_date__lt=now
|
||||||
).distinct()
|
).distinct()
|
||||||
}
|
}
|
||||||
|
|
||||||
return status_filters.get(status, Test.objects.none()).order_by('dateend')
|
return status_filters.get(status, Test.objects.none()).order_by('dateend')
|
||||||
|
|
||||||
def set_attemps_number(self, user, tests):
|
def set_attemps_number(self, user, tests):
|
||||||
attempt_counts = TestAttempt.objects.filter(testing__user=user, testing__test__in=tests).values('testing__test').annotate(count=Count('testing__test'))
|
"""Updates the number of attempts remaining for each test for the user."""
|
||||||
|
attempt_counts = TestAttempt.objects.filter(testing__user=user,
|
||||||
|
testing__test__in=tests
|
||||||
|
).values('testing__test'
|
||||||
|
).annotate(count=Count('testing__test'))
|
||||||
attempt_counts = {item['testing__test']: item['count'] for item in attempt_counts}
|
attempt_counts = {item['testing__test']: item['count'] for item in attempt_counts}
|
||||||
|
|
||||||
for test in tests:
|
for test in tests:
|
||||||
test.attemps_number = test.attemps_number - attempt_counts.get(test.id, 0)
|
test.attemps_number = test.attemps_number - attempt_counts.get(test.id, 0)
|
||||||
|
|
||||||
class TestDetail(generics.RetrieveUpdateDestroyAPIView):
|
class TestDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
"""comment"""
|
||||||
queryset = Test.objects.all()
|
queryset = Test.objects.all()
|
||||||
serializer_class = TestSerializer
|
serializer_class = TestSerializer
|
||||||
lookup_url_kwarg = 'id'
|
lookup_url_kwarg = 'id'
|
||||||
lookup_field = 'id'
|
lookup_field = 'id'
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
|
"""Processes a GET request to retrieve detailed information about a test."""
|
||||||
test = self.get_object()
|
test = self.get_object()
|
||||||
number_of_questions = test.question_count
|
number_of_questions = test.question_count
|
||||||
random_questions = test.questions.order_by('?')[:number_of_questions]
|
random_questions = test.questions.order_by('?')[:number_of_questions]
|
||||||
@@ -196,7 +205,7 @@ class TestCreate(APIView):
|
|||||||
# return Response(serializer)
|
# return Response(serializer)
|
||||||
|
|
||||||
class TestResultView(generics.ListCreateAPIView):
|
class TestResultView(generics.ListCreateAPIView):
|
||||||
|
"""API for getting and saving user test results."""
|
||||||
queryset = TestResult.objects.all()
|
queryset = TestResult.objects.all()
|
||||||
serializer_class = TestResultSerializer
|
serializer_class = TestResultSerializer
|
||||||
pagination_class = None
|
pagination_class = None
|
||||||
@@ -219,13 +228,18 @@ class TestResultView(generics.ListCreateAPIView):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
|
"""Get the results of the last user test attempt."""
|
||||||
user = request.user
|
user = request.user
|
||||||
# user = User.objects.get(username = "9966-00-125")
|
# user = User.objects.get(username = "9966-00-125")
|
||||||
test_id = request.GET.get('id')
|
test_id = request.GET.get('id')
|
||||||
last_attempt = TestAttempt.objects.filter(testing__user=user, testing__test__id=test_id).order_by('-completion_date').first()
|
last_attempt = TestAttempt.objects.filter(testing__user=user,
|
||||||
answer_ids = TestResult.objects.filter(attempt=last_attempt).values_list('answer__id', flat=True)
|
testing__test__id=test_id
|
||||||
|
).order_by('-completion_date').first()
|
||||||
|
answer_ids = TestResult.objects.filter(attempt=last_attempt
|
||||||
|
).values_list('answer__id', flat=True)
|
||||||
test_info = self.get_test_info(answer_ids)
|
test_info = self.get_test_info(answer_ids)
|
||||||
return Response(data={'score': test_info['score'], 'answered_questions_total': test_info['answered_questions_total']})
|
return Response(data={'score': test_info['score'],
|
||||||
|
'answered_questions_total': test_info['answered_questions_total']})
|
||||||
|
|
||||||
@swagger_auto_schema(
|
@swagger_auto_schema(
|
||||||
operation_description=""" Сохранить результаты тестов
|
operation_description=""" Сохранить результаты тестов
|
||||||
@@ -234,7 +248,8 @@ class TestResultView(generics.ListCreateAPIView):
|
|||||||
openapi.Parameter(
|
openapi.Parameter(
|
||||||
'status',
|
'status',
|
||||||
openapi.IN_QUERY,
|
openapi.IN_QUERY,
|
||||||
description="Статус тестов (например, 'testing'). Пустое значение - режим тренировки",
|
description=("Статус тестов (например, 'testing'). "
|
||||||
|
"Пустое значение - режим тренировки"),
|
||||||
type=openapi.TYPE_STRING,
|
type=openapi.TYPE_STRING,
|
||||||
required=False,
|
required=False,
|
||||||
),
|
),
|
||||||
@@ -254,6 +269,7 @@ class TestResultView(generics.ListCreateAPIView):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
|
"""Saves the results of a new test attempt."""
|
||||||
user = request.user
|
user = request.user
|
||||||
# user = User.objects.get(username = "9966-00-125")
|
# user = User.objects.get(username = "9966-00-125")
|
||||||
status = request.query_params.get('status')
|
status = request.query_params.get('status')
|
||||||
@@ -267,23 +283,31 @@ class TestResultView(generics.ListCreateAPIView):
|
|||||||
serializer.save(attempt=test_attempt)
|
serializer.save(attempt=test_attempt)
|
||||||
self.update_testing(user, test_info)
|
self.update_testing(user, test_info)
|
||||||
|
|
||||||
return Response(data={'score': test_info['score'], 'answered_questions_total': test_info['answered_questions_total']})
|
return Response(data={'score': test_info['score'],
|
||||||
|
'answered_questions_total': test_info['answered_questions_total']})
|
||||||
|
|
||||||
def create_test_attempt(self, user, test_info):
|
def create_test_attempt(self, user, test_info):
|
||||||
|
"""Creates a new test attempt for the user."""
|
||||||
test = test_info['test']
|
test = test_info['test']
|
||||||
score = test_info['score']
|
score = test_info['score']
|
||||||
testing = Testing.objects.get(user=user, test=test)
|
testing = Testing.objects.get(user=user, test=test)
|
||||||
attempt_count = get_attempt_count(testing)+ 1
|
attempt_count = get_attempt_count(testing)+ 1
|
||||||
test_attempt = TestAttempt.objects.create(testing=testing , score=score , attempt_number=attempt_count , completion_date= datetime.now())
|
test_attempt = TestAttempt.objects.create(testing=testing,
|
||||||
|
score=score,
|
||||||
|
attempt_number=attempt_count,
|
||||||
|
completion_date= datetime.now())
|
||||||
return test_attempt
|
return test_attempt
|
||||||
|
|
||||||
def update_testing(self, user, test_info):
|
def update_testing(self, user, test_info):
|
||||||
|
"""Updates or creates a Testing record with a test passing flag."""
|
||||||
test = test_info['test']
|
test = test_info['test']
|
||||||
test_passed = test_info['is_passed']
|
test_passed = test_info['is_passed']
|
||||||
Testing.objects.update_or_create(user=user, test=test, defaults = {"test_passed":test_passed})
|
Testing.objects.update_or_create(user=user,
|
||||||
|
test=test,
|
||||||
|
defaults = {"test_passed":test_passed})
|
||||||
|
|
||||||
def get_test_info(self, answer_ids):
|
def get_test_info(self, answer_ids):
|
||||||
|
"""Calculates test results from a list of answer IDs."""
|
||||||
test_answers = TestAnswer.objects.filter(id__in=answer_ids)
|
test_answers = TestAnswer.objects.filter(id__in=answer_ids)
|
||||||
test = test_answers[0].question.test
|
test = test_answers[0].question.test
|
||||||
answered_questions_total = test_answers.values('question').distinct().count()
|
answered_questions_total = test_answers.values('question').distinct().count()
|
||||||
@@ -309,4 +333,5 @@ class TestResultView(generics.ListCreateAPIView):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def get_attempt_count(testing):
|
def get_attempt_count(testing):
|
||||||
|
"""get attempt count"""
|
||||||
return TestAttempt.objects.filter(testing=testing).count()
|
return TestAttempt.objects.filter(testing=testing).count()
|
||||||
Reference in new issue
Block a user