get_test_info

This commit is contained in:
cja committed 2025-05-30 11:03:47 +03:00
1 parent 1656809393
commit 84ab7ee8e2
1 file changed
+10 -6
+10 -6
View File
@@ -343,7 +343,7 @@ class TestResultView(generics.ListCreateAPIView):
).order_by('-completion_date').first() ).order_by('-completion_date').first()
answer_ids = TestResult.objects.filter(attempt=last_attempt answer_ids = TestResult.objects.filter(attempt=last_attempt
).values_list('answer__id', flat=True) ).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'], return Response(data={'score': test_info['score'],
'answered_questions_total': test_info['answered_questions_total']}) 'answered_questions_total': test_info['answered_questions_total']})
@@ -379,8 +379,9 @@ class TestResultView(generics.ListCreateAPIView):
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')
test_id = request.query_params.get('id')
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, test_id)
if status == 'testing': if status == 'testing':
test_attempt, testing = self.create_test_attempt(user, test_info) test_attempt, testing = self.create_test_attempt(user, test_info)
@@ -415,10 +416,10 @@ class TestResultView(generics.ListCreateAPIView):
# test=test, # test=test,
# defaults = {"test_passed":test_passed}) # 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.""" """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.objects.get(id=test_id)
answered_questions_total = test_answers.values('question').distinct().count() answered_questions_total = test_answers.values('question').distinct().count()
correct_questions = sum( correct_questions = sum(
@@ -432,9 +433,12 @@ class TestResultView(generics.ListCreateAPIView):
for question in set(test_answers.values_list('question', flat=True)) 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 = { result = {
"is_passed": (correct_questions/answered_questions_total*100) >= pass_perc, "is_passed": correct_answers_perc >= pass_perc,
"score": correct_questions, "score": correct_questions,
"test": test, "test": test,
"answered_questions_total": answered_questions_total, "answered_questions_total": answered_questions_total,