diff options
-rw-r--r-- | apioforum/db.py | 16 | ||||
-rw-r--r-- | apioforum/forum.py | 17 | ||||
-rw-r--r-- | apioforum/static/style.css | 3 | ||||
-rw-r--r-- | apioforum/templates/base.html | 2 | ||||
-rw-r--r-- | apioforum/templates/view_forum.html | 12 | ||||
-rw-r--r-- | apioforum/thread.py | 7 |
6 files changed, 50 insertions, 7 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("/<int:forum_id>/create_thread",methods=("GET","POST")) diff --git a/apioforum/static/style.css b/apioforum/static/style.css index 237cad6..1f089a0 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 } @@ -188,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 @@ <body> <nav id="navbar"> <p style="font-family: monospace;"><b>apio</b><i>forum</i>™</p> - <form class="search-form" action="/search"> + <form class="inline-form" action="/search"> <input type="search" placeholder="query" name="q"> <input type="submit" value="search"> </form> diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 88be22f..475fdc2 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -11,6 +11,12 @@ {% if forum.description %} {{forum.description|md|safe}} {% endif %} +<p class="avail_tags">available tags: +{% for the_tag in avail_tags %} +{{tag(the_tag)}} +{% else %} +<em>(none available)</em> +{% endfor %} {% if subforums %} <h2>subforae</h2> @@ -37,11 +43,13 @@ {% endif %} <h2>threads</h2> +<p> {% if g.user %} -<p><a class="actionbutton" href="{{url_for('forum.create_thread',forum_id=forum.id)}}">create new thread</a></p> +<a class="actionbutton" href="{{url_for('forum.create_thread',forum_id=forum.id)}}">create new thread</a> {% else %} -<p>please log in to create a new thread</p> +please log in to create a new thread {% endif %} +</p> <div class="thread-list"> {%for thread in threads%} <div class="listing"> 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: |