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 From d22cd7736b46e4cae507cbb5e1d83f6794c2f921 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 22 Jul 2021 17:26:08 +0000 Subject: improve ui slightly --- apioforum/static/style.css | 1 + apioforum/templates/view_forum.html | 3 +++ 2 files changed, 4 insertions(+) diff --git a/apioforum/static/style.css b/apioforum/static/style.css index 237cad6..1ee3b14 100644 --- a/apioforum/static/style.css +++ b/apioforum/static/style.css @@ -80,6 +80,7 @@ dt { font-weight: bold } img { max-width: 100% } +.avail_tags { margin-top: 30px } nav#navbar { float: right; padding: 5px; margin: 2px; border: 1px solid black; display:flex; align-items: center; flex-wrap: wrap } nav#navbar p { margin-left: 15px; margin-top: 0; margin-bottom: 0; margin-right: 10px; padding: 0 } diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 67693d8..25d83e5 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -11,8 +11,11 @@ {% if forum.description %} {{forum.description|md|safe}} {% endif %} +

available tags: {% for the_tag in avail_tags %} {{tag(the_tag)}} +{% else %} +(none available) {% endfor %} {% if subforums %} -- cgit v1.2.3 From 054e159a1183bbd328d24924e4478e7677fade42 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 22 Jul 2021 17:35:37 +0000 Subject: minor html restructure --- apioforum/static/style.css | 2 +- apioforum/templates/base.html | 2 +- apioforum/templates/view_forum.html | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/apioforum/static/style.css b/apioforum/static/style.css index 1ee3b14..1f089a0 100644 --- a/apioforum/static/style.css +++ b/apioforum/static/style.css @@ -189,7 +189,7 @@ blockquote { border-left: 3px solid grey; } -.search-form { +.inline-form { display: inline-block; } diff --git a/apioforum/templates/base.html b/apioforum/templates/base.html index 9cfd7f3..ca1dd87 100644 --- a/apioforum/templates/base.html +++ b/apioforum/templates/base.html @@ -12,7 +12,7 @@