diff options
author | ubq323 <ubq323> | 2021-06-20 23:12:44 +0000 |
---|---|---|
committer | ubq323 <ubq323> | 2021-06-20 23:12:44 +0000 |
commit | 5f24e7ca10bffc20aa486e50e9dee222bda28dc6 (patch) | |
tree | b6b349bbca974cece24052509123997d34a87b74 | |
parent | fdbbda97f604e9fca9cd490e0227313130b75f28 (diff) |
much better schema, and some (but only some) multiforumification
-rw-r--r-- | apioforum/db.py | 17 | ||||
-rw-r--r-- | apioforum/forum.py | 19 | ||||
-rw-r--r-- | apioforum/templates/create_thread.html | 2 | ||||
-rw-r--r-- | 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("/<int:forum_id>") +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("/<int:forum_id>/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 %} -<form action="{{url_for('forum.create_thread')}}", method="POST"> +<form method="POST"> <label for="title">thread title</label> <input name="title" id="title"> <br> 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 %}<h1>{% block title %}apioforum{%endblock%}</h1>{%endblock%} +{% block header %}<h1>{% block title %}{{forum.name}}{%endblock%}</h1>{%endblock%} {%block nmcontent%} <main class="widemain"> -<p>welcome to the apioforum</p> -<p>forum rules: do not be a bad person. do not do bad things.</p> +{{forum.description|md|safe}} {% if g.user %} -<p><a class="actionbutton" href="{{url_for('forum.create_thread')}}">create new thread</a></p> +<p><a class="actionbutton" href="{{url_for('forum.create_thread',forum_id=forum.id)}}">create new thread</a></p> {% else %} <p>please log in to create a new thread</p> {% endif %} |