From 4303ff93359c42e61cb7362932a4433d4d51cf1b Mon Sep 17 00:00:00 2001 From: jwansek Date: Tue, 24 Mar 2026 18:33:56 +0000 Subject: Added page for each question --- edaweb/app.py | 15 +++++++++++++-- edaweb/database.py | 22 +++++++++++++++++++++- edaweb/templates/question.html.j2 | 22 ++++++++++++++++++++++ edaweb/templates/questions.html.j2 | 6 +----- 4 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 edaweb/templates/question.html.j2 (limited to 'edaweb') diff --git a/edaweb/app.py b/edaweb/app.py index ab2aa0c..0d4458a 100644 --- a/edaweb/app.py +++ b/edaweb/app.py @@ -17,7 +17,7 @@ import io app = flask.Flask(__name__) CONFIG = configparser.ConfigParser(interpolation = None) -CONFIG.read(os.path.join(os.path.dirname(__file__), "edaweb.conf")) +CONFIG.read(os.path.join(os.path.dirname(__file__), "..", "edaweb.conf")) shown_images = set() shown_sidebar_images = set() @@ -27,7 +27,6 @@ def get_pfp_img(db:database.Database): if len(shown_images) == len(dbimg): shown_images = set() folder = set(dbimg).difference(shown_images) - print(folder) choice = random.choice(list(folder)) shown_images.add(choice) return choice @@ -249,6 +248,18 @@ def serve_questions(): qnas_link = CONFIG.get("qnas", "url"), qnas = db.get_qnas() ) + +@app.route("/question") +def serve_question(): + with database.Database() as db: + question_id = flask.request.args.get("id", type=int) + qna = db.get_qna(question_id) + return flask.render_template( + "question.html.j2", + **get_template_items("question from %s" % qna["qna"][2], db), + qnas_link = CONFIG.get("qnas", "url"), + qna = qna + ) if __name__ == "__main__": try: diff --git a/edaweb/database.py b/edaweb/database.py index c6553a6..47e0f18 100644 --- a/edaweb/database.py +++ b/edaweb/database.py @@ -244,7 +244,27 @@ class Database: with self.__connection.cursor() as cursor: cursor.execute("SELECT * FROM qnas;") return sorted(cursor.fetchall(), key = operator.itemgetter(2), reverse = True) - + + def get_qna(self, id_): + with self.__connection.cursor() as cursor: + cursor.execute("SELECT * FROM `qnas` WHERE `curiouscat_id` = %s;", (id_, )) + qna = {"qna": list(cursor.fetchone())} + cursor.execute("SELECT `curiouscat_id` FROM `qnas` WHERE `timestamp` < %s ORDER BY `timestamp` DESC LIMIT 1;", (qna["qna"][2], )) + try: + qna["previous"] = cursor.fetchone()[0] + except TypeError: + qna["previous"] = None + cursor.execute("SELECT `curiouscat_id` FROM `qnas` WHERE `timestamp` > %s ORDER BY `timestamp` LIMIT 1;", (qna["qna"][2], )) + try: + qna["next"] = cursor.fetchone()[0] + except TypeError: + qna["next"] = None + + return qna + +if __name__ == "__main__": + with Database() as db: + print(db.get_qna(1098140963)) diff --git a/edaweb/templates/question.html.j2 b/edaweb/templates/question.html.j2 new file mode 100644 index 0000000..a9c7a0c --- /dev/null +++ b/edaweb/templates/question.html.j2 @@ -0,0 +1,22 @@ +{% extends "template.html.j2" %} +{% block content %} + + +

ask a question!

+
+
ID:
{{ qna["qna"][0] }}
+
Question source:
{{ qna["qna"][-1] }}
+
Answered at:
{{ qna["qna"][2] }}
+
Question:
{{ qna["qna"][3] }}
+
Answer:
{{ qna["qna"][4] }}
+
+{% endblock %} \ No newline at end of file diff --git a/edaweb/templates/questions.html.j2 b/edaweb/templates/questions.html.j2 index eb58380..a800941 100644 --- a/edaweb/templates/questions.html.j2 +++ b/edaweb/templates/questions.html.j2 @@ -3,11 +3,7 @@

ask a question!

{% for id_, link, dt, question, answer, host in qnas %} - {% if host == "curiouscat" %} -
{{ dt.isoformat() }} - {{ host }}
- {% else %} -
{{ dt.isoformat() }} - {{ host }}
- {% endif %} +
{{ dt.isoformat() }} - {{ host }}

{{ question }}

{{ answer }}

-- cgit v1.2.3