Repository: bookwyrm-social/bookwyrm

Issue:
(List page) Book showing up twice in Add Books suggestions
**Describe the bug**

When I set up a dev instance with one book, it showed up twice in the _Add Books_ suggestions on a list page:

**To Reproduce**
Steps to reproduce the behaviour:
1. Set up a fresh dev instance & create an admin user.
2. Add a book
3. Create a new list

The _Add Books_ suggestion list shows the same book twice. These are the exact same book (not multiple editions).

**Expected behaviour**

Should only show each book once.

**Screenshots**

<img width="326" alt="Screen Shot 2023-01-09 at 21 35 50 PM" src="https://user-images.githubusercontent.com/391371/211450358-2ced1ca6-aa00-48f6-98e2-74953ecda2f9.png">

**Instance**

Dev

**Additional context**

It's this code in **views/list/list.py** (_get_list_suggestions_):

```python
    # just suggest whatever books are nearby
    suggestions = user.shelfbook_set.filter(~Q(book__in=book_list.books.all()))
    suggestions = [s.book for s in suggestions[:5]]
    if len(suggestions) < 5:
        suggestions += [
            s.default_edition
            for s in models.Work.objects.filter(
                ~Q(editions__in=book_list.books.all()),
            ).order_by("-updated_date")[: 5 - len(suggestions)]
        ]
```

I tried replacing `suggestions` (`suggestions = [`) instead of appending to it (which works for this case), but I don't know if that's correct because I don't understand what this is supposed to be doing.

Is it trying to query other editions of the same book? (If so, why?)

I'm happy to submit a PR for a fix once it's clear.






Discussion:

Resolving patch:
diff --git a/bookwyrm/book_search.py b/bookwyrm/book_search.py
--- a/bookwyrm/book_search.py
+++ b/bookwyrm/book_search.py
@@ -20,7 +20,7 @@ def search(query, min_confidence=0, filters=None, return_first=False):
     query = query.strip()
 
     results = None
-    # first, try searching unqiue identifiers
+    # first, try searching unique identifiers
     # unique identifiers never have spaces, title/author usually do
     if not " " in query:
         results = search_identifiers(query, *filters, return_first=return_first)
diff --git a/bookwyrm/views/list/list.py b/bookwyrm/views/list/list.py
--- a/bookwyrm/views/list/list.py
+++ b/bookwyrm/views/list/list.py
@@ -94,7 +94,7 @@ def post(self, request, list_id):
         return redirect(book_list.local_path)
 
 
-def get_list_suggestions(book_list, user, query=None):
+def get_list_suggestions(book_list, user, query=None, num_suggestions=5):
     """What books might a user want to add to a list"""
     if query:
         # search for books
@@ -103,15 +103,21 @@ def get_list_suggestions(book_list, user, query=None):
             filters=[~Q(parent_work__editions__in=book_list.books.all())],
         )
     # just suggest whatever books are nearby
-    suggestions = user.shelfbook_set.filter(~Q(book__in=book_list.books.all()))
-    suggestions = [s.book for s in suggestions[:5]]
-    if len(suggestions) < 5:
-        suggestions += [
+    suggestions = user.shelfbook_set.filter(
+        ~Q(book__in=book_list.books.all())
+    ).distinct()[:num_suggestions]
+    suggestions = [s.book for s in suggestions[:num_suggestions]]
+    if len(suggestions) < num_suggestions:
+        others = [
             s.default_edition
             for s in models.Work.objects.filter(
                 ~Q(editions__in=book_list.books.all()),
-            ).order_by("-updated_date")[: 5 - len(suggestions)]
+            )
+            .distinct()
+            .order_by("-updated_date")[:num_suggestions]
         ]
+        # get 'num_suggestions' unique items
+        suggestions = list(set(suggestions + others))[:num_suggestions]
     return suggestions
 
 


Test patch: