Search for books to add to lists

This commit is contained in:
Mouse Reeve 2021-01-31 11:11:26 -08:00
parent 1e9189d43c
commit 6a68fe9475
5 changed files with 46 additions and 16 deletions

View file

@ -35,10 +35,10 @@ def search(query, min_confidence=0.1):
return results
def local_search(query, min_confidence=0.1):
def local_search(query, min_confidence=0.1, raw=False):
''' only look at local search results '''
connector = load_connector(models.Connector.objects.get(local=True))
return connector.search(query, min_confidence=min_confidence)
return connector.search(query, min_confidence=min_confidence, raw=raw)
def first_search_result(query, min_confidence=0.1):

View file

@ -11,7 +11,8 @@ from .abstract_connector import AbstractConnector, SearchResult
class Connector(AbstractConnector):
''' instantiate a connector '''
def search(self, query, min_confidence=0.1):
# pylint: disable=arguments-differ
def search(self, query, min_confidence=0.1, raw=False):
''' search your local database '''
if not query:
return []
@ -22,10 +23,14 @@ class Connector(AbstractConnector):
results = search_title_author(query, min_confidence)
search_results = []
for result in results:
search_results.append(self.format_search_result(result))
if raw:
search_results.append(result)
else:
search_results.append(self.format_search_result(result))
if len(search_results) >= 10:
break
search_results.sort(key=lambda r: r.confidence, reverse=True)
if not raw:
search_results.sort(key=lambda r: r.confidence, reverse=True)
return search_results

View file

@ -59,10 +59,10 @@
<a href="/#feed" class="navbar-item">
Feed
</a>
{% endif %}
<a href="{% url 'lists' %}" class="navbar-item">
Lists
</a>
{% endif %}
</div>
<div class="navbar-end">

View file

@ -65,6 +65,23 @@
{% if not list.curation == 'closed' or request.user == list.user %}
<section class="column is-one-quarter">
<h2>{% if list.curation == 'open' or request.user == list.user %}Add{% else %}Suggest{% endif %} Books</h2>
<form name="search" action="{% url 'list' list.id %}" method="GET" class="block">
<div class="field has-addons">
<div class="control">
<input aria-label="Search for a book" class="input" type="text" name="q" placeholder="Search for a book" value="{{ query }}">
</div>
<div class="control">
<button class="button" type="submit">
<span class="icon icon-search" title="Search">
<span class="is-sr-only">search</span>
</span>
</button>
</div>
</div>
{% if query %}
<p class="help"><a href="{% url 'list' list.id %}">Clear search</a></p>
{% endif %}
</form>
{% for book in suggested_books %}
<div class="block columns">
<div class="column is-narrow">

View file

@ -10,6 +10,7 @@ from django.views.decorators.http import require_POST
from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.connectors import connector_manager
from .helpers import is_api_request, object_visible_to_user, privacy_filter
@ -52,16 +53,22 @@ class List(View):
if is_api_request(request):
return ActivitypubResponse(book_list.to_activity())
suggestions = request.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)]
query = request.GET.get('q')
if query:
# search for books
suggestions = connector_manager.local_search(query, raw=True)
else:
# just suggest whatever books are nearby
suggestions = request.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)]
data = {
@ -69,6 +76,7 @@ class List(View):
'list': book_list,
'suggested_books': suggestions,
'list_form': forms.ListForm(instance=book_list),
'query': query or ''
}
return TemplateResponse(request, 'lists/list.html', data)