From e5e68826fb09e427c6c32856ebcd2ad4902ae844 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 20 Aug 2023 22:51:21 +0100 Subject: [PATCH] Allow ordering packages in collections --- app/blueprints/collections/__init__.py | 14 ++++--- app/models/collections.py | 2 +- app/public/static/collection_editor.js | 23 ++++++++++- app/templates/collections/create_edit.html | 45 +++++++++++++--------- 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/app/blueprints/collections/__init__.py b/app/blueprints/collections/__init__.py index 25acb280..c925e270 100644 --- a/app/blueprints/collections/__init__.py +++ b/app/blueprints/collections/__init__.py @@ -84,6 +84,7 @@ class CollectionForm(FlaskForm): min_entries=0) package_ids = FieldList(HiddenField(), min_entries=0) package_removed = FieldList(HiddenField(), min_entries=0) + order = HiddenField() submit = SubmitField(lazy_gettext("Save")) @@ -178,7 +179,10 @@ def handle_create_edit(collection: Collection, form: CollectionForm, form.populate_obj(collection) collection.name = name - order = 1 + item_lookup = {} + for link in collection.items: + item_lookup[link.package.get_id()] = link + for i, package_id in enumerate(form.package_ids): link = next((x for x in collection.items if str(x.package.get_id()) == package_id.data), None) to_delete = form.package_removed[i].data == "1" @@ -194,15 +198,15 @@ def handle_create_edit(collection: Collection, form: CollectionForm, link.package = package link.collection = collection link.description = form.descriptions[i].data - link.order = order - order += 1 + item_lookup[link.package.get_id()] = link db.session.add(link) elif to_delete: db.session.delete(link) else: link.description = form.descriptions[i].data - link.order = order - order += 1 + + for i, package_id in enumerate(form.order.data.split(",")): + item_lookup[package_id].order = i + 1 add_audit_log(severity, current_user, f"Edited collection {collection.author.username}/{collection.name}", diff --git a/app/models/collections.py b/app/models/collections.py index 113c5639..c0c1882c 100644 --- a/app/models/collections.py +++ b/app/models/collections.py @@ -57,7 +57,7 @@ class Collection(db.Model): private = db.Column(db.Boolean, nullable=False, default=False) packages = db.relationship("Package", secondary=CollectionPackage.__table__, backref="collections") - items = db.relationship("CollectionPackage", back_populates="collection", order_by=db.asc("created_at"), + items = db.relationship("CollectionPackage", back_populates="collection", order_by=db.asc("order"), cascade="all, delete, delete-orphan") collection_name_valid = db.CheckConstraint("name ~* '^[a-z0-9_]+$' AND name != '_game'") diff --git a/app/public/static/collection_editor.js b/app/public/static/collection_editor.js index 79bacdb5..7aba9719 100644 --- a/app/public/static/collection_editor.js +++ b/app/public/static/collection_editor.js @@ -2,6 +2,18 @@ // @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later +function updateOrder() { + const elements = [...document.querySelector(".sortable").children]; + const ids = elements + .filter(x => !x.classList.contains("d-none")) + .map(x => x.dataset.id) + .filter(x => x); + + console.log(ids); + document.querySelector("input[name='order']").value = ids.join(","); +} + + function removePackage(card) { const message = document.getElementById("confirm_delete").innerText.trim(); const title = card.querySelector("h5 a").innerText.trim(); @@ -12,6 +24,7 @@ function removePackage(card) { card.querySelector("input[name^=package_removed]").value = "1"; card.classList.add("d-none"); onPackageQueryUpdate(); + updateOrder(); } @@ -26,6 +39,7 @@ function restorePackage(id) { card.querySelector("input[name^=package_removed]").value = "0"; card.scrollIntoView(); onPackageQueryUpdate(); + updateOrder(); return true; } @@ -59,7 +73,7 @@ function addPackage(pkg) { const url = `/packages/${id}/`; const temp = document.createElement("div"); temp.innerHTML = ` -
+
-
+
{% for item in collection.items %} {% set package = item.package %} -
+
- -
- - {{ _("%(title)s by %(author)s", title=package.title, author=package.author.display_name) }} - -
-

- {{ package.short_desc }} -

- {{ render_field(form.descriptions[loop.index - 1], hint=_("You can replace the description with your own")) }} - {{ form.package_ids[loop.index - 1]() }} - {{ form.package_removed[loop.index - 1]() }} +
+
+ +
+
+ +
+ + {{ _("%(title)s by %(author)s", title=package.title, author=package.author.display_name) }} + +
+

+ {{ package.short_desc }} +

+ {{ render_field(form.descriptions[loop.index - 1], hint=_("You can replace the description with your own")) }} + {{ form.package_ids[loop.index - 1]() }} + {{ form.package_removed[loop.index - 1]() }} +
+
{% endfor %}
+ {{ form.order() }} {% endif %}