aboutsummaryrefslogtreecommitdiffhomepage
path: root/apioforum/forum.py
diff options
context:
space:
mode:
Diffstat (limited to 'apioforum/forum.py')
-rw-r--r--apioforum/forum.py53
1 files changed, 47 insertions, 6 deletions
diff --git a/apioforum/forum.py b/apioforum/forum.py
index 305cb51..988c9a5 100644
--- a/apioforum/forum.py
+++ b/apioforum/forum.py
@@ -96,8 +96,42 @@ def view_forum(forum,page=1):
if page < 1:
abort(400)
db = get_db()
+
+ sortby = request.args.get("sortby","ad")
+ try:
+ sortby_dir = {'d':'DESC','a':'ASC'}[sortby[1]]
+ sortby_by = {'a':'threads.updated','c':'threads.created'}[sortby[0]]
+ except KeyError:
+ return redirect(url_for('forum.view_forum',forum_id=forum['id']))
+
+ avail_tags = get_avail_tags(forum['id'])
+
+ tagfilter = request.args.get("tagfilter",None)
+ if tagfilter == "":
+ tagfilter = None
+ tagfilter_clause = ""
+ tagfilter_tag = None
+ if tagfilter is not None:
+ try:
+ tagfilter = int(tagfilter)
+ except ValueError:
+ flash(f'invalid tag id "{tagfilter}"')
+ return redirect(url_for('forum.view_forum',forum_id=forum['id']))
+ else:
+ # there is no risk of sql injection because
+ # we just checked it is an int
+ tagfilter_clause = f"AND thread_tags.tag = {tagfilter}"
+ for the_tag in avail_tags:
+ if the_tag['id'] == tagfilter:
+ tagfilter_tag = the_tag
+ break
+ else:
+ flash("that tag doesn't exist or isn't available here")
+ return redirect(url_for('forum.view_forum',forum_id=forum['id']))
+
+
threads = db.execute(
- """SELECT
+ f"""SELECT
threads.id, threads.title, threads.creator, threads.created,
threads.updated, threads.poll, number_of_posts.num_replies,
most_recent_posts.created as mrp_created,
@@ -108,8 +142,10 @@ def view_forum(forum,page=1):
FROM threads
INNER JOIN most_recent_posts ON most_recent_posts.thread = threads.id
INNER JOIN number_of_posts ON number_of_posts.thread = threads.id
- WHERE threads.forum = ?
- ORDER BY threads.updated DESC
+ LEFT OUTER JOIN thread_tags ON threads.id = thread_tags.thread
+ WHERE threads.forum = ? {tagfilter_clause}
+ GROUP BY threads.id
+ ORDER BY {sortby_by} {sortby_dir}
LIMIT ? OFFSET ?;
""",(
forum['id'],
@@ -117,14 +153,17 @@ def view_forum(forum,page=1):
(page-1)*THREADS_PER_PAGE,
)).fetchall()
- # XXX: update this when thread filtering happens
- num_threads = db.execute("SELECT count(*) AS count FROM threads WHERE threads.forum = ?",(forum['id'],)).fetchone()['count']
+ num_threads = db.execute(f"""
+ SELECT count(*) AS count FROM threads
+ LEFT OUTER JOIN thread_tags ON threads.id = thread_tags.thread
+ WHERE threads.forum = ? {tagfilter_clause};
+ """,(forum['id'],)).fetchone()['count']
+
max_pageno = math.ceil(num_threads/THREADS_PER_PAGE)
thread_tags = {}
thread_polls = {}
- avail_tags = get_avail_tags(forum['id'])
#todo: somehow optimise this
for thread in threads:
@@ -190,6 +229,8 @@ def view_forum(forum,page=1):
avail_tags=avail_tags,
max_pageno=max_pageno,
page=page,
+ current_sortby=sortby,
+ tagfilter_tag=tagfilter_tag,
)
@forum_route("create_thread",methods=("GET","POST"))