aboutsummaryrefslogtreecommitdiffhomepage
path: root/apioforum/forum.py
diff options
context:
space:
mode:
Diffstat (limited to 'apioforum/forum.py')
-rw-r--r--apioforum/forum.py75
1 files changed, 28 insertions, 47 deletions
diff --git a/apioforum/forum.py b/apioforum/forum.py
index 93dbddb..e8abe96 100644
--- a/apioforum/forum.py
+++ b/apioforum/forum.py
@@ -26,6 +26,7 @@ class Forum(DBObj,table="forums"):
return tags
+
THREADS_PER_PAGE = 35
bp = Blueprint("forum", __name__, url_prefix="/")
@@ -74,6 +75,29 @@ def requires_bureaucrat(f):
return wrapper
+def make_tagfilter_clause(request_arg,avail_tags):
+ # returns sql clause that matches tags
+ # that have the id contained in tagfilter_arg, if not empty
+ # also returns the tag in question
+ if request_arg == "":
+ return "", None
+
+ try:
+ tagid = int(request_arg)
+ except ValueError:
+ flash(f'invalid tag id "{request_arg}"')
+ abort(400)
+ else:
+ clause = f"AND thread_tags.tag = {tagid}"
+
+ # now find the tag in question
+ for tag in avail_tags:
+ if tag['id'] == tagid:
+ return clause, tag
+
+ flash("that tag doesn't exist or isn't available here")
+ abort(400)
+
@forum_route("",pagination=True)
@requires_permission("p_view_forum", login_required=False)
def view_forum(forum,page=1):
@@ -89,31 +113,10 @@ def view_forum(forum,page=1):
abort(400)
avail_tags = forum.avail_tags()
+ tagfilter_clause, tagfilter_tag =
+ make_tagfilter_clause(request.args.get("tagfilter",""),avail_tags)
- 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}"')
- abort(400)
- 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")
- abort(400)
-
-
+ # all threads on this page
threads = Thread.from_row_list(db.execute(
f"""select * from threads
left outer join thread_tags on threads.id = thread_tags.thread
@@ -122,30 +125,8 @@ def view_forum(forum,page=1):
limit ? offset ?;
""",(forum.id, THREADS_PER_PAGE, (page-1)*THREADS_PER_PAGE)).fetchall())
- # i want to preserve this
- #threads = db.execute(
- # 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,
- # most_recent_posts.author as mrp_author,
- # most_recent_posts.id as mrp_id,
- # most_recent_posts.content as mrp_content,
- # most_recent_posts.deleted as mrp_deleted
- # 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
- # 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,
- # THREADS_PER_PAGE,
- # (page-1)*THREADS_PER_PAGE,
- # )).fetchall()
+ # total number of threads in this forum, after tag filtering (for pagination bar)
num_threads = db.execute(f"""
SELECT count(*) AS count FROM threads
LEFT OUTER JOIN thread_tags ON threads.id = thread_tags.thread