From e9f202b9e05ca6bfe8649f1f88cb64904fe5ce6b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 29 Mar 2021 11:48:19 -0700 Subject: [PATCH] Paginate shelf page Also removes some fields from the table --- bookwyrm/templates/snippets/shelf.html | 73 -------------------------- bookwyrm/templates/user/shelf.html | 72 ++++++++++++++++++++++++- bookwyrm/views/shelf.py | 14 +++-- 3 files changed, 82 insertions(+), 77 deletions(-) delete mode 100644 bookwyrm/templates/snippets/shelf.html diff --git a/bookwyrm/templates/snippets/shelf.html b/bookwyrm/templates/snippets/shelf.html deleted file mode 100644 index 8cae29e6..00000000 --- a/bookwyrm/templates/snippets/shelf.html +++ /dev/null @@ -1,73 +0,0 @@ -{% load humanize %} -{% load i18n %} -{% load bookwyrm_tags %} -{% if books|length > 0 %} -
- - - - - - - - - - - {% if ratings %} - {% endif %} - -{% for book in books %} - - - - - - - {% latest_read_through book user as read_through %} - - - - {% if ratings %} - - {% endif %} - {% if shelf.user == request.user %} - - {% endif %} - -{% endfor %} -
{% trans "Cover" %}{% trans "Title" %}{% trans "Author" %}{% trans "Published" %}{% trans "Shelved" %}{% trans "Started" %}{% trans "Finished" %}{% trans "External links" %}{% trans "Rating" %}
- {% include 'snippets/book_cover.html' with book=book size="small" %} - - {{ book.title }} - - {{ book.authors.first.name }} - - {% if book.first_published_date %}{{ book.first_published_date }}{% endif %} - - {{ book.created_date | naturalday }} - - {{ read_through.start_date | naturalday |default_if_none:""}} - - {{ read_through.finish_date | naturalday |default_if_none:""}} - - {% trans "OpenLibrary" %} - - {% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %} - - {% include 'snippets/shelf_selector.html' with current=shelf %} -
-
-{% else %} -

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

-{% if shelf.editable %} -
- {% csrf_token %} - - -
-{% endif %} - -{% endif %} - diff --git a/bookwyrm/templates/user/shelf.html b/bookwyrm/templates/user/shelf.html index 189d2856..4c60258c 100644 --- a/bookwyrm/templates/user/shelf.html +++ b/bookwyrm/templates/user/shelf.html @@ -1,5 +1,6 @@ {% extends 'user/user_layout.html' %} {% load bookwyrm_tags %} +{% load humanize %} {% load i18n %} {% block header %} @@ -63,7 +64,76 @@
- {% include 'snippets/shelf.html' with shelf=shelf books=books ratings=ratings %} + {% if books|length > 0 %} +
+ + + + + + + + + + {% if ratings %}{% endif %} + {% if shelf.user == request.user %} + + {% endif %} + + {% for book in books %} + {% with book=book.book %} + + + + + + {% latest_read_through book user as read_through %} + + + {% if ratings %} + + {% endif %} + {% if shelf.user == request.user %} + + {% endif %} + + {% endwith %} + {% endfor %} +
{% trans "Cover" %}{% trans "Title" %}{% trans "Author" %}{% trans "Shelved" %}{% trans "Started" %}{% trans "Finished" %}{% trans "Rating" %}
+ {% include 'snippets/book_cover.html' with book=book size="small" %} + + {{ book.title }} + + {% include 'snippets/authors.html' %} + + {{ book.created_date | naturalday }} + + {{ read_through.start_date | naturalday |default_if_none:""}} + + {{ read_through.finish_date | naturalday |default_if_none:""}} + + {% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %} + + {% include 'snippets/shelf_selector.html' with current=shelf %} +
+
+ {% else %} +

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

+ {% if shelf.editable %} +
+ {% csrf_token %} + + +
+ {% endif %} + + {% endif %} +
+
+ {% include 'snippets/pagination.html' with page=books path=request.path %} +
{% endblock %} diff --git a/bookwyrm/views/shelf.py b/bookwyrm/views/shelf.py index 6256eac7..87c40ce6 100644 --- a/bookwyrm/views/shelf.py +++ b/bookwyrm/views/shelf.py @@ -1,5 +1,6 @@ """ shelf views""" from django.contrib.auth.decorators import login_required +from django.core.paginator import Paginator from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse @@ -9,6 +10,7 @@ from django.views.decorators.http import require_POST from bookwyrm import forms, models from bookwyrm.activitypub import ActivitypubResponse +from bookwyrm.settings import PAGE_LENGTH from .helpers import is_api_request, get_edition, get_user_from_username from .helpers import handle_reading_status @@ -24,6 +26,11 @@ class Shelf(View): except models.User.DoesNotExist: return HttpResponseNotFound() + try: + page = int(request.GET.get("page", 1)) + except ValueError: + page = 1 + if shelf_identifier: shelf = user.shelf_set.get(identifier=shelf_identifier) else: @@ -49,10 +56,11 @@ class Shelf(View): if is_api_request(request): return ActivitypubResponse(shelf.to_activity(**request.GET)) - books = ( + paginated = Paginator( models.ShelfBook.objects.filter(user=user, shelf=shelf) .order_by("-updated_date") - .all() + .all(), + PAGE_LENGTH, ) data = { @@ -60,7 +68,7 @@ class Shelf(View): "is_self": is_self, "shelves": shelves.all(), "shelf": shelf, - "books": [b.book for b in books], + "books": paginated.page(page), } return TemplateResponse(request, "user/shelf.html", data)