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 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 ''' ''' only look at local search results '''
connector = load_connector(models.Connector.objects.get(local=True)) 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): def first_search_result(query, min_confidence=0.1):

View file

@ -11,7 +11,8 @@ from .abstract_connector import AbstractConnector, SearchResult
class Connector(AbstractConnector): class Connector(AbstractConnector):
''' instantiate a connector ''' ''' 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 ''' ''' search your local database '''
if not query: if not query:
return [] return []
@ -22,9 +23,13 @@ class Connector(AbstractConnector):
results = search_title_author(query, min_confidence) results = search_title_author(query, min_confidence)
search_results = [] search_results = []
for result in results: for result in results:
if raw:
search_results.append(result)
else:
search_results.append(self.format_search_result(result)) search_results.append(self.format_search_result(result))
if len(search_results) >= 10: if len(search_results) >= 10:
break break
if not raw:
search_results.sort(key=lambda r: r.confidence, reverse=True) search_results.sort(key=lambda r: r.confidence, reverse=True)
return search_results return search_results

View file

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

View file

@ -65,6 +65,23 @@
{% if not list.curation == 'closed' or request.user == list.user %} {% if not list.curation == 'closed' or request.user == list.user %}
<section class="column is-one-quarter"> <section class="column is-one-quarter">
<h2>{% if list.curation == 'open' or request.user == list.user %}Add{% else %}Suggest{% endif %} Books</h2> <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 %} {% for book in suggested_books %}
<div class="block columns"> <div class="block columns">
<div class="column is-narrow"> <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 import forms, models
from bookwyrm.activitypub import ActivitypubResponse from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.connectors import connector_manager
from .helpers import is_api_request, object_visible_to_user, privacy_filter from .helpers import is_api_request, object_visible_to_user, privacy_filter
@ -52,6 +53,12 @@ class List(View):
if is_api_request(request): if is_api_request(request):
return ActivitypubResponse(book_list.to_activity()) return ActivitypubResponse(book_list.to_activity())
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( suggestions = request.user.shelfbook_set.filter(
~Q(book__in=book_list.books.all()) ~Q(book__in=book_list.books.all())
) )
@ -69,6 +76,7 @@ class List(View):
'list': book_list, 'list': book_list,
'suggested_books': suggestions, 'suggested_books': suggestions,
'list_form': forms.ListForm(instance=book_list), 'list_form': forms.ListForm(instance=book_list),
'query': query or ''
} }
return TemplateResponse(request, 'lists/list.html', data) return TemplateResponse(request, 'lists/list.html', data)