forked from mirrors/bookwyrm
Add sorting to shelf. Use table-sort-header
existing template. Signed-off-by: Faiazov Dmitrii <jjsolutions0110@gmail.com>
This commit is contained in:
parent
d7f8a7b99e
commit
ce16f36fe8
4 changed files with 49 additions and 15 deletions
|
@ -7,6 +7,7 @@ from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
env = Env()
|
env = Env()
|
||||||
|
env.read_env()
|
||||||
DOMAIN = env("DOMAIN")
|
DOMAIN = env("DOMAIN")
|
||||||
VERSION = "0.0.1"
|
VERSION = "0.0.1"
|
||||||
|
|
||||||
|
|
|
@ -124,14 +124,14 @@
|
||||||
<table class="table is-striped is-fullwidth is-mobile">
|
<table class="table is-striped is-fullwidth is-mobile">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>{% trans "Cover" %}</th>
|
<th>{% trans "Cover"%}</th>
|
||||||
<th>{% trans "Title" %}</th>
|
<th>{% trans "Title" as text %}{% include 'snippets/table-sort-header.html' with field="title" sort=sort text=text %}</th>
|
||||||
<th>{% trans "Author" %}</th>
|
<th>{% trans "Author" as text %}{% include 'snippets/table-sort-header.html' with field="author" sort=sort text=text %}</th>
|
||||||
<th>{% trans "Shelved" %}</th>
|
<th>{% trans "Shelved" as text %}{% include 'snippets/table-sort-header.html' with field="shelved_date" sort=sort text=text %}</th>
|
||||||
<th>{% trans "Started" %}</th>
|
<th>{% trans "Started" as text %}{% include 'snippets/table-sort-header.html' with field="start_date" sort=sort text=text %}</th>
|
||||||
<th>{% trans "Finished" %}</th>
|
<th>{% trans "Finished" as text %}{% include 'snippets/table-sort-header.html' with field="finish_date" sort=sort text=text %}</th>
|
||||||
{% if request.user.is_authenticated %}
|
{% if request.user.is_authenticated %}
|
||||||
<th>{% trans "Rating" %}</th>
|
<th>{% trans "Rating" as text %}{% include 'snippets/table-sort-header.html' with field="rating" sort=sort text=text %}</th>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if shelf.user == request.user %}
|
{% if shelf.user == request.user %}
|
||||||
<th aria-hidden="true"></th>
|
<th aria-hidden="true"></th>
|
||||||
|
@ -154,12 +154,11 @@
|
||||||
<td data-title="{% trans "Shelved" %}">
|
<td data-title="{% trans "Shelved" %}">
|
||||||
{{ book.shelved_date|naturalday }}
|
{{ book.shelved_date|naturalday }}
|
||||||
</td>
|
</td>
|
||||||
{% latest_read_through book user as read_through %}
|
|
||||||
<td data-title="{% trans "Started" %}">
|
<td data-title="{% trans "Started" %}">
|
||||||
{{ read_through.start_date|naturalday|default_if_none:""}}
|
{{ book.start_date|naturalday|default_if_none:""}}
|
||||||
</td>
|
</td>
|
||||||
<td data-title="{% trans "Finished" %}">
|
<td data-title="{% trans "Finished" %}">
|
||||||
{{ read_through.finish_date|naturalday|default_if_none:""}}
|
{{ book.finish_date|naturalday|default_if_none:""}}
|
||||||
</td>
|
</td>
|
||||||
{% if request.user.is_authenticated %}
|
{% if request.user.is_authenticated %}
|
||||||
<td data-title="{% trans "Rating" %}">
|
<td data-title="{% trans "Rating" %}">
|
||||||
|
|
|
@ -68,16 +68,28 @@ class Shelf(View):
|
||||||
deleted=False,
|
deleted=False,
|
||||||
).order_by("-published_date")
|
).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(
|
books = books.annotate(
|
||||||
rating=Subquery(reviews.values("rating")[:1]),
|
rating=Subquery(reviews.values("rating")[:1]),
|
||||||
shelved_date=F("shelfbook__shelved_date"),
|
shelved_date=F("shelfbook__shelved_date"),
|
||||||
).prefetch_related("authors")
|
start_date=Subquery(reading.values("start_date")),
|
||||||
|
finish_date=Subquery(reading.values("finish_date")),
|
||||||
paginated = Paginator(
|
author=F("authors__name"),
|
||||||
books.order_by("-shelfbook__updated_date"),
|
|
||||||
PAGE_LENGTH,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
books = sort_books(books, request.GET.get("sort"))
|
||||||
|
|
||||||
|
paginated = Paginator(
|
||||||
|
books,
|
||||||
|
PAGE_LENGTH,
|
||||||
|
)
|
||||||
page = paginated.get_page(request.GET.get("page"))
|
page = paginated.get_page(request.GET.get("page"))
|
||||||
data = {
|
data = {
|
||||||
"user": user,
|
"user": user,
|
||||||
|
@ -87,6 +99,7 @@ class Shelf(View):
|
||||||
"books": page,
|
"books": page,
|
||||||
"edit_form": forms.ShelfForm(instance=shelf if shelf_identifier else None),
|
"edit_form": forms.ShelfForm(instance=shelf if shelf_identifier else None),
|
||||||
"create_form": forms.ShelfForm(),
|
"create_form": forms.ShelfForm(),
|
||||||
|
"sort": request.GET.get("sort"),
|
||||||
"page_range": paginated.get_elided_page_range(
|
"page_range": paginated.get_elided_page_range(
|
||||||
page.number, on_each_side=2, on_ends=1
|
page.number, on_each_side=2, on_ends=1
|
||||||
),
|
),
|
||||||
|
@ -207,3 +220,23 @@ def unshelve(request):
|
||||||
|
|
||||||
shelf_book.delete()
|
shelf_book.delete()
|
||||||
return redirect(request.headers.get("Referer", "/"))
|
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
|
||||||
|
|
|
@ -7,6 +7,7 @@ markers =
|
||||||
|
|
||||||
env =
|
env =
|
||||||
DEBUG = false
|
DEBUG = false
|
||||||
|
USE_HTTPS=true
|
||||||
DOMAIN = your.domain.here
|
DOMAIN = your.domain.here
|
||||||
BOOKWYRM_DATABASE_BACKEND = postgres
|
BOOKWYRM_DATABASE_BACKEND = postgres
|
||||||
MEDIA_ROOT = images/
|
MEDIA_ROOT = images/
|
||||||
|
|
Loading…
Reference in a new issue