From d3cfe12fc80fc26101b45e7c7eae68c6f33a8977 Mon Sep 17 00:00:00 2001 From: citrons Date: Mon, 16 Jun 2025 21:25:17 -0500 Subject: unify thread creation and modification principally, allow tags to be added when creating a post. this should increase usage of tags in general. --- apioforum/forum.py | 8 +++++++- apioforum/templates/config_thread.html | 31 +++++++++++++++++++++++++------ apioforum/thread.py | 33 +++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/apioforum/forum.py b/apioforum/forum.py index 05fe231..49e0c73 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -267,6 +267,8 @@ def view_forum(forum,page=1): is_read=read.is_read, ) +from .thread import apply_selected_tags + @forum_route("create_thread",methods=("GET","POST")) @requires_permission("p_create_threads") @requires_permission("p_view_forum") @@ -299,6 +301,8 @@ def create_thread(forum): "INSERT INTO posts (thread,created,author,content) VALUES (?,current_timestamp,?,?);", (thread_id,g.user,content) ) + thread = {'id': thread_id, 'forum': forum['id']} + apply_selected_tags(thread, request.form) db.commit() set_updated(forum['id']) @@ -309,7 +313,9 @@ def create_thread(forum): flash(err) - return render_template("create_thread.html") + return render_template("config_thread.html", + forum=forum, + avail_tags=get_avail_tags(forum['id'])) @forum_route("roles",methods=("GET","POST")) @requires_bureaucrat diff --git a/apioforum/templates/config_thread.html b/apioforum/templates/config_thread.html index 3f2939c..cfbd8ef 100644 --- a/apioforum/templates/config_thread.html +++ b/apioforum/templates/config_thread.html @@ -1,13 +1,27 @@ {% extends 'base.html' %} {% from 'common.html' import tag %} -{% block header %}

{% block title %}configure thread '{{thread.title}}'{% endblock %}

{% endblock %} +{% block header %} +

+ {% block title %} + {% if thread %}configure thread '{{thread.title}}' + {% else %}create new thread{% endif %} + {% endblock %} +

+{% endblock %} + {% block content %}

thread options

title - + +{% if not thread %} +
+ + +{% endif %} +
tags @@ -24,12 +38,17 @@

there are no available tags.

{% endif %}
-

confirm changes?

- -cancel +{% if thread %} +

confirm changes?

+ + cancel +{% else %} + + cancel +{% endif %}
-{% if has_permission(thread.forum, g.user, "p_create_polls") %} +{% if thread and has_permission(thread.forum, g.user, "p_create_polls") %} {% if thread.poll is none %}

create poll

diff --git a/apioforum/thread.py b/apioforum/thread.py index 9352afb..98a4206 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -362,8 +362,24 @@ def view_post(post_id): abort(403) return render_template("view_post.html",post=post) - - + + +def apply_selected_tags(thread, form): + db = get_db() + avail_tags = get_avail_tags(thread['forum']) + thread_tags = [r['tag'] for r in db.execute("select tag from thread_tags where thread = ?",(thread['id'],)).fetchall()] + changed = False + for avail_tag in avail_tags: + tagid = avail_tag['id'] + current = tagid in thread_tags + wanted = f'tag_{tagid}' in form + if wanted and not current: + db.execute("insert into thread_tags (thread, tag) values (?,?)",(thread['id'],tagid)) + changed = True + elif current and not wanted: + db.execute("delete from thread_tags where thread = ? and tag = ?",(thread['id'],tagid)) + changed = True + return changed @bp.route("//config",methods=["GET","POST"]) def config_thread(thread_id): @@ -393,18 +409,7 @@ def config_thread(thread_id): db.execute("update threads set title = ? where id = ?;",(title,thread_id)) flash("title updated successfully") db.commit() - changed = False - 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: - db.execute("insert into thread_tags (thread, tag) values (?,?)",(thread_id,tagid)) - changed = True - elif current and not wanted: - db.execute("delete from thread_tags where thread = ? and tag = ?",(thread_id,tagid)) - changed = True - if changed: + if apply_selected_tags(thread, request.form): db.commit() flash("tags updated successfully") -- cgit v1.2.3