mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-22 23:18:08 +00:00
Controls search type
This commit is contained in:
parent
5ca9d2a7b0
commit
21a5147c3c
4 changed files with 39 additions and 42 deletions
|
@ -10,14 +10,14 @@
|
|||
</h1>
|
||||
</div>
|
||||
|
||||
<form class="block">
|
||||
<form class="block" action="{% url 'search' %}" method="GET">
|
||||
<div class="field has-addons">
|
||||
<div class="control">
|
||||
<input type="input" class="input" value="{{ query }}">
|
||||
<input type="input" class="input" name="q" value="{{ query }}">
|
||||
</div>
|
||||
<div class="control">
|
||||
<div class="select">
|
||||
<select>
|
||||
<select name="type">
|
||||
<option value="book" {% if type == "book" %}selected{% endif %}>{% trans "Books" %}</option>
|
||||
<option value="user" {% if type == "user" %}selected{% endif %}>{% trans "Users" %}</option>
|
||||
<option value="list" {% if type == "list" %}selected{% endif %}>{% trans "Lists" %}</option>
|
||||
|
@ -35,25 +35,25 @@
|
|||
|
||||
<nav class="tabs">
|
||||
<ul>
|
||||
{% url 'search' "book" as url %}
|
||||
<li{% if request.path in url %} class="is-active"{% endif %}>
|
||||
<a href="{{ url }}">{% trans "Books" %}</a>
|
||||
<li{% if type == "book" %} class="is-active"{% endif %}>
|
||||
<a href="{% url 'search' %}?q={{ query }}&type=book">{% trans "Books" %}</a>
|
||||
</li>
|
||||
{% url 'search' "user" as url %}
|
||||
<li{% if url in request.path %} class="is-active"{% endif %}>
|
||||
<a href="{{ url }}">{% trans "Users" %}</a>
|
||||
<li{% if type == "user" %} class="is-active"{% endif %}>
|
||||
<a href="{% url 'search' %}?q={{ query }}&type=user">{% trans "Users" %}</a>
|
||||
</li>
|
||||
{% url 'search' "list" as url %}
|
||||
<li{% if url in request.path %} class="is-active"{% endif %}>
|
||||
<a href="{{ url }}">{% trans "Lists" %}</a>
|
||||
<li{% if type == "list" %} class="is-active"{% endif %}>
|
||||
<a href="{% url 'search' %}?q={{ query }}&type=list">{% trans "Lists" %}</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
<div class="block columns">
|
||||
<section class="block">
|
||||
{% block panel %}
|
||||
{% endblock %}
|
||||
</section>
|
||||
</div>
|
||||
<section class="block">
|
||||
{% block panel %}
|
||||
{% endblock %}
|
||||
{% if not results %}
|
||||
<p>
|
||||
<em>{% blocktrans %}No results found for "{{ query }}"{% endblocktrans %}</em>
|
||||
</p>
|
||||
{% endif %}
|
||||
</section>
|
||||
{% endblock %}
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
{% block panel %}
|
||||
|
||||
{% for result in results %}
|
||||
hi
|
||||
{% endfor %}
|
||||
{% include 'lists/list_items.html' with lists=results %}
|
||||
|
||||
{% endblock %}
|
||||
|
|
|
@ -164,7 +164,6 @@ urlpatterns = [
|
|||
),
|
||||
# search
|
||||
re_path(r"^search/?$", views.Search.as_view(), name="search"),
|
||||
re_path(r"^search/(?P<search_type>user|list|book)/?$", views.Search.as_view(), name="search"),
|
||||
# imports
|
||||
re_path(r"^import/?$", views.Import.as_view()),
|
||||
re_path(r"^import/(\d+)/?$", views.ImportStatus.as_view()),
|
||||
|
|
|
@ -18,10 +18,11 @@ from .helpers import handle_remote_webfinger
|
|||
class Search(View):
|
||||
"""search users or books"""
|
||||
|
||||
def get(self, request, search_type=None):
|
||||
def get(self, request):
|
||||
"""that search bar up top"""
|
||||
query = request.GET.get("q")
|
||||
min_confidence = request.GET.get("min_confidence", 0.1)
|
||||
search_type = request.GET.get("type")
|
||||
|
||||
if is_api_request(request):
|
||||
# only return local book results via json so we don't cascade
|
||||
|
@ -30,38 +31,39 @@ class Search(View):
|
|||
)
|
||||
return JsonResponse([r.json() for r in book_results], safe=False)
|
||||
|
||||
if not search_type:
|
||||
search_type = "user" if "@" in query else "book"
|
||||
|
||||
endpoints = {
|
||||
"book": book_search,
|
||||
"user": user_search,
|
||||
"list": list_search,
|
||||
}
|
||||
if not search_type in endpoints:
|
||||
search_type = "book"
|
||||
endpoint = endpoints[search_type]
|
||||
|
||||
data = {"query": query or "", "type": search_type}
|
||||
results = {}
|
||||
if query:
|
||||
# make a guess about what type of query this is for
|
||||
if search_type == "user" or (not search_type and "@" in query):
|
||||
results = user_search(query, request.user)
|
||||
elif search_type == "list":
|
||||
results = list_search(query, request.user)
|
||||
else:
|
||||
results = book_search(query, min_confidence)
|
||||
results = endpoint(query, request.user, min_confidence) if query else {}
|
||||
|
||||
return TemplateResponse(
|
||||
request,
|
||||
"search/{:s}.html".format(search_type or "book"),
|
||||
{**data, **results}
|
||||
request, "search/{:s}.html".format(search_type), {**data, **results}
|
||||
)
|
||||
|
||||
|
||||
def book_search(query, min_confidence):
|
||||
def book_search(query, _, min_confidence):
|
||||
"""that search bar up top"""
|
||||
|
||||
return {
|
||||
"query": query or "",
|
||||
"results": connector_manager.search(query, min_confidence=min_confidence),
|
||||
}
|
||||
|
||||
|
||||
def user_search(query, viewer):
|
||||
def user_search(query, viewer, _):
|
||||
"""that search bar up top"""
|
||||
# logged out viewers can't search users
|
||||
if not viewer.is_authenticated:
|
||||
return None
|
||||
return {}
|
||||
|
||||
# use webfinger for mastodon style account@domain.com username to load the user if
|
||||
# they don't exist locally (handle_remote_webfinger will check the db)
|
||||
|
@ -69,7 +71,6 @@ def user_search(query, viewer):
|
|||
handle_remote_webfinger(query)
|
||||
|
||||
return {
|
||||
"query": query,
|
||||
"results": (
|
||||
models.User.viewer_aware_objects(viewer)
|
||||
.annotate(
|
||||
|
@ -86,10 +87,9 @@ def user_search(query, viewer):
|
|||
}
|
||||
|
||||
|
||||
def list_search(query, viewer):
|
||||
def list_search(query, viewer, _):
|
||||
"""any relevent lists?"""
|
||||
return {
|
||||
"query": query,
|
||||
"results": (
|
||||
privacy_filter(
|
||||
viewer,
|
||||
|
|
Loading…
Reference in a new issue