mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 19:41:11 +00:00
{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:
parent
917569ef0e
commit
dd23bc4d0f
2 changed files with 13 additions and 7 deletions
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue