From 3b7377581cce6704aedda6aca285187c1eaf58b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Madis=20M=C3=A4gi?= <madis.magi@k-space.ee>
Date: Tue, 22 Oct 2024 20:51:55 +0300
Subject: [PATCH] Add archiving items support

---
 inventory-app/inventory.py                  | 12 ++++++++++++
 inventory-app/templates/inventory_view.html | 11 +++++++++++
 2 files changed, 23 insertions(+)

diff --git a/inventory-app/inventory.py b/inventory-app/inventory.py
index 7aa3712..f035475 100644
--- a/inventory-app/inventory.py
+++ b/inventory-app/inventory.py
@@ -259,6 +259,18 @@ def save_inventory_item(item_id=None, **_):
         return render_template("inventory_edit.html", **locals())
     return redirect("/m/inventory/%s/view" % item_id)
 
+@page_inventory.route("/m/inventory/<item_id>/archive", methods=['POST'])
+@login_required
+def archive_inventory_item(item_id):
+    item = db.inventory.find_one({"_id": ObjectId(item_id)})
+    if not item:
+        return abort(404)
+    if not check_edit_permission(item_id):
+        return abort(403)
+    db.archive.insert_one(item);
+    db.inventory.delete_one({"_id": ObjectId(item_id)})
+    return redirect("/m/inventory")
+
 @page_inventory.route("/m/inventory/add-slug/<slug>", methods=['GET'])
 @login_required
 def add_inventory_slug(slug):
diff --git a/inventory-app/templates/inventory_view.html b/inventory-app/templates/inventory_view.html
index b3b2e5f..df6ea35 100644
--- a/inventory-app/templates/inventory_view.html
+++ b/inventory-app/templates/inventory_view.html
@@ -206,6 +206,17 @@
     </button>
   </div>
 </div>
+
+<div class="row">
+  <div class="col s12">
+    <form action="/m/inventory/{{ item._id }}/archive" method="post" style="display: inline;">
+      <button {% if not can_edit %} disabled="" {% endif %} class="red lighten-1 waves-effect waves-light btn" type="submit">
+        <i class="material-icons left">archive</i>Archive
+      </button>
+    </form>
+  </div>
+</div>
+
 {% if can_audit %}
 <div id="audit-modal" class="modal">
   <div class="modal-content">