{list} Fix duplicate suggestions in Add Books section

Fixes #2584

(Also fix a spelling mistake in a comment in book_search.py)
This commit is contained in:
Andy Maloney 2023-01-09 21:40:40 -05:00
parent 917569ef0e
commit dd23bc4d0f
2 changed files with 13 additions and 7 deletions

View file

@ -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)

View file

@ -94,7 +94,7 @@ class List(View):
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