From fd0b203f1e001152dfaf49e49abccf157cd940e3 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 9 Dec 2020 18:40:25 +0000 Subject: [PATCH] Add ability to delete threads This reverts commit 78630b30715cb46b5695f8aba8443ffcfd4c67e5. --- app/blueprints/threads/__init__.py | 25 +++++++++++++++++++++++- app/models.py | 5 +++-- app/templates/threads/delete_thread.html | 22 +++++++++++++++++++++ app/templates/threads/view.html | 7 +++++-- 4 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 app/templates/threads/delete_thread.html diff --git a/app/blueprints/threads/__init__.py b/app/blueprints/threads/__init__.py index c19d823..e99136f 100644 --- a/app/blueprints/threads/__init__.py +++ b/app/blueprints/threads/__init__.py @@ -22,12 +22,12 @@ bp = Blueprint("threads", __name__) from flask_login import current_user, login_required from app.models import * from app.utils import addNotification, isYes, addAuditLog - from flask_wtf import FlaskForm from wtforms import * from wtforms.validators import * from app.utils import get_int_or_abort + @bp.route("/threads/") def list_all(): query = Thread.query @@ -107,6 +107,29 @@ def set_lock(id): @bp.route("/threads//delete/", methods=["GET", "POST"]) @login_required +def delete_thread(id): + thread = Thread.query.get(id) + if thread is None or not thread.checkPerm(current_user, Permission.DELETE_THREAD): + abort(404) + + if request.method == "GET": + return render_template("threads/delete_thread.html", thread=thread) + + summary = "\n\n".join([("<{}> {}".format(reply.author.display_name, reply.comment)) for reply in thread.replies]) + + msg = "Deleted thread {} by {}".format(thread.title, thread.author.display_name) + + db.session.delete(thread) + + addAuditLog(AuditSeverity.MODERATION, current_user, msg, None, thread.package, summary) + + db.session.commit() + + return redirect(url_for("homepage.home")) + + +@bp.route("/threads//delete-reply/", methods=["GET", "POST"]) +@login_required def delete_reply(id): thread = Thread.query.get(id) if thread is None: diff --git a/app/models.py b/app/models.py index 4212d3d..eb5fb0d 100644 --- a/app/models.py +++ b/app/models.py @@ -94,6 +94,7 @@ class Permission(enum.Enum): CREATE_THREAD = "CREATE_THREAD" COMMENT_THREAD = "COMMENT_THREAD" LOCK_THREAD = "LOCK_THREAD" + DELETE_THREAD = "DELETE_THREAD" DELETE_REPLY = "DELETE_REPLY" EDIT_REPLY = "EDIT_REPLY" UNAPPROVE_PACKAGE = "UNAPPROVE_PACKAGE" @@ -1352,7 +1353,7 @@ class Thread(db.Model): created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) replies = db.relationship("ThreadReply", back_populates="thread", lazy="dynamic", - order_by=db.asc("thread_reply_id")) + order_by=db.asc("thread_reply_id"), cascade="all, delete, delete-orphan") watchers = db.relationship("User", secondary=watchers, lazy="subquery", backref="watching") @@ -1386,7 +1387,7 @@ class Thread(db.Model): elif perm == Permission.COMMENT_THREAD: return canSee and (not self.locked or user.rank.atLeast(UserRank.MODERATOR)) - elif perm == Permission.LOCK_THREAD: + elif perm == Permission.LOCK_THREAD or perm == Permission.DELETE_THREAD: return user.rank.atLeast(UserRank.MODERATOR) else: diff --git a/app/templates/threads/delete_thread.html b/app/templates/threads/delete_thread.html new file mode 100644 index 0000000..7da540a --- /dev/null +++ b/app/templates/threads/delete_thread.html @@ -0,0 +1,22 @@ +{% extends "base.html" %} + +{% block title %} + Delete thread in {{ thread.title }} +{% endblock %} + +{% block content %} +
+ + +

Delete {{ thread.title }} by {{ thread.author.display_name }}

+
+ {{ thread.replies[0].comment | markdown }} +
+
+

Deleting is permanent

+ + Cancel + +
+
+{% endblock %} diff --git a/app/templates/threads/view.html b/app/templates/threads/view.html index f92ef39..7c6987b 100644 --- a/app/templates/threads/view.html +++ b/app/templates/threads/view.html @@ -17,16 +17,19 @@ {% endif %} + {% if thread and thread.checkPerm(current_user, "DELETE_THREAD") %} + {{ _('Delete') }} + {% endif %} {% if thread and thread.checkPerm(current_user, "LOCK_THREAD") %} {% if thread.locked %}
- +
{% else %}
- +
{% endif %} {% endif %}