mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-25 19:11:09 +00:00
Add search for author
This commit is contained in:
parent
6b1ffbc634
commit
6c9ca0bf19
4 changed files with 49 additions and 1 deletions
|
@ -36,7 +36,7 @@
|
|||
<div class="field has-addons">
|
||||
<div class="control">
|
||||
{% if request.user.is_authenticated %}
|
||||
{% trans "Search for a book, user, or list" as search_placeholder %}
|
||||
{% trans "Search for a book, author, user, or list" as search_placeholder %}
|
||||
{% else %}
|
||||
{% trans "Search for a book" as search_placeholder %}
|
||||
{% endif %}
|
||||
|
|
17
bookwyrm/templates/search/author.html
Normal file
17
bookwyrm/templates/search/author.html
Normal file
|
@ -0,0 +1,17 @@
|
|||
{% extends 'search/layout.html' %}
|
||||
|
||||
{% block panel %}
|
||||
|
||||
{% if results %}
|
||||
<ul class="block">
|
||||
{% for author in results %}
|
||||
<li class="">
|
||||
<a href="{{ author.local_path }}" class="author" itemprop="author" itemscope itemtype="https://schema.org/Thing">
|
||||
<span itemprop="name">{{ author.name }}</span>
|
||||
</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
|
@ -20,6 +20,7 @@
|
|||
<div class="select" aria-label="{% trans 'Search type' %}">
|
||||
<select name="type">
|
||||
<option value="book" {% if type == "book" %}selected{% endif %}>{% trans "Books" %}</option>
|
||||
<option value="author" {% if type == "author" %}selected{% endif %}>{% trans "Authors" %}</option>
|
||||
{% if request.user.is_authenticated %}
|
||||
<option value="user" {% if type == "user" %}selected{% endif %}>{% trans "Users" %}</option>
|
||||
{% endif %}
|
||||
|
@ -42,6 +43,9 @@
|
|||
<li{% if type == "book" %} class="is-active"{% endif %}>
|
||||
<a href="{% url 'search' %}?q={{ query }}&type=book">{% trans "Books" %}</a>
|
||||
</li>
|
||||
<li{% if type == "author" %} class="is-active"{% endif %}>
|
||||
<a href="{% url 'search' %}?q={{ query }}&type=author">{% trans "Authors" %}</a>
|
||||
</li>
|
||||
{% if request.user.is_authenticated %}
|
||||
<li{% if type == "user" %} class="is-active"{% endif %}>
|
||||
<a href="{% url 'search' %}?q={{ query }}&type=user">{% trans "Users" %}</a>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
""" search views"""
|
||||
|
||||
import re
|
||||
|
||||
from django.contrib.postgres.search import TrigramSimilarity
|
||||
|
@ -39,6 +40,7 @@ class Search(View):
|
|||
|
||||
endpoints = {
|
||||
"book": book_search,
|
||||
"author": author_search,
|
||||
"user": user_search,
|
||||
"list": list_search,
|
||||
}
|
||||
|
@ -90,6 +92,31 @@ def book_search(request):
|
|||
return TemplateResponse(request, "search/book.html", data)
|
||||
|
||||
|
||||
def author_search(request):
|
||||
"""search for an author"""
|
||||
query = request.GET.get("q")
|
||||
query = query.strip()
|
||||
data = {"type": "author", "query": query}
|
||||
|
||||
results = (
|
||||
models.Author.objects.annotate(
|
||||
similarity=TrigramSimilarity("name", query),
|
||||
)
|
||||
.filter(
|
||||
similarity__gt=0.1,
|
||||
)
|
||||
.order_by("-similarity")
|
||||
)
|
||||
|
||||
paginated = Paginator(results, PAGE_LENGTH)
|
||||
page = paginated.get_page(request.GET.get("page"))
|
||||
data["results"] = page
|
||||
data["page_range"] = paginated.get_elided_page_range(
|
||||
page.number, on_each_side=2, on_ends=1
|
||||
)
|
||||
return TemplateResponse(request, "search/author.html", data)
|
||||
|
||||
|
||||
def user_search(request):
|
||||
"""user search: search for a user"""
|
||||
viewer = request.user
|
||||
|
|
Loading…
Reference in a new issue