Repository: bookwyrm-social/bookwyrm

Issue:
Your books: All books: shelved date is incorrect
**Describe the bug**

I just started using Bookwyrm, and added 4 books as "To Read". On my "All Books" page, the "Shelved" dates for 3 of those books are incorrect. https://bookwyrm.social/user/chorist/books

If I click over to my "To Read" page however, the Shelved dates are all correct (all showing "today").

**Screenshots**

<img width="1181" alt="Screen Shot 2022-01-18 at 4 52 23 PM" src="https://user-images.githubusercontent.com/557851/150031715-652dc082-a45a-4e71-af7f-efc34dfb0de9.png">

**Instance**
bookwyrm.social

**Desktop (please complete the following information):**
    - OS: MacOS
    - Browser: Safari
    - Version 15.2




Discussion:
Oh thank you! I think this is an unintended side effect of #1740 -- this view actually isn't getting the shelve date so it's displaying some nonsense value there, when it should just not try to show that columns.
Ah shoot, I remembering thinking about this when looking at that PR but forgot to mention it. I wonder if we could show an Added Date for this view instead - which would I guess be the earliest date the book was shelved in any shelf? I'd have to dig in to see if that's feasible with how we're doing the query building and things.

Resolving patch:
diff --git a/bookwyrm/views/shelf/shelf.py b/bookwyrm/views/shelf/shelf.py
--- a/bookwyrm/views/shelf/shelf.py
+++ b/bookwyrm/views/shelf/shelf.py
@@ -1,7 +1,7 @@
 """ shelf views """
 from collections import namedtuple
 
-from django.db.models import OuterRef, Subquery, F
+from django.db.models import OuterRef, Subquery, F, Max
 from django.contrib.auth.decorators import login_required
 from django.core.paginator import Paginator
 from django.http import HttpResponseBadRequest
@@ -72,11 +72,7 @@ def get(self, request, username, shelf_identifier=None):
             "start_date"
         )
 
-        if shelf_identifier:
-            books = books.annotate(shelved_date=F("shelfbook__shelved_date"))
-        else:
-            # sorting by shelved date will cause duplicates in the "all books" view
-            books = books.annotate(shelved_date=F("updated_date"))
+        books = books.annotate(shelved_date=Max("shelfbook__shelved_date"))
         books = books.annotate(
             rating=Subquery(reviews.values("rating")[:1]),
             start_date=Subquery(reading.values("start_date")[:1]),


Test patch:
diff --git a/bookwyrm/tests/views/shelf/test_shelf.py b/bookwyrm/tests/views/shelf/test_shelf.py
--- a/bookwyrm/tests/views/shelf/test_shelf.py
+++ b/bookwyrm/tests/views/shelf/test_shelf.py
@@ -51,12 +51,52 @@ def setUp(self):
 
     def test_shelf_page_all_books(self, *_):
         """there are so many views, this just makes sure it LOADS"""
+        models.ShelfBook.objects.create(
+            book=self.book,
+            shelf=self.shelf,
+            user=self.local_user,
+        )
+        view = views.Shelf.as_view()
+        request = self.factory.get("")
+        request.user = self.local_user
+        with patch("bookwyrm.views.shelf.shelf.is_api_request") as is_api:
+            is_api.return_value = False
+            result = view(request, self.local_user.username)
+        self.assertIsInstance(result, TemplateResponse)
+        validate_html(result.render())
+        self.assertEqual(result.status_code, 200)
+
+    def test_shelf_page_all_books_empty(self, *_):
+        """No books shelved"""
+        view = views.Shelf.as_view()
+        request = self.factory.get("")
+        request.user = self.local_user
+        with patch("bookwyrm.views.shelf.shelf.is_api_request") as is_api:
+            is_api.return_value = False
+            result = view(request, self.local_user.username)
+        self.assertIsInstance(result, TemplateResponse)
+        validate_html(result.render())
+        self.assertEqual(result.status_code, 200)
+
+    def test_shelf_page_all_books_avoid_duplicates(self, *_):
+        """Make sure books aren't showing up twice on the all shelves view"""
+        models.ShelfBook.objects.create(
+            book=self.book,
+            shelf=self.shelf,
+            user=self.local_user,
+        )
+        models.ShelfBook.objects.create(
+            book=self.book,
+            shelf=self.local_user.shelf_set.first(),
+            user=self.local_user,
+        )
         view = views.Shelf.as_view()
         request = self.factory.get("")
         request.user = self.local_user
         with patch("bookwyrm.views.shelf.shelf.is_api_request") as is_api:
             is_api.return_value = False
             result = view(request, self.local_user.username)
+        self.assertEqual(result.context_data["books"].object_list.count(), 1)
         self.assertIsInstance(result, TemplateResponse)
         validate_html(result.render())
         self.assertEqual(result.status_code, 200)