From 6e18d8bed80ae930cce191759aeb636395a001e2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 Jan 2025 15:18:14 -0500 Subject: [PATCH] edit collection item without reload page --- journal/models/itemlist.py | 4 +- journal/templates/_list_item.html | 6 +- journal/templates/collection_edit.html | 5 +- journal/templates/collection_items.html | 10 +-- .../collection_update_item_note.html | 10 +-- .../collection_update_item_note_ok.html | 6 ++ journal/tests.py | 2 +- journal/views/collection.py | 67 +++++++++++++------ 8 files changed, 76 insertions(+), 34 deletions(-) create mode 100644 journal/templates/collection_update_item_note_ok.html diff --git a/journal/models/itemlist.py b/journal/models/itemlist.py index 9d697ae4..7af30a09 100644 --- a/journal/models/itemlist.py +++ b/journal/models/itemlist.py @@ -76,7 +76,7 @@ class List(Piece): raise ValueError("item is None") member = self.get_member_for_item(item) if member: - return member + return member, False ml = self.ordered_members p = {"parent": self} p.update(params) @@ -88,7 +88,7 @@ class List(Piece): **p, ) list_add.send(sender=self.__class__, instance=self, item=item, member=member) - return member + return member, True def remove_item(self, item): member = self.get_member_for_item(item) diff --git a/journal/templates/_list_item.html b/journal/templates/_list_item.html index 27a2acf3..3b88af74 100644 --- a/journal/templates/_list_item.html +++ b/journal/templates/_list_item.html @@ -8,12 +8,14 @@ {% get_mark_for_item item as mark %} {% endif %}
{% if collection_edit %} @@ -86,7 +88,7 @@ {% endif %} {% endif %} {% if collection_member %} -
+
{% if collection_edit %} {% trans "Items" %}
{% csrf_token %} @@ -59,7 +61,7 @@ style="min-width:24rem" required> {% csrf_token %} diff --git a/journal/templates/collection_items.html b/journal/templates/collection_items.html index f811eaf5..7f8387ea 100644 --- a/journal/templates/collection_items.html +++ b/journal/templates/collection_items.html @@ -3,15 +3,17 @@ {% load l10n %} {% for member in members %} {% include '_list_item.html' with item=member.item mark=None collection_member=member %} + {% if forloop.counter == 10 %}
{% endif %} {% if forloop.last %}
- {{ member.position }} + hx-trigger="load delay:0.1s" + hx-swap="outerHTML" + class=".loader"> +
{% endif %} {% empty %} - {% if not request.GET.last_pos %} + {% if not request.GET.last_pos and not collection_edit %} {% trans "nothing so far." %} {% endif %} {% endfor %} diff --git a/journal/templates/collection_update_item_note.html b/journal/templates/collection_update_item_note.html index ccee3d0e..878f3dd6 100644 --- a/journal/templates/collection_update_item_note.html +++ b/journal/templates/collection_update_item_note.html @@ -1,9 +1,9 @@ {% load i18n %} - - + + + hx-get="{% url 'journal:collection_update_item_note' collection.uuid item.uuid %}?cancel=1" + hx-target="closest form" + hx-swap="outerHTML">{% trans "Cancel" %}
diff --git a/journal/templates/collection_update_item_note_ok.html b/journal/templates/collection_update_item_note_ok.html new file mode 100644 index 00000000..296a33da --- /dev/null +++ b/journal/templates/collection_update_item_note_ok.html @@ -0,0 +1,6 @@ +{% load i18n %} + +
+
+{{ collection_member.note|default:" " }} diff --git a/journal/tests.py b/journal/tests.py index 5d90c14c..9d505a5f 100644 --- a/journal/tests.py +++ b/journal/tests.py @@ -21,7 +21,7 @@ class CollectionTest(TestCase): Collection.objects.create(title="test", owner=self.user.identity) collection = Collection.objects.get(title="test", owner=self.user.identity) self.assertEqual(collection.catalog_item.title, "test") - member1 = collection.append_item(self.book1) + member1, _ = collection.append_item(self.book1) self.assertIsNotNone(member1) member1.note = "my notes" member1.save() diff --git a/journal/views/collection.py b/journal/views/collection.py index 97921006..d84e577f 100644 --- a/journal/views/collection.py +++ b/journal/views/collection.py @@ -1,6 +1,6 @@ from django.contrib.auth.decorators import login_required from django.core.exceptions import BadRequest, PermissionDenied -from django.http import HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse from django.utils.translation import gettext as _ @@ -196,7 +196,6 @@ def collection_retrieve_items( raise PermissionDenied(_("Insufficient permission")) members = collection.ordered_members last_pos = int_(request.GET.get("last_pos")) - print("p", last_pos) if last_pos: last_member = int_(request.GET.get("last_member")) members = members.filter(position__gte=last_pos).exclude(id=last_member) @@ -220,15 +219,29 @@ def collection_append_item(request: AuthedHttpRequest, collection_uuid): raise PermissionDenied(_("Insufficient permission")) url = request.POST.get("url", "") - note = request.POST.get("note") + note = request.POST.get("note", "") item = Item.get_by_url(url) + member = None if item: - collection.append_item(item, note=note) - collection.save() - msg = None + member, new = collection.append_item(item, note=note) + if new: + collection.save() + msg = None + else: + member = None + msg = _("The item is already in the collection.") else: msg = _("Unable to find the item, please use item url from this site.") - return collection_retrieve_items(request, collection_uuid, True, msg) + return render( + request, + "collection_items.html", + { + "collection": collection, + "members": [member] if member else [], + "collection_edit": True, + "msg": msg, + }, + ) @login_required @@ -239,7 +252,7 @@ def collection_remove_item(request: AuthedHttpRequest, collection_uuid, item_uui if not collection.is_editable_by(request.user): raise PermissionDenied(_("Insufficient permission")) collection.remove_item(item) - return collection_retrieve_items(request, collection_uuid, True) + return HttpResponse("") @login_required @@ -269,7 +282,16 @@ def collection_update_member_order(request: AuthedHttpRequest, collection_uuid): raise BadRequest(_("Invalid parameter")) ordered_member_ids = [int_(i) for i in ids.split(",")] collection.update_member_order(ordered_member_ids) - return collection_retrieve_items(request, collection_uuid, True) + return render( + request, + "collection_items.html", + { + "collection": collection, + "members": [], + "collection_edit": True, + "msg": _("Saved."), + }, + ) @login_required @@ -281,21 +303,28 @@ def collection_update_item_note(request: AuthedHttpRequest, collection_uuid, ite item = get_object_or_404(Item, uid=get_uuid_or_404(item_uuid)) if not collection.is_editable_by(request.user): raise PermissionDenied(_("Insufficient permission")) - if request.method == "POST": - collection.update_item_metadata( - item, {"note": request.POST.get("note", default="")} + member = collection.get_member_for_item(item) + note = request.POST.get("note", default="") + cancel = request.GET.get("cancel") + if request.method == "POST" and member: + member.note = note # type: ignore + member.save() + return render( + request, + "collection_update_item_note_ok.html", + {"collection": collection, "item": item, "collection_member": member}, + ) + elif cancel: + return render( + request, + "collection_update_item_note_ok.html", + {"collection": collection, "item": item, "collection_member": member}, ) - return collection_retrieve_items(request, collection_uuid, True) else: - member = collection.get_member_for_item(item) return render( request, "collection_update_item_note.html", - { - "collection": collection, - "item": item, - "note": member.note if member else None, # type:ignore - }, + {"collection": collection, "item": item, "collection_member": member}, )