add delete post route

This commit is contained in:
2026-05-20 00:07:06 +03:00
parent d74dd6c5f3
commit a5a3565496
5 changed files with 95 additions and 6 deletions

View File

@@ -1,9 +1,10 @@
from flask import Blueprint, abort, render_template, redirect, url_for, request
from flask import Blueprint, abort, render_template, redirect, url_for, request, flash
from functools import wraps
from ..auth import login_required, get_active_user
from ..models import Posts, Threads
from ..models import Posts, Threads, Topics
from ..util import get_post_url
from ..db import db
from ..constants import InfoboxKind
bp = Blueprint('posts', __name__, url_prefix='/posts/')
@@ -39,12 +40,15 @@ def ownership_or_mod_required(view_func):
@ownership_required
def edit(post_id):
post = Posts.find({'id': post_id})
if not post:
abort(404)
thread = Threads.find({'id': post.thread_id})
user = get_active_user()
if not thread:
# what?
abort(404)
user = get_active_user()
if thread.locked() and not user.is_mod():
abort(403)
@@ -66,12 +70,15 @@ def edit(post_id):
@ownership_required
def edit_post(post_id):
post = Posts.find({'id': post_id})
if not post:
abort(404)
thread = Threads.find({'id': post.thread_id})
user = get_active_user()
if not thread:
# what?
abort(404)
user = get_active_user()
if thread.locked() and not user.is_mod():
abort(403)
@@ -83,4 +90,46 @@ def edit_post(post_id):
@login_required
@ownership_or_mod_required
def delete(post_id):
return 'stub'
post = Posts.find({'id': post_id})
if not post:
abort(404)
thread = Threads.find({'id': post.thread_id})
if not thread:
# what?
abort(404)
user = get_active_user()
if thread.locked() and not user.is_mod():
abort(403)
return render_template('posts/delete.html', post=post.get_full_post_view())
@bp.post('/<int:post_id>/delete/')
@login_required
@ownership_or_mod_required
def delete_post(post_id):
post = Posts.find({'id': post_id})
if not post:
abort(404)
thread = Threads.find({'id': post.thread_id})
if not thread:
# what?
abort(404)
user = get_active_user()
if thread.locked() and not user.is_mod():
abort(403)
post.delete()
if Posts.count({'thread_id': thread.id}) == 0:
topic = Topics.find({'id': thread.topic_id})
thread.delete()
flash('Thread deleted.', InfoboxKind.INFO)
return redirect(url_for('topics.topic_by_id', topic_id=topic.id))
flash('Post deleted.', InfoboxKind.INFO)
return redirect(url_for('threads.thread_by_id', thread_id=thread.id))