From d2b2065db8856c7e6b2b4381bfb358b6c1a41343 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 23 Apr 2021 13:32:58 -0700 Subject: [PATCH 1/3] Show user's book content --- bookwyrm/templates/book/book.html | 33 ++++++++++++++- bookwyrm/urls.py | 3 +- bookwyrm/views/books.py | 67 +++++++++++++++++++------------ 3 files changed, 74 insertions(+), 29 deletions(-) diff --git a/bookwyrm/templates/book/book.html b/bookwyrm/templates/book/book.html index f1aaaf51..31c24532 100644 --- a/bookwyrm/templates/book/book.html +++ b/bookwyrm/templates/book/book.html @@ -246,7 +246,36 @@
- {% for review in reviews %} + {% if request.user.is_authenticated %} + + {% endif %} + + {% for review in statuses %}
- {% include 'snippets/pagination.html' with page=reviews path=book.local_path anchor="#reviews" %} + {% include 'snippets/pagination.html' with page=statuses path=book.local_path anchor="#reviews" %}
diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 3ad064b2..b85a81e0 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -260,7 +260,8 @@ urlpatterns = [ re_path(r"^boost/(?P\d+)/?$", views.Boost.as_view()), re_path(r"^unboost/(?P\d+)/?$", views.Unboost.as_view()), # 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/(?Preview|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/confirm/?$" % book_path, views.ConfirmEditBook.as_view()), re_path(r"^create-book/?$", views.EditBook.as_view()), diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index 34ebe0b6..67b3792e 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -28,7 +28,7 @@ from .helpers import is_api_request, get_edition, privacy_filter class Book(View): """ 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 """ try: book = models.Book.objects.select_subclasses().get(id=book_id) @@ -40,22 +40,42 @@ class Book(View): if isinstance(book, models.Work): book = book.get_default_edition() - if not book: + if not book or not book.parent_work: return HttpResponseNotFound() work = book.parent_work - if not work: - return HttpResponseNotFound() # all reviews for the book - reviews = models.Review.objects.filter(book__in=work.editions.all()) - reviews = privacy_filter(request.user, reviews) + reviews = privacy_filter( + request.user, + models.Review.objects.filter(book__in=work.editions.all()) + ) # the reviews to show - paginated = Paginator( - reviews.exclude(Q(content__isnull=True) | Q(content="")), PAGE_LENGTH - ) - reviews_page = paginated.get_page(request.GET.get("page")) + if user_statuses and request.user.is_authenticated: + if user_statuses == 'review': + queryset = book.review_set + elif user_statuses == 'comment': + queryset = book.comment_set + else: + queryset = book.quotation_set + paginated = Paginator( + queryset.filter(user=request.user), PAGE_LENGTH + ) + else: + paginated = Paginator( + reviews.exclude(Q(content__isnull=True) | Q(content="")), 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: readthroughs = models.ReadThrough.objects.filter( @@ -67,29 +87,24 @@ class Book(View): readthrough.progress_updates = ( 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), user=request.user, book__parent_work=book.parent_work, ) - data = { - "book": book, - "reviews": reviews_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) - ), - "user_shelves": user_shelves, - "other_edition_shelves": other_edition_shelves, - "readthroughs": readthroughs, - "path": "/book/%s" % book_id, - } + data["user_statuses"] = { + "review_count": book.review_set.filter(user=request.user).count(), + "comment_count": book.comment_set.filter(user=request.user).count(), + "quotation_count": book.quotation_set.filter(user=request.user).count(), + } + return TemplateResponse(request, "book/book.html", data) From 888930f891c21b7ca6b11080e89dc9552ec8e9e1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 23 Apr 2021 13:35:12 -0700 Subject: [PATCH 2/3] User request path for pagination --- bookwyrm/templates/book/book.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/templates/book/book.html b/bookwyrm/templates/book/book.html index 31c24532..cfa400d6 100644 --- a/bookwyrm/templates/book/book.html +++ b/bookwyrm/templates/book/book.html @@ -316,7 +316,7 @@ {% endfor %}
- {% include 'snippets/pagination.html' with page=statuses path=book.local_path anchor="#reviews" %} + {% include 'snippets/pagination.html' with page=statuses path=request.path anchor="#reviews" %}
From 126594ec4935f1f6a863560f200371ae04c33d8e Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 23 Apr 2021 13:46:22 -0700 Subject: [PATCH 3/3] Cleans up queryset declarations --- bookwyrm/templates/book/book.html | 2 +- bookwyrm/urls.py | 6 +++++- bookwyrm/views/books.py | 17 +++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/bookwyrm/templates/book/book.html b/bookwyrm/templates/book/book.html index cfa400d6..97f105bf 100644 --- a/bookwyrm/templates/book/book.html +++ b/bookwyrm/templates/book/book.html @@ -251,7 +251,7 @@
    {% url 'book' book.id as tab_url %}
  • - {% trans "Reviews" %} + {% trans "Reviews" %} ({{ review_count }})
  • {% if user_statuses.review_count %} {% url 'book-user-statuses' book.id 'review' as tab_url %} diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index b85a81e0..53ceeaa8 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -261,7 +261,11 @@ urlpatterns = [ re_path(r"^unboost/(?P\d+)/?$", views.Unboost.as_view()), # books re_path(r"%s(.json)?/?$" % book_path, views.Book.as_view(), name="book"), - re_path(r"%s/(?Preview|comment|quote)/?$" % book_path, views.Book.as_view(), name="book-user-statuses"), + re_path( + r"%s/(?Preview|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/confirm/?$" % book_path, views.ConfirmEditBook.as_view()), re_path(r"^create-book/?$", views.EditBook.as_view()), diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index 67b3792e..829408a8 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -47,25 +47,22 @@ class Book(View): # all reviews for the book reviews = privacy_filter( - request.user, - models.Review.objects.filter(book__in=work.editions.all()) + request.user, models.Review.objects.filter(book__in=work.editions.all()) ) # the reviews to show if user_statuses and request.user.is_authenticated: - if user_statuses == 'review': + if user_statuses == "review": queryset = book.review_set - elif user_statuses == 'comment': + elif user_statuses == "comment": queryset = book.comment_set else: queryset = book.quotation_set - paginated = Paginator( - queryset.filter(user=request.user), PAGE_LENGTH - ) + queryset = queryset.filter(user=request.user) else: - paginated = Paginator( - reviews.exclude(Q(content__isnull=True) | Q(content="")), PAGE_LENGTH - ) + 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")),