From 57ba3e8700ac0176b71c6b99aae2a8e63da0043e Mon Sep 17 00:00:00 2001
From: rubenwardy
Date: Sat, 19 Aug 2023 01:25:13 +0100
Subject: [PATCH] Add ability to remove packages from collection page
---
app/blueprints/collections/__init__.py | 6 ++++-
app/public/static/collection_editor.js | 26 ++++++++++++++++++++++
app/templates/collections/create_edit.html | 12 +++++++++-
3 files changed, 42 insertions(+), 2 deletions(-)
create mode 100644 app/public/static/collection_editor.js
diff --git a/app/blueprints/collections/__init__.py b/app/blueprints/collections/__init__.py
index f7b92c7a..dd2ac047 100644
--- a/app/blueprints/collections/__init__.py
+++ b/app/blueprints/collections/__init__.py
@@ -82,6 +82,7 @@ class CollectionForm(FlaskForm):
StringField(lazy_gettext("Short Description"), [Optional(), Length(0, 500)], filters=[nonempty_or_none]),
min_entries=0)
package_ids = FieldList(HiddenField(), min_entries=0)
+ package_removed = FieldList(HiddenField(), min_entries=0)
submit = SubmitField(lazy_gettext("Save"))
@@ -122,6 +123,7 @@ def create_edit(author=None, name=None):
for item in collection.items:
form.descriptions.append_entry(item.description)
form.package_ids.append_entry(item.package.id)
+ form.package_removed.append_entry("0")
else:
form.name = None
@@ -178,9 +180,11 @@ def handle_create_edit(collection: Collection, form: CollectionForm,
for i, package_id in enumerate(form.package_ids):
item = next((x for x in collection.items if str(x.package.id) == package_id.data), None)
if item is None:
- abort(400)
+ continue
item.description = form.descriptions[i].data
+ if form.package_removed[i].data == "1":
+ db.session.delete(item)
add_audit_log(severity, current_user,
f"Edited collection {collection.author.username}/{collection.name}",
diff --git a/app/public/static/collection_editor.js b/app/public/static/collection_editor.js
new file mode 100644
index 00000000..f3c587ac
--- /dev/null
+++ b/app/public/static/collection_editor.js
@@ -0,0 +1,26 @@
+// @author rubenwardy
+// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
+
+
+function handleRemovePackage(card) {
+ if (!confirm(card.getAttribute("data-delete-confirm"))) {
+ return;
+ }
+ card.querySelector("input[name^=package_removed]").value = "1";
+ card.classList.add("d-none");
+}
+
+window.onload = () => {
+ console.log("Loaded");
+ document.querySelectorAll(".remove-package").forEach(button => {
+ const card = button.parentNode.parentNode;
+ const field = card.querySelector("input[name^=package_removed]");
+
+ // Reloading/validation errors will cause this to be 1 at load
+ if (field && field.value === "1") {
+ card.classList.add("d-none");
+ } else {
+ button.addEventListener("click", () => handleRemovePackage(card));
+ }
+ });
+};
diff --git a/app/templates/collections/create_edit.html b/app/templates/collections/create_edit.html
index b9da6d8c..9b50f375 100644
--- a/app/templates/collections/create_edit.html
+++ b/app/templates/collections/create_edit.html
@@ -8,6 +8,10 @@
{% endif %}
{% endblock %}
+{% block scriptextra %}
+
+{% endblock %}
+
{% block content %}
{% from "macros/forms.html" import render_field, render_submit_field, render_checkbox_field, render_field_prefix_button %}
{% for item in collection.items %}
{% set package = item.package %}
-
+
{% endfor %}