mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-15 12:46:38 +00:00
Fixes ratings on shelf pages
This commit is contained in:
parent
1fbd6537b7
commit
63172ecf00
2 changed files with 21 additions and 7 deletions
|
@ -79,7 +79,9 @@
|
||||||
<th>{% trans "Shelved" %}</th>
|
<th>{% trans "Shelved" %}</th>
|
||||||
<th>{% trans "Started" %}</th>
|
<th>{% trans "Started" %}</th>
|
||||||
<th>{% trans "Finished" %}</th>
|
<th>{% trans "Finished" %}</th>
|
||||||
{% if ratings %}<th>{% trans "Rating" %}</th>{% endif %}
|
{% if request.user.is_authenticated %}
|
||||||
|
<th>{% trans "Rating" %}</th>
|
||||||
|
{% endif %}
|
||||||
{% if shelf.user == request.user %}
|
{% if shelf.user == request.user %}
|
||||||
<th aria-hidden="true"></th>
|
<th aria-hidden="true"></th>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -108,9 +110,9 @@
|
||||||
<td data-title="{% trans "Finished" %}">
|
<td data-title="{% trans "Finished" %}">
|
||||||
{{ read_through.finish_date | naturalday |default_if_none:""}}
|
{{ read_through.finish_date | naturalday |default_if_none:""}}
|
||||||
</td>
|
</td>
|
||||||
{% if ratings %}
|
{% if request.user.is_authenticated %}
|
||||||
<td data-title="{% trans "Rating" %}">
|
<td data-title="{% trans "Rating" %}">
|
||||||
{% include 'snippets/stars.html' with rating=ratings|dict_key:book.id %}
|
{% include 'snippets/stars.html' with rating=book.rating %}
|
||||||
</td>
|
</td>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if shelf.user == request.user %}
|
{% if shelf.user == request.user %}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from django.db import IntegrityError
|
from django.db import IntegrityError
|
||||||
|
from django.db.models import Count, OuterRef, Subquery, F, Q
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.core.paginator import Paginator
|
from django.core.paginator import Paginator
|
||||||
from django.http import HttpResponseBadRequest, HttpResponseNotFound
|
from django.http import HttpResponseBadRequest, HttpResponseNotFound
|
||||||
|
@ -37,30 +38,41 @@ class Shelf(View):
|
||||||
return HttpResponseNotFound()
|
return HttpResponseNotFound()
|
||||||
if not shelf.visible_to_user(request.user):
|
if not shelf.visible_to_user(request.user):
|
||||||
return HttpResponseNotFound()
|
return HttpResponseNotFound()
|
||||||
|
books = shelf.books
|
||||||
# this is a constructed "all books" view, with a fake "shelf" obj
|
# this is a constructed "all books" view, with a fake "shelf" obj
|
||||||
else:
|
else:
|
||||||
FakeShelf = namedtuple(
|
FakeShelf = namedtuple(
|
||||||
"Shelf", ("identifier", "name", "user", "books", "privacy")
|
"Shelf", ("identifier", "name", "user", "books", "privacy")
|
||||||
)
|
)
|
||||||
books = models.Edition.objects.filter(
|
books = models.Edition.objects.filter(
|
||||||
|
# privacy is ensured because the shelves are already filtered above
|
||||||
shelfbook__shelf__in=shelves.all()
|
shelfbook__shelf__in=shelves.all()
|
||||||
).distinct()
|
).distinct()
|
||||||
shelf = FakeShelf("all", _("All books"), user, books, "public")
|
shelf = FakeShelf("all", _("All books"), user, books, "public")
|
||||||
|
|
||||||
is_self = request.user == user
|
|
||||||
|
|
||||||
if is_api_request(request):
|
if is_api_request(request):
|
||||||
return ActivitypubResponse(shelf.to_activity(**request.GET))
|
return ActivitypubResponse(shelf.to_activity(**request.GET))
|
||||||
|
|
||||||
|
reviews = privacy_filter(
|
||||||
|
request.user,
|
||||||
|
models.Review.objects.filter(
|
||||||
|
user=user,
|
||||||
|
rating__isnull=False,
|
||||||
|
book__id=OuterRef("id"),
|
||||||
|
),
|
||||||
|
).order_by("-published_date")
|
||||||
|
|
||||||
|
books = books.annotate(rating=Subquery(reviews.values("rating")[:1]))
|
||||||
|
|
||||||
paginated = Paginator(
|
paginated = Paginator(
|
||||||
shelf.books.order_by("-updated_date"),
|
books.order_by("-updated_date"),
|
||||||
PAGE_LENGTH,
|
PAGE_LENGTH,
|
||||||
)
|
)
|
||||||
|
|
||||||
page = paginated.get_page(request.GET.get("page"))
|
page = paginated.get_page(request.GET.get("page"))
|
||||||
data = {
|
data = {
|
||||||
"user": user,
|
"user": user,
|
||||||
"is_self": is_self,
|
"is_self": request.user == user,
|
||||||
"shelves": shelves.all(),
|
"shelves": shelves.all(),
|
||||||
"shelf": shelf,
|
"shelf": shelf,
|
||||||
"books": page,
|
"books": page,
|
||||||
|
|
Loading…
Reference in a new issue