From 5ea40e23b74206af2849ca38e6db43210a05776d Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 28 Sep 2021 17:17:01 -0700 Subject: [PATCH] Moves shelf delete button --- bookwyrm/models/shelf.py | 8 +++++--- bookwyrm/templates/shelf/shelf.html | 26 ++++++++++++++------------ bookwyrm/tests/views/test_shelf.py | 3 ++- bookwyrm/views/shelf.py | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/bookwyrm/models/shelf.py b/bookwyrm/models/shelf.py index f12b9471..e35971e2 100644 --- a/bookwyrm/models/shelf.py +++ b/bookwyrm/models/shelf.py @@ -53,6 +53,10 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel): """list of books for this shelf, overrides OrderedCollectionMixin""" return self.books.order_by("shelfbook") + @property + def deletable(self): + return self.editable and not self.shelfbook_set.exists() + def get_remote_id(self): """shelf identifier instead of id""" base_path = self.user.remote_id @@ -62,9 +66,7 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel): def raise_not_deletable(self, viewer): """don't let anyone delete a default shelf""" super().raise_not_deletable(viewer) - if not self.editable: - raise PermissionDenied() - if self.shelfbook_set.exists(): + if not self.deletable: raise PermissionDenied() class Meta: diff --git a/bookwyrm/templates/shelf/shelf.html b/bookwyrm/templates/shelf/shelf.html index ed926826..c073d37e 100644 --- a/bookwyrm/templates/shelf/shelf.html +++ b/bookwyrm/templates/shelf/shelf.html @@ -91,8 +91,20 @@ {% if is_self and shelf.id %}
- {% trans "Edit shelf" as button_text %} - {% include 'snippets/toggle/open_button.html' with text=button_text icon_with_text="pencil" controls_text="edit_shelf_form" focus="edit_shelf_form_header" %} +
+ {% trans "Edit shelf" as button_text %} + {% include 'snippets/toggle/open_button.html' with text=button_text icon_with_text="pencil" controls_text="edit_shelf_form" focus="edit_shelf_form_header" %} + + {% if shelf.deletable %} +
+ {% csrf_token %} + + +
+ {% endif %} +
{% endif %} @@ -168,16 +180,6 @@ {% else %}

{% trans "This shelf is empty." %}

- {% if shelf.id and shelf.editable %} -
- {% csrf_token %} - - -
- {% endif %} - {% endif %} diff --git a/bookwyrm/tests/views/test_shelf.py b/bookwyrm/tests/views/test_shelf.py index 83c8d3ba..682e7ebe 100644 --- a/bookwyrm/tests/views/test_shelf.py +++ b/bookwyrm/tests/views/test_shelf.py @@ -270,7 +270,8 @@ class ShelfViews(TestCase): def test_delete_shelf_not_editable(self, *_): """delete a brand new custom shelf""" - shelf = self.local_user.shelfset.first() + shelf = self.local_user.shelf_set.first() + self.assertFalse(shelf.editable) request = self.factory.post("") request.user = self.local_user diff --git a/bookwyrm/views/shelf.py b/bookwyrm/views/shelf.py index 352c4b83..35f660b5 100644 --- a/bookwyrm/views/shelf.py +++ b/bookwyrm/views/shelf.py @@ -85,7 +85,7 @@ class Shelf(View): "shelves": shelves, "shelf": shelf, "books": page, - "edit_form": forms.ShelfForm(instance=shelf), + "edit_form": forms.ShelfForm(instance=shelf if shelf_identifier else None), "create_form": forms.ShelfForm(), "page_range": paginated.get_elided_page_range( page.number, on_each_side=2, on_ends=1