Merge pull request #993 from bookwyrm-social/find-own-book-content

Find own book content
This commit is contained in:
Mouse Reeve 2021-04-25 11:37:00 -07:00 committed by GitHub
commit ef83eb33b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 29 deletions

View file

@ -246,7 +246,36 @@
</div> </div>
<div class="block" id="reviews"> <div class="block" id="reviews">
{% for review in reviews %} {% if request.user.is_authenticated %}
<nav class="tabs">
<ul>
{% url 'book' book.id as tab_url %}
<li {% if tab_url == request.path %}class="is-active"{% endif %}>
<a href="{{ tab_url }}">{% trans "Reviews" %} ({{ review_count }})</a>
</li>
{% if user_statuses.review_count %}
{% url 'book-user-statuses' book.id 'review' as tab_url %}
<li {% if tab_url == request.path %}class="is-active"{% endif %}>
<a href="{{ tab_url }}">{% trans "Your reviews" %} ({{ user_statuses.review_count }})</a>
</li>
{% endif %}
{% if user_statuses.comment_count %}
{% url 'book-user-statuses' book.id 'comment' as tab_url %}
<li {% if tab_url == request.path %}class="is-active"{% endif %}>
<a href="{{ tab_url }}">{% trans "Your comments" %} ({{ user_statuses.comment_count }})</a>
</li>
{% endif %}
{% if user_statuses.quotation_count %}
{% url 'book-user-statuses' book.id 'quote' as tab_url %}
<li {% if tab_url == request.path %}class="is-active"{% endif %}>
<a href="{{ tab_url }}">{% trans "Your quotes" %} ({{ user_statuses.quotation_count }})</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
{% for review in statuses %}
<div <div
class="block" class="block"
itemprop="review" itemprop="review"
@ -287,7 +316,7 @@
{% endfor %} {% endfor %}
</div> </div>
<div class="block"> <div class="block">
{% include 'snippets/pagination.html' with page=reviews path=book.local_path anchor="#reviews" %} {% include 'snippets/pagination.html' with page=statuses path=request.path anchor="#reviews" %}
</div> </div>
</div> </div>
</div> </div>

View file

@ -260,7 +260,12 @@ urlpatterns = [
re_path(r"^boost/(?P<status_id>\d+)/?$", views.Boost.as_view()), re_path(r"^boost/(?P<status_id>\d+)/?$", views.Boost.as_view()),
re_path(r"^unboost/(?P<status_id>\d+)/?$", views.Unboost.as_view()), re_path(r"^unboost/(?P<status_id>\d+)/?$", views.Unboost.as_view()),
# books # books
re_path(r"%s(.json)?/?$" % book_path, views.Book.as_view()), re_path(r"%s(.json)?/?$" % book_path, views.Book.as_view(), name="book"),
re_path(
r"%s/(?P<user_statuses>review|comment|quote)/?$" % book_path,
views.Book.as_view(),
name="book-user-statuses",
),
re_path(r"%s/edit/?$" % book_path, views.EditBook.as_view()), re_path(r"%s/edit/?$" % book_path, views.EditBook.as_view()),
re_path(r"%s/confirm/?$" % book_path, views.ConfirmEditBook.as_view()), re_path(r"%s/confirm/?$" % book_path, views.ConfirmEditBook.as_view()),
re_path(r"^create-book/?$", views.EditBook.as_view()), re_path(r"^create-book/?$", views.EditBook.as_view()),

View file

@ -28,7 +28,7 @@ from .helpers import is_api_request, get_edition, privacy_filter
class Book(View): class Book(View):
""" a book! this is the stuff """ """ a book! this is the stuff """
def get(self, request, book_id): def get(self, request, book_id, user_statuses=False):
""" info about a book """ """ info about a book """
try: try:
book = models.Book.objects.select_subclasses().get(id=book_id) book = models.Book.objects.select_subclasses().get(id=book_id)
@ -40,22 +40,39 @@ class Book(View):
if isinstance(book, models.Work): if isinstance(book, models.Work):
book = book.get_default_edition() book = book.get_default_edition()
if not book: if not book or not book.parent_work:
return HttpResponseNotFound() return HttpResponseNotFound()
work = book.parent_work work = book.parent_work
if not work:
return HttpResponseNotFound()
# all reviews for the book # all reviews for the book
reviews = models.Review.objects.filter(book__in=work.editions.all()) reviews = privacy_filter(
reviews = privacy_filter(request.user, reviews) request.user, models.Review.objects.filter(book__in=work.editions.all())
)
# the reviews to show # the reviews to show
paginated = Paginator( if user_statuses and request.user.is_authenticated:
reviews.exclude(Q(content__isnull=True) | Q(content="")), PAGE_LENGTH if user_statuses == "review":
) queryset = book.review_set
reviews_page = paginated.get_page(request.GET.get("page")) elif user_statuses == "comment":
queryset = book.comment_set
else:
queryset = book.quotation_set
queryset = queryset.filter(user=request.user)
else:
queryset = reviews.exclude(Q(content__isnull=True) | Q(content=""))
paginated = Paginator(queryset, PAGE_LENGTH)
data = {
"book": book,
"statuses": paginated.get_page(request.GET.get("page")),
"review_count": reviews.count(),
"ratings": reviews.filter(Q(content__isnull=True) | Q(content="")),
"rating": reviews.aggregate(Avg("rating"))["rating__avg"],
"lists": privacy_filter(
request.user, book.list_set.filter(listitem__approved=True)
),
}
if request.user.is_authenticated: if request.user.is_authenticated:
readthroughs = models.ReadThrough.objects.filter( readthroughs = models.ReadThrough.objects.filter(
@ -67,29 +84,24 @@ class Book(View):
readthrough.progress_updates = ( readthrough.progress_updates = (
readthrough.progressupdate_set.all().order_by("-updated_date") readthrough.progressupdate_set.all().order_by("-updated_date")
) )
data["readthroughs"] = readthroughs
user_shelves = models.ShelfBook.objects.filter(user=request.user, book=book) data["user_shelves"] = models.ShelfBook.objects.filter(
user=request.user, book=book
)
other_edition_shelves = models.ShelfBook.objects.filter( data["other_edition_shelves"] = models.ShelfBook.objects.filter(
~Q(book=book), ~Q(book=book),
user=request.user, user=request.user,
book__parent_work=book.parent_work, book__parent_work=book.parent_work,
) )
data = { data["user_statuses"] = {
"book": book, "review_count": book.review_set.filter(user=request.user).count(),
"reviews": reviews_page, "comment_count": book.comment_set.filter(user=request.user).count(),
"review_count": reviews.count(), "quotation_count": book.quotation_set.filter(user=request.user).count(),
"ratings": reviews.filter(Q(content__isnull=True) | Q(content="")), }
"rating": reviews.aggregate(Avg("rating"))["rating__avg"],
"lists": privacy_filter(
request.user, book.list_set.filter(listitem__approved=True)
),
"user_shelves": user_shelves,
"other_edition_shelves": other_edition_shelves,
"readthroughs": readthroughs,
"path": "/book/%s" % book_id,
}
return TemplateResponse(request, "book/book.html", data) return TemplateResponse(request, "book/book.html", data)