From 002dfa21d5afb6b073a1315366f57d2462666dfe Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sat, 1 Jun 2024 20:05:41 +0300 Subject: [PATCH] hotfix: 500 while rendering view-test page that has questions with no answer options --- server/cgi/db/question.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server/cgi/db/question.py b/server/cgi/db/question.py index 2b389c5..b01be16 100644 --- a/server/cgi/db/question.py +++ b/server/cgi/db/question.py @@ -57,14 +57,22 @@ class Question: def get_correct_response_percentage(self, cur): rop = ResponseOptionPool(cur) response_options = rop.select_by_question_id(self.id) - return sum([int(i.get_correctness()) for i in response_options]) / len(response_options) * 100 + if len(response_options) == 0: + return None + else: + return sum([int(i.get_correctness()) for i in response_options]) / len(response_options) * 100 def render_short(self, cur): time_label = self.get_time_label() response_options = self.get_response_option_short_list(cur) - correct_percentage = round(self.get_correct_response_percentage(cur)) + correct_percentage = self.get_correct_response_percentage(cur) - return f'
#{self.id}{self.title}{time_label}
{correct_percentage}% відповідей правильні
{response_options}
' + if correct_percentage != None: + sub_label = f"{time_label}
{round(correct_percentage)}% відповідей правильні" + else: + sub_label = f"{time_label}" + + return f'
#{self.id}{self.title}{sub_label}
{response_options}
' class QuestionPool: def __init__(self, db):