hotfix: 500 while rendering view-test page that has questions with no answer options

This commit is contained in:
ІО-23 Шмуляр Олег 2024-06-01 20:05:41 +03:00
parent fbdc014bf4
commit 002dfa21d5
1 changed files with 11 additions and 3 deletions

View File

@ -57,14 +57,22 @@ class Question:
def get_correct_response_percentage(self, cur): def get_correct_response_percentage(self, cur):
rop = ResponseOptionPool(cur) rop = ResponseOptionPool(cur)
response_options = rop.select_by_question_id(self.id) 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): def render_short(self, cur):
time_label = self.get_time_label() time_label = self.get_time_label()
response_options = self.get_response_option_short_list(cur) 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'<div class="question-short"><a class="question-link" href="/index.py?mode=view-question&id={self.id}"><span class="sub-label">#{self.id}</span><span class="main-label">{self.title}</span></a><span class="sub-title">{time_label}<br>{correct_percentage}% відповідей правильні</span><div class="response-option-short-list">{response_options}</div><div class="controls"><a class="magic-button" href="?mode=generate-response-options&id={self.id}">Згенерувати відповіді</a><a class="sub-button" href="/index.py?mode=edit-question&id={self.id}">Редагувати</a><a class="scary-button" href="/index.py?mode=delete-question&id={self.id}">Видалити</a></div></div>' if correct_percentage != None:
sub_label = f"{time_label}<br>{round(correct_percentage)}% відповідей правильні"
else:
sub_label = f"{time_label}"
return f'<div class="question-short"><a class="question-link" href="/index.py?mode=view-question&id={self.id}"><span class="sub-label">#{self.id}</span><span class="main-label">{self.title}</span></a><span class="sub-title">{sub_label}</span><div class="response-option-short-list">{response_options}</div><div class="controls"><a class="magic-button" href="?mode=generate-response-options&id={self.id}">Згенерувати відповіді</a><a class="sub-button" href="/index.py?mode=edit-question&id={self.id}">Редагувати</a><a class="scary-button" href="/index.py?mode=delete-question&id={self.id}">Видалити</a></div></div>'
class QuestionPool: class QuestionPool:
def __init__(self, db): def __init__(self, db):