Controls search type

This commit is contained in:
Mouse Reeve 2021-04-30 18:59:02 -07:00
parent 5ca9d2a7b0
commit 21a5147c3c
4 changed files with 39 additions and 42 deletions

View file

@ -10,14 +10,14 @@
</h1> </h1>
</div> </div>
<form class="block"> <form class="block" action="{% url 'search' %}" method="GET">
<div class="field has-addons"> <div class="field has-addons">
<div class="control"> <div class="control">
<input type="input" class="input" value="{{ query }}"> <input type="input" class="input" name="q" value="{{ query }}">
</div> </div>
<div class="control"> <div class="control">
<div class="select"> <div class="select">
<select> <select name="type">
<option value="book" {% if type == "book" %}selected{% endif %}>{% trans "Books" %}</option> <option value="book" {% if type == "book" %}selected{% endif %}>{% trans "Books" %}</option>
<option value="user" {% if type == "user" %}selected{% endif %}>{% trans "Users" %}</option> <option value="user" {% if type == "user" %}selected{% endif %}>{% trans "Users" %}</option>
<option value="list" {% if type == "list" %}selected{% endif %}>{% trans "Lists" %}</option> <option value="list" {% if type == "list" %}selected{% endif %}>{% trans "Lists" %}</option>
@ -35,25 +35,25 @@
<nav class="tabs"> <nav class="tabs">
<ul> <ul>
{% url 'search' "book" as url %} <li{% if type == "book" %} class="is-active"{% endif %}>
<li{% if request.path in url %} class="is-active"{% endif %}> <a href="{% url 'search' %}?q={{ query }}&type=book">{% trans "Books" %}</a>
<a href="{{ url }}">{% trans "Books" %}</a>
</li> </li>
{% url 'search' "user" as url %} <li{% if type == "user" %} class="is-active"{% endif %}>
<li{% if url in request.path %} class="is-active"{% endif %}> <a href="{% url 'search' %}?q={{ query }}&type=user">{% trans "Users" %}</a>
<a href="{{ url }}">{% trans "Users" %}</a>
</li> </li>
{% url 'search' "list" as url %} <li{% if type == "list" %} class="is-active"{% endif %}>
<li{% if url in request.path %} class="is-active"{% endif %}> <a href="{% url 'search' %}?q={{ query }}&type=list">{% trans "Lists" %}</a>
<a href="{{ url }}">{% trans "Lists" %}</a>
</li> </li>
</ul> </ul>
</nav> </nav>
<div class="block columns"> <section class="block">
<section class="block">
{% block panel %} {% block panel %}
{% endblock %} {% endblock %}
</section> {% if not results %}
</div> <p>
<em>{% blocktrans %}No results found for "{{ query }}"{% endblocktrans %}</em>
</p>
{% endif %}
</section>
{% endblock %} {% endblock %}

View file

@ -2,8 +2,6 @@
{% block panel %} {% block panel %}
{% for result in results %} {% include 'lists/list_items.html' with lists=results %}
hi
{% endfor %}
{% endblock %} {% endblock %}

View file

@ -164,7 +164,6 @@ urlpatterns = [
), ),
# search # search
re_path(r"^search/?$", views.Search.as_view(), name="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 # imports
re_path(r"^import/?$", views.Import.as_view()), re_path(r"^import/?$", views.Import.as_view()),
re_path(r"^import/(\d+)/?$", views.ImportStatus.as_view()), re_path(r"^import/(\d+)/?$", views.ImportStatus.as_view()),

View file

@ -18,10 +18,11 @@ from .helpers import handle_remote_webfinger
class Search(View): class Search(View):
"""search users or books""" """search users or books"""
def get(self, request, search_type=None): def get(self, request):
"""that search bar up top""" """that search bar up top"""
query = request.GET.get("q") query = request.GET.get("q")
min_confidence = request.GET.get("min_confidence", 0.1) min_confidence = request.GET.get("min_confidence", 0.1)
search_type = request.GET.get("type")
if is_api_request(request): if is_api_request(request):
# only return local book results via json so we don't cascade # 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) 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} data = {"query": query or "", "type": search_type}
results = {} results = endpoint(query, request.user, min_confidence) if query else {}
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)
return TemplateResponse( return TemplateResponse(
request, request, "search/{:s}.html".format(search_type), {**data, **results}
"search/{:s}.html".format(search_type or "book"),
{**data, **results}
) )
def book_search(query, min_confidence): def book_search(query, _, min_confidence):
"""that search bar up top""" """that search bar up top"""
return { return {
"query": query or "",
"results": connector_manager.search(query, min_confidence=min_confidence), "results": connector_manager.search(query, min_confidence=min_confidence),
} }
def user_search(query, viewer): def user_search(query, viewer, _):
"""that search bar up top""" """that search bar up top"""
# logged out viewers can't search users # logged out viewers can't search users
if not viewer.is_authenticated: if not viewer.is_authenticated:
return None return {}
# use webfinger for mastodon style account@domain.com username to load the user if # 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) # 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) handle_remote_webfinger(query)
return { return {
"query": query,
"results": ( "results": (
models.User.viewer_aware_objects(viewer) models.User.viewer_aware_objects(viewer)
.annotate( .annotate(
@ -86,10 +87,9 @@ def user_search(query, viewer):
} }
def list_search(query, viewer): def list_search(query, viewer, _):
"""any relevent lists?""" """any relevent lists?"""
return { return {
"query": query,
"results": ( "results": (
privacy_filter( privacy_filter(
viewer, viewer,