diff --git a/src/back/tests/views.py b/src/back/tests/views.py index d3e6a3e7d..20a98b3ad 100644 --- a/src/back/tests/views.py +++ b/src/back/tests/views.py @@ -56,7 +56,7 @@ class GetTests(generics.ListAPIView): def set_attemps_number(self, user, tests): test_ids = [test.id for test in tests] - attempt_counts = TestAttempt.objects.filter(user=user, testing__test__id__in=test_ids).values('testing__test').annotate(count=Count('testing__test')) + attempt_counts = TestAttempt.objects.filter(testing__user=user, testing__test__id__in=test_ids).values('testing__test').annotate(count=Count('testing__test')) attempt_counts = {item['testing__test']: item['count'] for item in attempt_counts} for test in tests: @@ -133,11 +133,11 @@ class TestResultCreate(generics.ListCreateAPIView): serializer_class = TestResultSerializer def get(self, request, *args, **kwargs): - # user = request.user - user = User.objects.get(username = "9966-00-125") + user = request.user + # user = User.objects.get(username = "9966-00-125") test_id = request.GET.get('id') - test = Testing.objects.filter(user=user, test__id=test_id).first().test - last_attempt = TestAttempt.objects.filter(user=user, testing__test=test).order_by('-completion_date').first() + testing = Testing.objects.filter(user=user, test__id=test_id) + last_attempt = TestAttempt.objects.filter(testing=testing).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) return Response(data={'score': test_info['score'], 'answered_questions_total': test_info['answered_questions_total']}) @@ -159,9 +159,9 @@ class TestResultCreate(generics.ListCreateAPIView): def create_test_attempt(self, user, test_info): test = test_info['test'] score = test_info['score'] - attempt_count = get_attempt_count(user=user, test=test)+ 1 testing = Testing.objects.get(user=user, test=test) - test_attempt = TestAttempt.objects.create(user=user, testing=testing , score=score , attempt_number=attempt_count , completion_date= datetime.now()) + attempt_count = get_attempt_count(testing)+ 1 + test_attempt = TestAttempt.objects.create(testing=testing , score=score , attempt_number=attempt_count , completion_date= datetime.now()) return test_attempt def update_testing(self, user, test_info): @@ -195,5 +195,5 @@ class TestResultCreate(generics.ListCreateAPIView): } return result -def get_attempt_count(user, test): - return TestAttempt.objects.filter(user=user, testing__test=test).count() \ No newline at end of file +def get_attempt_count(testing): + return TestAttempt.objects.filter(testing=testing).count() \ No newline at end of file