From cf2d60507774e662a3f94cd84ed74bcdd1d8f70a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sun, 10 Aug 2025 06:28:42 +0300 Subject: [PATCH] delete thread when no more posts remain --- app/routes/posts.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/routes/posts.py b/app/routes/posts.py index afd9e8c..7f695c8 100644 --- a/app/routes/posts.py +++ b/app/routes/posts.py @@ -5,7 +5,7 @@ from .users import login_required, get_active_user from ..lib.babycode import babycode_to_html from ..constants import InfoboxKind from ..db import db -from ..models import Posts, PostHistory, Threads +from ..models import Posts, PostHistory, Threads, Topics bp = Blueprint("posts", __name__, url_prefix = "/post") @@ -50,10 +50,25 @@ def delete(post_id): post = Posts.find({'id': post_id}) thread = Threads.find({'id': post.thread_id}) user = get_active_user() + if not user: + return redirect(url_for('threads.thread', slug=thread.slug)) if user.is_mod() or post.user_id == user.id: post.delete() - flash("Post deleted.", InfoboxKind.INFO) + + post_count = Posts.count({ + 'thread_id': thread.id, + }) + + if post_count == 0: + topic = Topics.find({ + 'id': thread.topic_id, + }) + thread.delete() + flash('Thread deleted.', InfoboxKind.INFO) + return redirect(url_for('topics.topic', slug=topic.slug)) + + flash('Post deleted.', InfoboxKind.INFO) return redirect(url_for('threads.thread', slug=thread.slug))