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))