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

This commit is contained in:
Denis Voinovich committed 2025-05-29 16:26:28 +03:00
commit a98ae686f9
1 file changed
+16 -11
+16 -11
View File
@@ -69,12 +69,14 @@ class GetTests(generics.ListAPIView):
status_filters = { status_filters = {
'active' : Test.objects.filter( 'active' : Test.objects.filter(
testing__user=user, testing__user=user,
testing__completion_date__gte=now testing__completion_date__gte=now,
testing__is_active=True
).distinct(), ).distinct(),
'completed' : Test.objects.filter( 'completed' : Test.objects.filter(
testing__user=user, testing__user=user,
testing__completion_date__lt=now testing__completion_date__lt=now,
testing__is_active=True
).distinct() ).distinct()
} }
return status_filters.get(status, Test.objects.none()) return status_filters.get(status, Test.objects.none())
@@ -82,7 +84,8 @@ class GetTests(generics.ListAPIView):
def set_attemps_number(self, user, tests): def set_attemps_number(self, user, tests):
"""Updates the number of attempts remaining for each test for the user.""" """Updates the number of attempts remaining for each test for the user."""
attempt_counts = TestAttempt.objects.filter(testing__user=user, attempt_counts = TestAttempt.objects.filter(testing__user=user,
testing__test__in=tests testing__test__in=tests,
testing__is_active=True
).values('testing__test' ).values('testing__test'
).annotate(count=Count('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}
@@ -362,12 +365,12 @@ class TestResultView(generics.ListCreateAPIView):
answer_ids = [item['answer'] for item in request.data] answer_ids = [item['answer'] for item in request.data]
test_info = self.get_test_info(answer_ids) test_info = self.get_test_info(answer_ids)
if status == 'testing': if status == 'testing':
test_attempt = self.create_test_attempt(user, test_info) 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(): if serializer.is_valid():
serializer.save(attempt=test_attempt) serializer.save(attempt=test_attempt)
self.update_testing(user, test_info) 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'],
@@ -377,21 +380,23 @@ class TestResultView(generics.ListCreateAPIView):
"""Creates a new test attempt for the user.""" """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, is_active=True)
attempt_count = get_attempt_count(testing)+ 1 attempt_count = get_attempt_count(testing)+ 1
test_attempt = TestAttempt.objects.create(testing=testing, test_attempt = TestAttempt.objects.create(testing=testing,
score=score, score=score,
attempt_number=attempt_count, attempt_number=attempt_count,
completion_date= datetime.now()) completion_date= datetime.now())
return test_attempt return test_attempt, testing
def update_testing(self, user, test_info): def update_testing(self, user, test_info, testing):
"""Updates or creates a Testing record with a test passing flag.""" """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, testing.test_passed = test_passed
test=test, testing.save()
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.""" """Calculates test results from a list of answer IDs."""