From 3f545238d787dfd0be76d343e82cc94c0705a0f2 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Thu, 18 Oct 2018 13:45:21 +0200
Subject: [PATCH] Allow to delete comments

JIRA INFRA-618
---
 app/inventory/views.py      | 14 ++++++++++++++
 app/templates/_helpers.html |  7 +++++++
 2 files changed, 21 insertions(+)

diff --git a/app/inventory/views.py b/app/inventory/views.py
index 6a24ab3..93c0b4f 100644
--- a/app/inventory/views.py
+++ b/app/inventory/views.py
@@ -135,6 +135,20 @@ def edit_comment(comment_id):
     )
 
 
+@bp.route("/items/comment/delete", methods=["POST"])
+@login_groups_accepted("admin", "create")
+def delete_comment():
+    comment = models.ItemComment.query.get_or_404(request.form["comment_id"])
+    ics_id = comment.item.ics_id
+    # Explicitely remove the comment from the item to make sure
+    # it will be re-indexed
+    comment.item.comments.remove(comment)
+    db.session.delete(comment)
+    db.session.commit()
+    flash(f"Comment {comment.id} has been deleted", "success")
+    return redirect(url_for("inventory.view_item", ics_id=ics_id))
+
+
 @bp.route("/items/edit/<ics_id>", methods=("GET", "POST"))
 @login_groups_accepted("admin", "create")
 def edit_item(ics_id):
diff --git a/app/templates/_helpers.html b/app/templates/_helpers.html
index daa1c7f..d201040 100644
--- a/app/templates/_helpers.html
+++ b/app/templates/_helpers.html
@@ -227,6 +227,13 @@
       {% if comment.updated_at != comment.created_at %}
         <small class="">Edited {{ format_datetime(comment.updated_at) }}</small>
       {% endif %}
+      <div class="float-sm-right">
+        <form method="POST" action="/inventory/items/comment/delete">
+          <input id="comment_id" name="comment_id" type="hidden" value="{{ comment.id }}">
+          {{ delete_button_with_confirmation("Delete comment", "deleteConfirmation-%s" | format(comment.id),
+             "Are you sure you want to delete this comment?") }}
+        </form>
+      </div>
       <a class="btn btn-light float-sm-right" href="{{ url_for('inventory.edit_comment', comment_id=comment.id) }}#body">
         <span class="oi oi-pencil" title="Edit comment" aria-hidden="true"></span>
       </a>
-- 
GitLab