From ce16f36fe855872e0c8df15ba24f4a7431cd3a3e Mon Sep 17 00:00:00 2001 From: Faiazov Dmitrii Date: Mon, 11 Oct 2021 23:22:12 +0300 Subject: [PATCH 1/3] Add sorting to shelf. Use table-sort-header existing template. Signed-off-by: Faiazov Dmitrii --- bookwyrm/settings.py | 1 + bookwyrm/templates/shelf/shelf.html | 19 ++++++------- bookwyrm/views/shelf.py | 43 +++++++++++++++++++++++++---- pytest.ini | 1 + 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index 55b4c4454..5ae85e6b1 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -7,6 +7,7 @@ from django.utils.translation import gettext_lazy as _ env = Env() +env.read_env() DOMAIN = env("DOMAIN") VERSION = "0.0.1" diff --git a/bookwyrm/templates/shelf/shelf.html b/bookwyrm/templates/shelf/shelf.html index 88f4b2bb6..eb3b472ab 100644 --- a/bookwyrm/templates/shelf/shelf.html +++ b/bookwyrm/templates/shelf/shelf.html @@ -124,14 +124,14 @@ - - - - - - + + + + + + {% if request.user.is_authenticated %} - + {% endif %} {% if shelf.user == request.user %} @@ -154,12 +154,11 @@ - {% latest_read_through book user as read_through %} {% if request.user.is_authenticated %} + {% if request.user.is_authenticated %} + {% if is_self %} - {% if request.user.is_authenticated %} + {% endif %} {% endif %} {% if shelf.user == request.user %} @@ -151,6 +153,8 @@ + {% if request.user.is_authenticated %} + {% if is_self %} @@ -160,7 +164,7 @@ - {% if request.user.is_authenticated %} + {% endif %} diff --git a/bookwyrm/views/shelf.py b/bookwyrm/views/shelf.py index c1d3512f3..0b830d906 100644 --- a/bookwyrm/views/shelf.py +++ b/bookwyrm/views/shelf.py @@ -69,20 +69,22 @@ class Shelf(View): ).order_by("-published_date") reading = models.ReadThrough.objects - if not is_self: - reading = models.ReadThrough.privacy_filter(request.user) reading = reading.filter(user=user, book__id=OuterRef("id")).order_by( - "-start_date" + "start_date" ) books = books.annotate( rating=Subquery(reviews.values("rating")[:1]), shelved_date=F("shelfbook__shelved_date"), - start_date=Subquery(reading.values("start_date")), - finish_date=Subquery(reading.values("finish_date")), - author=F("authors__name"), - ) + start_date=Subquery(reading.values("start_date")[:1]), + finish_date=Subquery(reading.values("finish_date")[:1]), + author=Subquery( + models.Book.objects.filter(id=OuterRef("id")).values("authors__name")[ + :1 + ] + ), + ).prefetch_related("authors") books = sort_books(books, request.GET.get("sort")) From b9251b4dc71d0cdd82d43cc1754f0cdcd3bf9dfa Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 14 Oct 2021 16:11:33 -0700 Subject: [PATCH 3/3] Fixes book identifiers not displaying --- bookwyrm/templates/book/book_identifiers.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/templates/book/book_identifiers.html b/bookwyrm/templates/book/book_identifiers.html index 8c8313f38..19ab619da 100644 --- a/bookwyrm/templates/book/book_identifiers.html +++ b/bookwyrm/templates/book/book_identifiers.html @@ -1,7 +1,7 @@ {% spaceless %} {% load i18n %} -{% if book.isbn13 or book.oclc_number or book.asin %} +{% if book.isbn_13 or book.oclc_number or book.asin %}
{% if book.isbn_13 %}
{% trans "Cover" %}{% trans "Title" %}{% trans "Author" %}{% trans "Shelved" %}{% trans "Started" %}{% trans "Finished" %}{% trans "Cover"%}{% trans "Title" as text %}{% include 'snippets/table-sort-header.html' with field="title" sort=sort text=text %}{% trans "Author" as text %}{% include 'snippets/table-sort-header.html' with field="author" sort=sort text=text %}{% trans "Shelved" as text %}{% include 'snippets/table-sort-header.html' with field="shelved_date" sort=sort text=text %}{% trans "Started" as text %}{% include 'snippets/table-sort-header.html' with field="start_date" sort=sort text=text %}{% trans "Finished" as text %}{% include 'snippets/table-sort-header.html' with field="finish_date" sort=sort text=text %}{% trans "Rating" %}{% trans "Rating" as text %}{% include 'snippets/table-sort-header.html' with field="rating" sort=sort text=text %} {{ book.shelved_date|naturalday }} - {{ read_through.start_date|naturalday|default_if_none:""}} + {{ book.start_date|naturalday|default_if_none:""}} - {{ read_through.finish_date|naturalday|default_if_none:""}} + {{ book.finish_date|naturalday|default_if_none:""}} diff --git a/bookwyrm/views/shelf.py b/bookwyrm/views/shelf.py index 37f320dcd..c1d3512f3 100644 --- a/bookwyrm/views/shelf.py +++ b/bookwyrm/views/shelf.py @@ -68,16 +68,28 @@ class Shelf(View): deleted=False, ).order_by("-published_date") + reading = models.ReadThrough.objects + if not is_self: + reading = models.ReadThrough.privacy_filter(request.user) + + reading = reading.filter(user=user, book__id=OuterRef("id")).order_by( + "-start_date" + ) + books = books.annotate( rating=Subquery(reviews.values("rating")[:1]), shelved_date=F("shelfbook__shelved_date"), - ).prefetch_related("authors") - - paginated = Paginator( - books.order_by("-shelfbook__updated_date"), - PAGE_LENGTH, + start_date=Subquery(reading.values("start_date")), + finish_date=Subquery(reading.values("finish_date")), + author=F("authors__name"), ) + books = sort_books(books, request.GET.get("sort")) + + paginated = Paginator( + books, + PAGE_LENGTH, + ) page = paginated.get_page(request.GET.get("page")) data = { "user": user, @@ -87,6 +99,7 @@ class Shelf(View): "books": page, "edit_form": forms.ShelfForm(instance=shelf if shelf_identifier else None), "create_form": forms.ShelfForm(), + "sort": request.GET.get("sort"), "page_range": paginated.get_elided_page_range( page.number, on_each_side=2, on_ends=1 ), @@ -207,3 +220,23 @@ def unshelve(request): shelf_book.delete() return redirect(request.headers.get("Referer", "/")) + + +def sort_books(books, sort): + """Books in shelf sorting""" + sort_fields = [ + "title", + "author", + "shelved_date", + "start_date", + "finish_date", + "rating", + ] + + if sort in sort_fields: + books = books.order_by(sort) + elif sort and sort[1:] in sort_fields: + books = books.order_by(F(sort[1:]).desc(nulls_last=True)) + else: + books = books.order_by("-shelved_date") + return books diff --git a/pytest.ini b/pytest.ini index 8539d1167..9ef72449c 100644 --- a/pytest.ini +++ b/pytest.ini @@ -7,6 +7,7 @@ markers = env = DEBUG = false + USE_HTTPS=true DOMAIN = your.domain.here BOOKWYRM_DATABASE_BACKEND = postgres MEDIA_ROOT = images/ From 1abbc674831fb7eb30112856377738c1975b820d Mon Sep 17 00:00:00 2001 From: Faiazov Dmitrii Date: Thu, 14 Oct 2021 13:39:15 +0300 Subject: [PATCH 2/3] Make requested changes. Signed-off-by: Faiazov Dmitrii --- bookwyrm/templates/shelf/shelf.html | 8 ++++++-- bookwyrm/views/shelf.py | 16 +++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/bookwyrm/templates/shelf/shelf.html b/bookwyrm/templates/shelf/shelf.html index eb3b472ab..fa1e91f98 100644 --- a/bookwyrm/templates/shelf/shelf.html +++ b/bookwyrm/templates/shelf/shelf.html @@ -127,10 +127,12 @@ {% trans "Cover"%} {% trans "Title" as text %}{% include 'snippets/table-sort-header.html' with field="title" sort=sort text=text %} {% trans "Author" as text %}{% include 'snippets/table-sort-header.html' with field="author" sort=sort text=text %}{% trans "Shelved" as text %}{% include 'snippets/table-sort-header.html' with field="shelved_date" sort=sort text=text %} {% trans "Started" as text %}{% include 'snippets/table-sort-header.html' with field="start_date" sort=sort text=text %} {% trans "Finished" as text %}{% include 'snippets/table-sort-header.html' with field="finish_date" sort=sort text=text %}{% trans "Rating" as text %}{% include 'snippets/table-sort-header.html' with field="rating" sort=sort text=text %} {% include 'snippets/authors.html' %} {{ book.shelved_date|naturalday }} {{ book.finish_date|naturalday|default_if_none:""}} {% include 'snippets/stars.html' with rating=book.rating %}