From 14f72326212adc91f27c436e201a19c431eab3e2 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 22 Jul 2021 18:35:01 +0000 Subject: add ability to sort threads by creation or modification, asc or desc --- apioforum/forum.py | 13 +++++++++++-- apioforum/static/style.css | 3 ++- apioforum/templates/view_forum.html | 24 ++++++++++++++++++++---- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/apioforum/forum.py b/apioforum/forum.py index 305cb51..bf9610d 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -96,8 +96,16 @@ 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)) + 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, @@ -109,7 +117,7 @@ def view_forum(forum,page=1): 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 + ORDER BY {sortby_by} {sortby_dir} LIMIT ? OFFSET ?; """,( forum['id'], @@ -190,6 +198,7 @@ def view_forum(forum,page=1): avail_tags=avail_tags, max_pageno=max_pageno, page=page, + current_sortby=sortby ) @forum_route("create_thread",methods=("GET","POST")) diff --git a/apioforum/static/style.css b/apioforum/static/style.css index d50c7b0..cd275ff 100644 --- a/apioforum/static/style.css +++ b/apioforum/static/style.css @@ -291,4 +291,5 @@ fieldset { margin-bottom: 15px; } content: "/\00a0"; padding: 8px; } - + + diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 649c87c..c1906b6 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -83,7 +83,26 @@ please log in to create a new thread you do not have permission to create threads in this forum {% endif %} -{% if has_permission(forum.id, g.user, "p_view_threads", login_required=False) %} +{% macro sortby_option(code, text) %} +{% if current_sortby==code %} + +{% else %} + +{% endif %} +{% endmacro %} + +
+ + + +
+

+ {{ pagination_nav(page,max_pageno,'forum.view_forum',forum_id=forum.id) }}
{%for thread in threads%} @@ -136,7 +155,4 @@ you do not have permission to create threads in this forum
-{% else %} -

you do not have permission to view threads in this forum

-{% endif %} {%endblock%} -- cgit v1.2.3 From 95744c80aae576aabd8961ecee2035b317a5849c Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 14 May 2022 01:25:10 +0100 Subject: add tag filtering the only currently supported form of tag filtering is by specifying a single tag, and then only threads with that tag will be displayed. this meets all of the reasonable use cases i can think of right now. --- apioforum/forum.py | 41 +++++++++++++++++++++++++++++++------ apioforum/templates/common.html | 16 +++++++++++++-- apioforum/templates/view_forum.html | 33 ++++++++++++++++++++++------- 3 files changed, 75 insertions(+), 15 deletions(-) diff --git a/apioforum/forum.py b/apioforum/forum.py index bf9610d..7f9b0b6 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -102,7 +102,30 @@ def view_forum(forum,page=1): 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)) + return redirect(url_for('forum.view_forum',forum_id=forum['id'])) + + avail_tags = get_avail_tags(forum['id']) + + tagfilter = request.args.get("tagfilter",None) + tagfilter_clause = "" + tagfilter_tag = None + if tagfilter is not None: + try: + tagfilter = int(tagfilter) + except ValueError: + 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( f"""SELECT @@ -116,7 +139,9 @@ 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 = ? + 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 ?; """,( @@ -125,14 +150,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: @@ -198,7 +226,8 @@ def view_forum(forum,page=1): avail_tags=avail_tags, max_pageno=max_pageno, page=page, - current_sortby=sortby + current_sortby=sortby, + tagfilter_tag=tagfilter_tag, ) @forum_route("create_thread",methods=("GET","POST")) diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html index 46402df..fae4b7c 100644 --- a/apioforum/templates/common.html +++ b/apioforum/templates/common.html @@ -67,8 +67,20 @@ {%- endmacro %} -{% macro tag(the_tag) -%} -{{the_tag.name}} +{% macro tag(the_tag,href=None) -%} +{% if href is none %} +{% set el = "span" %} +{% else %} +{% set el = "a" %} +{% endif %} +<{{el}} + class="tag" + style="color: {{the_tag.text_colour}}; background-color: {{the_tag.bg_colour}}" + {% if href is not none -%} + href="{{href}}" + {%- endif -%}> + {{-the_tag.name-}} + {%- endmacro %} {% macro ab(name,href) -%} diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index c1906b6..bac06e4 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -26,13 +26,6 @@

your role in this forum: {{role}}

{% endif %} -

available tags: - {% for the_tag in avail_tags %} - {{tag(the_tag)}} - {% else %} - (none available) - {% endfor %} -

@@ -91,6 +84,12 @@ you do not have permission to create threads in this forum {% endif %} {% endmacro %} +

+ sorting and filtering options +
+ {% if tagfilter_tag is not none %} + + {% endif %}
+ +{% if tagfilter_tag is none %} +

filter by tag: +{% for the_tag in avail_tags %} +{{tag(the_tag,href=url_for('forum.view_forum',forum_id=forum.id, + sortby=current_sortby,tagfilter=the_tag.id))}} + +{% else %} +(none available) +{% endfor %} +

+{% else %} +

only showing posts with the {{tag(tagfilter_tag)}} tag. + stop filtering +

+{% endif %} +

{{ pagination_nav(page,max_pageno,'forum.view_forum',forum_id=forum.id) }} -- cgit v1.2.3 From b2ac344c4bb4e2fed50a5e6925cdfef9c8cd041a Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 11 Jun 2022 00:03:48 +0100 Subject: make ui of tag filtering use radio buttons instead of links --- apioforum/forum.py | 3 +++ apioforum/templates/view_forum.html | 39 +++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/apioforum/forum.py b/apioforum/forum.py index 7f9b0b6..988c9a5 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -107,12 +107,15 @@ def view_forum(forum,page=1): 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 diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index bac06e4..4cc5f5d 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -84,6 +84,7 @@ you do not have permission to create threads in this forum {% endif %} {% endmacro %} +
sorting and filtering options
+
- - {% if tagfilter_tag is not none %} - - {% endif %} - +

filter by tag:

+ + -{% if tagfilter_tag is none %} -

filter by tag: -{% for the_tag in avail_tags %} -{{tag(the_tag,href=url_for('forum.view_forum',forum_id=forum.id, - sortby=current_sortby,tagfilter=the_tag.id))}} + {% for the_tag in avail_tags %} +
+ + + {% endfor %} +

+ + + +

+ -{% else %} -(none available) -{% endfor %} -

-{% else %} -

only showing posts with the {{tag(tagfilter_tag)}} tag. - stop filtering -

-{% endif %}

-- cgit v1.2.3 From 421e5d579a005214a7ff3f3305b76e8be8037694 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 11 Jun 2022 00:21:48 +0100 Subject: make radio buttons inline instead of on multiple lines --- apioforum/templates/view_forum.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 4cc5f5d..b03d51a 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -105,7 +105,7 @@ you do not have permission to create threads in this forum {% for the_tag in avail_tags %} -
+ @@ -113,7 +113,7 @@ you do not have permission to create threads in this forum {{tag(the_tag)}} {% endfor %} -

+
-- cgit v1.2.3