initial development checkpoint 3

This commit is contained in:
2024-06-01 13:49:13 +03:00
parent b4c1a23ce2
commit 54377708b0
9 changed files with 240 additions and 24 deletions

View File

@@ -1,4 +1,5 @@
from db.object_pool import ObjectPool
from db.response_option import ResponseOptionPool
class Question:
def init(self, sID, title, max_time, test_id):
@@ -24,7 +25,10 @@ class Question:
def get_test_id(self):
return self.test_id
def render_short(self):
def get_time_label(self):
if int(self.max_time) == 0:
return "Час на відповідь не обмежений"
total_time = self.max_time
hours = total_time // 3600
@@ -36,6 +40,7 @@ class Question:
seconds = total_time
total_label = []
if hours:
total_label.append(f"{hours} год.")
if minutes:
@@ -43,23 +48,17 @@ class Question:
if seconds:
total_label.append(f"{seconds} c.")
total_time = " ".join(total_label)
return "На відповідь є " + " ".join(total_label)
def get_response_option_short_list(self, cur):
rop = ResponseOptionPool(cur)
return "<br>".join([i.render_short() for i in rop.select_by_question_id(self.id)])
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>'
def render_short(self, cur):
time_label = self.get_time_label()
response_options = self.get_response_option_short_list(cur)
'''
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
'''
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}</span><div class="response-option-short-list">{response_options}</div><div class="controls"><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:
def __init__(self, db):

View File

@@ -28,7 +28,7 @@ class ResponseOption:
if self.correctness:
c_mark = "+ "
else:
c_mark = " "
c_mark = "- "
return f'<span class="response-option-short">{c_mark} {self.label}</span>'

View File

@@ -8,8 +8,8 @@ class QuestionList:
qp = QuestionPool(self.cursor)
if test_id:
rendered_questions = [i.render_short() for i in qp.select_by_test_id(test_id)]
rendered_questions = [i.render_short(self.cursor) for i in qp.select_by_test_id(test_id)]
else:
rendered_questions = [i.render_short() for i in qp.iter()]
rendered_questions = [i.render_short(self.cursor) for i in qp.iter()]
return "\n".join(rendered_questions)

View File

@@ -23,8 +23,11 @@ class View:
"view-test": self.render_view_test,
"create-question": self.render_create_question,
"edit-question": self.render_edit_question,
"delete-question": self.render_delete_question,
"view-question": self.render_view_question,
"create-response-option": self.render_create_response_option,
"edit-response-option": self.render_edit_response_option,
"delete-response-option": self.render_delete_response_option,
}
def get_db_connection(self):
@@ -110,6 +113,7 @@ class View:
subheader = f'<a class="generic-button" href="?mode=create-question&test_id={self.args["id"]}">Додати запитання</a>'
content = QuestionList(cur).render(self.args['id'])
content += '<div class="return-button-centerer"><a class="return-button" href="/index.py">Назад до переліку тестів</a></div>'
return header, subheader, content
@@ -146,11 +150,31 @@ class View:
<label for="title">Текст запитання:</label>
<input type="text" name="title" placeholder="Введіть запитання..." value="{escape_html(question_title)}" required><br>
<label for="mtime">Максимальний час на відповідь (в сек):</label>
<input type="number" name="mtime" placeholder="Наприклад, 120"><br>
<input type="number" name="mtime" placeholder="Наприклад, 120" value="{question_max_time}"><br>
<input type="submit" value="Зберегти зміни">
</form>'''
return header, subheader, content
def render_delete_question(self, cur):
cur.execute(f"SELECT title FROM question WHERE id = {self.args['id']};")
question_title = next(iter(cur), [None])[0]
if not question_title:
header = f"<h2>Такого запитання не існує: {self.args['id']}</h2>"
subheader = f'<a href="/index.py">Повернутися на головну сторінку</a>'
content = ""
return header, subheader, content
header = f"<h2>Точно видалити запитання?</h2>"
subheader = f"<i>{question_title}</i>"
cur.execute(f"SELECT test.id FROM test JOIN question ON test.id = question.tstID WHERE question.id = {self.args['id']};")
test_id = iter(cur).__next__()[0]
content = f'''<a class="delete-button" href="/delete-question.py?id={self.args["id"]}">Так, видалити</a><br><a class="cancel-button" href="/index.py?mode=view-test&id={test_id}">Ні, залишити</a>'''
return header, subheader, content
def render_view_question(self, cur):
cur.execute(f"SELECT title FROM question WHERE id = {self.args['id']};")
@@ -169,6 +193,14 @@ class View:
content = ResponseOptionList(cur).render(self.args['id'])
cur.execute(f"""SELECT test.id FROM test
JOIN question ON test.id = question.tstID
WHERE question.id = {self.args['id']};""")
test_id = iter(cur).__next__()[0]
content += f'<div class="return-button-centerer"><a class="return-button" href="/index.py?mode=view-test&id={test_id}">Назад до тесту</a></div>'
return header, subheader, content
def render_create_response_option(self, cur):
@@ -183,3 +215,33 @@ class View:
</form>'''
return header, subheader, content
def render_edit_response_option(self, cur):
cur.execute(f"SELECT label FROM response_option WHERE id = {self.args['id']};")
respose_option_label = next(iter(cur), [None])[0]
header = f"<h2>Редагувати варіант відповіді</h2>"
subheader = "<i>Введіть властивості варіанту відповіді нижче</i>"
content = f'''<form action="/edit-response-option.py">
<input type="text" name="id" value="{self.args["id"]}" style="display:none;">
<label for="label">Текст відповіді:</label>
<input type="text" name="label" placeholder="Введіть текст відповіді..." value="{respose_option_label}" required><br>
<input type="submit" value="Оновити">
</form>'''
return header, subheader, content
def render_delete_response_option(self, cur):
cur.execute(f"SELECT label FROM response_option WHERE id = {self.args['id']};")
response_option_label = next(iter(cur), [None])[0]
header = f"<h2>Точно видалити цей варіант відповіді?</h2>"
subheader = f"<i>{response_option_label}</i>"
cur.execute(f"SELECT question.id FROM question JOIN response_option ON question.id = response_option.qstID WHERE response_option.id = {self.args['id']};")
quest_id = iter(cur).__next__()[0]
content = f'''<a class="delete-button" href="/delete-response-option.py?id={self.args["id"]}">Так, видалити</a><br><a class="cancel-button" href="/index.py?mode=view-question&id={quest_id}">Ні, залишити</a>'''
return header, subheader, content