forked from mirrors/bookwyrm
Search for books to add to lists
This commit is contained in:
parent
1e9189d43c
commit
6a68fe9475
5 changed files with 46 additions and 16 deletions
|
@ -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):
|
||||||
|
|
|
@ -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,10 +23,14 @@ 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:
|
||||||
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:
|
if len(search_results) >= 10:
|
||||||
break
|
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
|
return search_results
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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">
|
||||||
|
|
|
@ -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">
|
||||||
|
|
|
@ -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,16 +53,22 @@ 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())
|
||||||
|
|
||||||
suggestions = request.user.shelfbook_set.filter(
|
query = request.GET.get('q')
|
||||||
~Q(book__in=book_list.books.all())
|
if query:
|
||||||
)
|
# search for books
|
||||||
suggestions = [s.book for s in suggestions[:5]]
|
suggestions = connector_manager.local_search(query, raw=True)
|
||||||
if len(suggestions) < 5:
|
else:
|
||||||
suggestions += [s.default_edition for s in \
|
# just suggest whatever books are nearby
|
||||||
models.Work.objects.filter(
|
suggestions = request.user.shelfbook_set.filter(
|
||||||
~Q(editions__in=book_list.books.all()),
|
~Q(book__in=book_list.books.all())
|
||||||
).order_by('-updated_date')
|
)
|
||||||
][:5 - len(suggestions)]
|
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 = {
|
data = {
|
||||||
|
@ -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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue