]> git.lizzy.rs Git - cheatdb.git/blobdiff - app/blueprints/threads/__init__.py
Implement package states for easier reviews
[cheatdb.git] / app / blueprints / threads / __init__.py
index e3043c0d68f4fff29dbcf82d55f3ee24d801924b..5380389da67a43e6e326d40c3cd7ccac58b21b5c 100644 (file)
@@ -1,4 +1,4 @@
-# Content DB
+# ContentDB
 # Copyright (C) 2018  rubenwardy
 #
 # This program is free software: you can redistribute it and/or modify
@@ -107,6 +107,81 @@ def set_lock(id):
        return redirect(thread.getViewURL())
 
 
+@bp.route("/threads/<int:id>/delete/", methods=["GET", "POST"])
+@login_required
+def delete_reply(id):
+       thread = Thread.query.get(id)
+       if thread is None:
+               abort(404)
+
+       reply_id = request.args.get("reply")
+       if reply_id is None:
+               abort(404)
+
+       reply = ThreadReply.query.get(reply_id)
+       if reply is None or reply.thread != thread:
+               abort(404)
+
+       if thread.replies[0] == reply:
+               flash("Cannot delete thread opening post!", "danger")
+               return redirect(thread.getViewURL())
+
+       if not reply.checkPerm(current_user, Permission.DELETE_REPLY):
+               abort(403)
+
+       if request.method == "GET":
+               return render_template("threads/delete_reply.html", thread=thread, reply=reply)
+
+       msg = "Deleted reply by {}".format(reply.author.display_name)
+       addAuditLog(AuditSeverity.MODERATION, current_user, msg, thread.getViewURL(), thread.package, reply.comment)
+
+       db.session.delete(reply)
+       db.session.commit()
+
+       return redirect(thread.getViewURL())
+
+
+class CommentForm(FlaskForm):
+       comment = TextAreaField("Comment", [InputRequired(), Length(10, 2000)])
+       submit  = SubmitField("Comment")
+
+
+@bp.route("/threads/<int:id>/edit/", methods=["GET", "POST"])
+@login_required
+def edit_reply(id):
+       thread = Thread.query.get(id)
+       if thread is None:
+               abort(404)
+
+       reply_id = request.args.get("reply")
+       if reply_id is None:
+               abort(404)
+
+       reply = ThreadReply.query.get(reply_id)
+       if reply is None or reply.thread != thread:
+               abort(404)
+
+       if not reply.checkPerm(current_user, Permission.EDIT_REPLY):
+               abort(403)
+
+       form = CommentForm(formdata=request.form, obj=reply)
+       if request.method == "POST" and form.validate():
+               comment = form.comment.data
+
+               msg = "Edited reply by {}".format(reply.author.display_name)
+               severity = AuditSeverity.NORMAL if current_user == reply.author else AuditSeverity.MODERATION
+               addNotification(reply.author, current_user, msg, thread.getViewURL(), thread.package)
+               addAuditLog(severity, current_user, msg, thread.getViewURL(), thread.package, reply.comment)
+
+               reply.comment = comment
+
+               db.session.commit()
+
+               return redirect(thread.getViewURL())
+
+       return render_template("threads/edit_reply.html", thread=thread, reply=reply, form=form)
+
+
 @bp.route("/threads/<int:id>/", methods=["GET", "POST"])
 def view(id):
        thread = Thread.query.get(id)
@@ -124,7 +199,7 @@ def view(id):
                        flash("Please wait before commenting again", "danger")
                        return redirect(thread.getViewURL())
 
-               if len(comment) <= 500 and len(comment) > 3:
+               if len(comment) <= 2000 and len(comment) > 3:
                        reply = ThreadReply()
                        reply.author = current_user
                        reply.comment = comment
@@ -141,17 +216,18 @@ def view(id):
                        return redirect(thread.getViewURL())
 
                else:
-                       flash("Comment needs to be between 3 and 500 characters.")
+                       flash("Comment needs to be between 3 and 2000 characters.")
 
        return render_template("threads/view.html", thread=thread)
 
 
 class ThreadForm(FlaskForm):
        title   = StringField("Title", [InputRequired(), Length(3,100)])
-       comment = TextAreaField("Comment", [InputRequired(), Length(10, 500)])
+       comment = TextAreaField("Comment", [InputRequired(), Length(10, 2000)])
        private = BooleanField("Private")
        submit  = SubmitField("Open Thread")
 
+
 @bp.route("/threads/new/", methods=["GET", "POST"])
 @login_required
 def new():
@@ -222,6 +298,10 @@ def new():
                if is_review_thread:
                        package.review_thread = thread
 
+                       if package.state == PackageState.READY_FOR_REVIEW and current_user not in package.maintainers:
+                               package.state = PackageState.CHANGES_NEEDED
+
+
                notif_msg = "New thread '{}'".format(thread.title)
                if package is not None:
                        addNotification(package.maintainers, current_user, notif_msg, thread.getViewURL(), package)