test-platform/server/cgi/db/question.py

76 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from db.object_pool import ObjectPool
class Question:
def init(self, sID, title, max_time, test_id):
self.id = sID
self.title = title
self.max_time = max_time
self.test_id = test_id
return self
def init_from_data(self, data):
return self.init(*data)
def get_id(self):
return self.id
def get_title(self):
return self.title
def get_max_time(self):
return self.max_time
def get_test_id(self):
return self.test_id
def render_short(self):
total_time = self.max_time
hours = total_time // 3600
total_time -= hours * 3600
minutes = total_time // 60
total_time -= minutes * 60
seconds = total_time
total_label = []
if hours:
total_label.append(f"{hours} год.")
if minutes:
total_label.append(f"{minutes} хв.")
if seconds:
total_label.append(f"{seconds} c.")
total_time = " ".join(total_label)
if int(self.max_time) > 0:
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">На відповідь є {total_time}</span></div>'
else:
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">Час на відповідь не обмежений</span></div>'
'''
class QuestionPool:
def __init__(self):
self.pool = []
def load_from_db(self, cur):
db.execute("SELECT * FROM response_option;")
self.pool = [ResponseOption().init_from_data(i) for i in cur]
return self
'''
class QuestionPool:
def __init__(self, db):
self.object_pool = ObjectPool("question", Question)
if db:
self.object_pool.load_from_db(db)
def iter(self):
return iter(self.object_pool.pool)
def select_by_test_id(self, test_id):
return [i for i in self.object_pool.pool if i.get_test_id() == int(test_id)]