Merge pull request #2586 from asmaloney/fix-duplicate-suggestions

{list} Fix duplicate suggestions in Add Books section
This commit is contained in:
Mouse Reeve 2023-01-11 08:40:32 -08:00 committed by GitHub
commit 5c8145b79d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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() query = query.strip()
results = None results = None
# first, try searching unqiue identifiers # first, try searching unique identifiers
# unique identifiers never have spaces, title/author usually do # unique identifiers never have spaces, title/author usually do
if not " " in query: if not " " in query:
results = search_identifiers(query, *filters, return_first=return_first) results = search_identifiers(query, *filters, return_first=return_first)

View file

@ -94,7 +94,7 @@ class List(View):
return redirect(book_list.local_path) 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""" """What books might a user want to add to a list"""
if query: if query:
# search for books # 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())], filters=[~Q(parent_work__editions__in=book_list.books.all())],
) )
# just suggest whatever books are nearby # just suggest whatever books are nearby
suggestions = user.shelfbook_set.filter(~Q(book__in=book_list.books.all())) suggestions = user.shelfbook_set.filter(
suggestions = [s.book for s in suggestions[:5]] ~Q(book__in=book_list.books.all())
if len(suggestions) < 5: ).distinct()[:num_suggestions]
suggestions += [ suggestions = [s.book for s in suggestions[:num_suggestions]]
if len(suggestions) < num_suggestions:
others = [
s.default_edition s.default_edition
for s in models.Work.objects.filter( for s in models.Work.objects.filter(
~Q(editions__in=book_list.books.all()), ~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 return suggestions