Merge pull request #3274 from Minnozz/author-search

Add search for author
This commit is contained in:
Mouse Reeve 2024-02-29 15:55:12 -08:00 committed by GitHub
commit ec52460f02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 49 additions and 1 deletions

View file

@ -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 %}

View 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 %}

View file

@ -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>

View file

@ -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