From dc65b43ddce195c2ed35f6b7a0c1a96d284e0d55 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 22 Jul 2021 16:59:50 +0000 Subject: make tags be per-forum and have the thread config interface work with this --- apioforum/db.py | 16 ++++++++++++++++ apioforum/forum.py | 17 +++++++++++++++++ apioforum/templates/view_forum.html | 3 +++ apioforum/thread.py | 7 ++++--- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/apioforum/db.py b/apioforum/db.py index abcd774..50b142e 100644 --- a/apioforum/db.py +++ b/apioforum/db.py @@ -155,6 +155,22 @@ CREATE VIEW number_of_posts AS CREATE VIEW total_vote_counts AS SELECT poll, count(*) AS total_votes FROM votes WHERE current AND NOT is_retraction GROUP BY poll; """, +""" +PRAGMA foreign_keys = off; +BEGIN TRANSACTION; +CREATE TABLE tags_new ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + text_colour TEXT NOT NULL, + bg_colour TEXT NOT NULL, + forum INTEGER NOT NULL REFERENCES forums(id) +); +INSERT INTO tags_new (id,name,text_colour,bg_colour,forum) + SELECT id,name,text_colour,bg_colour,1 FROM tags; +DROP TABLE tags; +ALTER TABLE tags_new RENAME TO tags; +PRAGMA foreign_keys = on; +""", ] def init_db(): diff --git a/apioforum/forum.py b/apioforum/forum.py index d3402f0..e03ca01 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -18,6 +18,19 @@ bp = Blueprint("forum", __name__, url_prefix="/") def not_actual_index(): return redirect("/1") +def get_avail_tags(forum_id): + db = get_db() + tags = db.execute(""" + WITH RECURSIVE fs AS + (SELECT * FROM forums WHERE id = ? + UNION ALL + SELECT forums.* FROM forums, fs WHERE fs.parent=forums.id) + SELECT * FROM tags + WHERE tags.forum in (SELECT id FROM fs) + ORDER BY id; + """,(forum_id,)).fetchall() + return tags + def forum_path(forum_id): db = get_db() ancestors = db.execute(""" @@ -50,6 +63,9 @@ def view_forum(forum_id): """,(forum_id,)).fetchall() thread_tags = {} thread_polls = {} + + avail_tags = get_avail_tags(forum_id) + #todo: somehow optimise this for thread in threads: thread_tags[thread['id']] = db.execute( @@ -103,6 +119,7 @@ def view_forum(forum_id): threads=threads, thread_tags=thread_tags, thread_polls=thread_polls, + avail_tags=avail_tags, ) @bp.route("//create_thread",methods=("GET","POST")) diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 88be22f..67693d8 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -11,6 +11,9 @@ {% if forum.description %} {{forum.description|md|safe}} {% endif %} +{% for the_tag in avail_tags %} +{{tag(the_tag)}} +{% endfor %} {% if subforums %}

subforae

diff --git a/apioforum/thread.py b/apioforum/thread.py index f8e8036..991dc0b 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -7,6 +7,7 @@ from flask import ( url_for, flash, jsonify ) from .db import get_db +from .forum import get_avail_tags bp = Blueprint("thread", __name__, url_prefix="/thread") @@ -275,7 +276,7 @@ def config_thread(thread_id): db = get_db() thread = db.execute("select * from threads where id = ?",(thread_id,)).fetchone() thread_tags = [r['tag'] for r in db.execute("select tag from thread_tags where thread = ?",(thread_id,)).fetchall()] - avail_tags = db.execute("select * from tags order by id").fetchall() + avail_tags = get_avail_tags(thread['forum']) err = None if g.user is None: err = "you need to be logged in to do that" @@ -297,8 +298,8 @@ def config_thread(thread_id): flash("title updated successfully") db.commit() changed = False - wanted_tags = [] - for tagid in range(1,len(avail_tags)+1): + for avail_tag in avail_tags: + tagid = avail_tag['id'] current = tagid in thread_tags wanted = f'tag_{tagid}' in request.form if wanted and not current: -- cgit v1.2.3