aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorcitrons <citrons@mondecitronne.com>2025-06-16 21:25:17 -0500
committerubq323 <ubq323@ubq323.website>2025-06-17 12:05:16 +0100
commitd3cfe12fc80fc26101b45e7c7eae68c6f33a8977 (patch)
treee8f9357fb30ddde2390c21dd490f0e3208de9877
parent314a0e4ed739776b7d3c4ee08d2ceeee8248a421 (diff)
unify thread creation and modification
principally, allow tags to be added when creating a post. this should increase usage of tags in general.
-rw-r--r--apioforum/forum.py8
-rw-r--r--apioforum/templates/config_thread.html31
-rw-r--r--apioforum/thread.py33
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 %}<h1>{% block title %}configure thread '{{thread.title}}'{% endblock %}</h1>{% endblock %}
+{% block header %}
+<h1>
+ {% block title %}
+ {% if thread %}configure thread '{{thread.title}}'
+ {% else %}create new thread{% endif %}
+ {% endblock %}
+</h1>
+{% endblock %}
+
{% block content %}
<h2>thread options</h2>
<form method="post">
<fieldset>
<legend>title</legend>
<label for="title">thread title</label>
-<input type="text" id="title" name="title" value="{{thread.title}}">
+<input type="text" id="title" name="title" {%if thread%}value="{{thread.title}}"{%endif%}>
+{% if not thread %}
+ <br>
+ <label for="content">thread content</label>
+ <textarea name="content" id="content" class="new-post-box" placeholder="thread content here"></textarea>
+{% endif %}
+
</fieldset>
<fieldset>
<legend>tags</legend>
@@ -24,12 +38,17 @@
<p>there are no available tags.</p>
{% endif %}
</fieldset>
-<p>confirm changes?</p>
-<input type="submit" value="confirm">
-<a href="{{url_for('thread.view_thread',thread_id=thread.id)}}">cancel</a>
+{% if thread %}
+ <p>confirm changes?</p>
+ <input type="submit" value="confirm">
+ <a href="{{url_for('thread.view_thread',thread_id=thread.id)}}">cancel</a>
+{% else %}
+ <input type="submit" value="create thread">
+ <a href="{{url_for('forum.view_forum',forum_id=forum.id)}}">cancel</a>
+{% endif %}
</form>
-{% 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 %}
<h2>create poll</h2>
<form method="post" action="{{url_for('thread.create_poll',thread_id=thread.id)}}">
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("/<int:thread_id>/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")