From 5f24e7ca10bffc20aa486e50e9dee222bda28dc6 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 20 Jun 2021 23:12:44 +0000 Subject: much better schema, and some (but only some) multiforumification --- apioforum/db.py | 17 ++++++++++++++++- apioforum/forum.py | 19 +++++++++++++------ apioforum/templates/create_thread.html | 2 +- apioforum/templates/view_forum.html | 7 +++---- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/apioforum/db.py b/apioforum/db.py index ad6cd39..136b7c6 100644 --- a/apioforum/db.py +++ b/apioforum/db.py @@ -92,7 +92,22 @@ CREATE TABLE forums ( description TEXT ); INSERT INTO forums (name,parent,description) values ('root',null,'the default root forum'); -ALTER TABLE threads ADD COLUMN forum NOT NULL DEFAULT 1 REFERENCES forums(id); + +PRAGMA foreign_keys = off; +BEGIN TRANSACTION; +CREATE TABLE threads_new ( + id INTEGER PRIMARY KEY, + title TEXT NOT NULL, + creator TEXT NOT NULL, + created TIMESTAMP NOT NULL, + updated TIMESTAMP NOT NULL, + forum NOT NULL REFERENCES forums(id) +); +INSERT INTO threads_new (id,title,creator,created,updated,forum) + SELECT id,title,creator,created,updated,1 FROM threads; +DROP TABLE threads; +ALTER TABLE threads_new RENAME TO threads; +COMMIT; """, ] diff --git a/apioforum/forum.py b/apioforum/forum.py index defc5b1..3022507 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -14,17 +14,19 @@ from sqlite3 import OperationalError bp = Blueprint("forum", __name__, url_prefix="/") -@bp.route("/") -def view_forum(): +@bp.route("/") +def view_forum(forum_id): db = get_db() + forum = db.execute("SELECT * FROM forums WHERE id = ?",(forum_id,)).fetchone() threads = db.execute( """SELECT threads.id, threads.title, threads.creator, threads.created, threads.updated, count(posts.id) as num_replies, max(posts.id), posts.author as last_user FROM threads INNER JOIN posts ON posts.thread = threads.id + WHERE threads.forum = ? GROUP BY threads.id ORDER BY threads.updated DESC; - """).fetchall() + """,(forum_id,)).fetchall() thread_tags = {} #todo: somehow optimise this for thread in threads: @@ -34,10 +36,15 @@ def view_forum(): WHERE thread_tags.thread = ? ORDER BY tags.id; """,(thread['id'],)).fetchall() - return render_template("view_forum.html",threads=threads,thread_tags=thread_tags) + return render_template( + "view_forum.html", + threads=threads, + thread_tags=thread_tags, + forum=forum + ) -@bp.route("/create_thread",methods=("GET","POST")) -def create_thread(): +@bp.route("//create_thread",methods=("GET","POST")) +def create_thread(forum_id): db = get_db() if g.user is None: diff --git a/apioforum/templates/create_thread.html b/apioforum/templates/create_thread.html index 9de7312..04b4f42 100644 --- a/apioforum/templates/create_thread.html +++ b/apioforum/templates/create_thread.html @@ -4,7 +4,7 @@ {% endblock %} {% block content %} -
+
diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 59c594b..c5bcef1 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -1,12 +1,11 @@ {% extends 'base.html' %} {% from 'common.html' import ts, tag, disp_user %} -{% block header %}

{% block title %}apioforum{%endblock%}

{%endblock%} +{% block header %}

{% block title %}{{forum.name}}{%endblock%}

{%endblock%} {%block nmcontent%}
-

welcome to the apioforum

-

forum rules: do not be a bad person. do not do bad things.

+{{forum.description|md|safe}} {% if g.user %} -

create new thread

+

create new thread

{% else %}

please log in to create a new thread

{% endif %} -- cgit v1.2.3