Merge pull request #819 from mouse-reeve/shelf-pagination

Paginate shelf page
This commit is contained in:
Mouse Reeve 2021-03-29 12:00:35 -07:00 committed by GitHub
commit 91b81de8a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 77 deletions

View file

@ -1,73 +0,0 @@
{% load humanize %}
{% load i18n %}
{% load bookwyrm_tags %}
{% if books|length > 0 %}
<div class="scroll-x">
<table class="table is-striped is-fullwidth">
<tr class="book-preview">
<th>{% trans "Cover" %}</th>
<th>{% trans "Title" %}</th>
<th>{% trans "Author" %}</th>
<th>{% trans "Published" %}</th>
<th>{% trans "Shelved" %}</th>
<th>{% trans "Started" %}</th>
<th>{% trans "Finished" %}</th>
<th>{% trans "External links" %}</th>{% if ratings %}
<th>{% trans "Rating" %}</th>{% endif %}
</tr>
{% for book in books %}
<tr class="book-preview">
<td>
<a href="{{ book.local_path }}">{% include 'snippets/book_cover.html' with book=book size="small" %}</a>
</td>
<td>
<a href="{{ book.local_path }}">{{ book.title }}</a>
</td>
<td>
{{ book.authors.first.name }}
</td>
<td>
{% if book.first_published_date %}{{ book.first_published_date }}{% endif %}
</td>
<td>
{{ book.created_date | naturalday }}
</td>
{% latest_read_through book user as read_through %}
<td>
{{ read_through.start_date | naturalday |default_if_none:""}}
</td>
<td>
{{ read_through.finish_date | naturalday |default_if_none:""}}
</td>
<td>
<a href="https://openlibrary.org/book/{{ book.openlibrary_key }}" target="_blank">{% trans "OpenLibrary" %}</a>
</td>
{% if ratings %}
<td>
{% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %}
</td>
{% endif %}
{% if shelf.user == request.user %}
<td>
{% include 'snippets/shelf_selector.html' with current=shelf %}
</td>
{% endif %}
</tr>
{% endfor %}
</table>
</div>
{% else %}
<p>{% trans "This shelf is empty." %}</p>
{% if shelf.editable %}
<form name="delete-shelf" action="/delete-shelf/{{ shelf.id }}" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ request.user.id }}">
<button class="button is-danger is-light" type="submit">
{% trans "Delete shelf" %}
</button>
</form>
{% endif %}
{% endif %}

View file

@ -1,5 +1,6 @@
{% extends 'user/user_layout.html' %} {% extends 'user/user_layout.html' %}
{% load bookwyrm_tags %} {% load bookwyrm_tags %}
{% load humanize %}
{% load i18n %} {% load i18n %}
{% block header %} {% block header %}
@ -63,7 +64,76 @@
<div class="block"> <div class="block">
<div> <div>
{% include 'snippets/shelf.html' with shelf=shelf books=books ratings=ratings %} {% if books|length > 0 %}
<div class="scroll-x">
<table class="table is-striped is-fullwidth">
<tr class="book-preview">
<th>{% trans "Cover" %}</th>
<th>{% trans "Title" %}</th>
<th>{% trans "Author" %}</th>
<th>{% trans "Shelved" %}</th>
<th>{% trans "Started" %}</th>
<th>{% trans "Finished" %}</th>
{% if ratings %}<th>{% trans "Rating" %}</th>{% endif %}
{% if shelf.user == request.user %}
<th aria-hidden="true"></th>
{% endif %}
</tr>
{% for book in books %}
{% with book=book.book %}
<tr class="book-preview">
<td>
<a href="{{ book.local_path }}">{% include 'snippets/book_cover.html' with book=book size="small" %}</a>
</td>
<td>
<a href="{{ book.local_path }}">{{ book.title }}</a>
</td>
<td>
{% include 'snippets/authors.html' %}
</td>
<td>
{{ book.created_date | naturalday }}
</td>
{% latest_read_through book user as read_through %}
<td>
{{ read_through.start_date | naturalday |default_if_none:""}}
</td>
<td>
{{ read_through.finish_date | naturalday |default_if_none:""}}
</td>
{% if ratings %}
<td>
{% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %}
</td>
{% endif %}
{% if shelf.user == request.user %}
<td>
{% include 'snippets/shelf_selector.html' with current=shelf %}
</td>
{% endif %}
</tr>
{% endwith %}
{% endfor %}
</table>
</div>
{% else %}
<p>{% trans "This shelf is empty." %}</p>
{% if shelf.editable %}
<form name="delete-shelf" action="/delete-shelf/{{ shelf.id }}" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ request.user.id }}">
<button class="button is-danger is-light" type="submit">
{% trans "Delete shelf" %}
</button>
</form>
{% endif %}
{% endif %}
</div> </div>
</div> </div>
<div>
{% include 'snippets/pagination.html' with page=books path=request.path %}
</div>
{% endblock %} {% endblock %}

View file

@ -1,5 +1,6 @@
""" shelf views""" """ shelf views"""
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.http import HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse 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 import forms, models
from bookwyrm.activitypub import ActivitypubResponse 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 is_api_request, get_edition, get_user_from_username
from .helpers import handle_reading_status from .helpers import handle_reading_status
@ -24,6 +26,11 @@ class Shelf(View):
except models.User.DoesNotExist: except models.User.DoesNotExist:
return HttpResponseNotFound() return HttpResponseNotFound()
try:
page = int(request.GET.get("page", 1))
except ValueError:
page = 1
if shelf_identifier: if shelf_identifier:
shelf = user.shelf_set.get(identifier=shelf_identifier) shelf = user.shelf_set.get(identifier=shelf_identifier)
else: else:
@ -49,10 +56,11 @@ class Shelf(View):
if is_api_request(request): if is_api_request(request):
return ActivitypubResponse(shelf.to_activity(**request.GET)) return ActivitypubResponse(shelf.to_activity(**request.GET))
books = ( paginated = Paginator(
models.ShelfBook.objects.filter(user=user, shelf=shelf) models.ShelfBook.objects.filter(user=user, shelf=shelf)
.order_by("-updated_date") .order_by("-updated_date")
.all() .all(),
PAGE_LENGTH,
) )
data = { data = {
@ -60,7 +68,7 @@ class Shelf(View):
"is_self": is_self, "is_self": is_self,
"shelves": shelves.all(), "shelves": shelves.all(),
"shelf": shelf, "shelf": shelf,
"books": [b.book for b in books], "books": paginated.page(page),
} }
return TemplateResponse(request, "user/shelf.html", data) return TemplateResponse(request, "user/shelf.html", data)