diff --git a/src/back/tests/views.py b/src/back/tests/views.py index 59becc2c5..a418931ef 100644 --- a/src/back/tests/views.py +++ b/src/back/tests/views.py @@ -343,7 +343,7 @@ class TestResultView(generics.ListCreateAPIView): ).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, test_id) return Response(data={'score': test_info['score'], 'answered_questions_total': test_info['answered_questions_total']}) @@ -379,8 +379,9 @@ class TestResultView(generics.ListCreateAPIView): 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_info = self.get_test_info(answer_ids, test_id) if status == 'testing': test_attempt, testing = self.create_test_attempt(user, test_info) @@ -415,10 +416,10 @@ class TestResultView(generics.ListCreateAPIView): # test=test, # defaults = {"test_passed":test_passed}) - def get_test_info(self, answer_ids): + def get_test_info(self, answer_ids, test_id): """Calculates test results from a list of answer IDs.""" test_answers = TestAnswer.objects.filter(id__in=answer_ids) - test = test_answers[0].question.test + test = Test.objects.get(id=test_id) answered_questions_total = test_answers.values('question').distinct().count() correct_questions = sum( @@ -432,9 +433,12 @@ class TestResultView(generics.ListCreateAPIView): for question in set(test_answers.values_list('question', flat=True)) ) - pass_perc = test_answers[0].question.test.pass_perc + pass_perc = test.pass_perc + correct_answers_perc = 0 + if answered_questions_total !=0: + correct_answers_perc = (correct_questions/answered_questions_total*100) result = { - "is_passed": (correct_questions/answered_questions_total*100) >= pass_perc, + "is_passed": correct_answers_perc >= pass_perc, "score": correct_questions, "test": test, "answered_questions_total": answered_questions_total,