Add search for author

This commit is contained in:
Bart Schuurmans 2024-02-03 21:55:46 +01:00
parent 6b1ffbc634
commit 6c9ca0bf19
4 changed files with 49 additions and 1 deletions

View file

@ -36,7 +36,7 @@
<div class="field has-addons"> <div class="field has-addons">
<div class="control"> <div class="control">
{% if request.user.is_authenticated %} {% 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 %} {% else %}
{% trans "Search for a book" as search_placeholder %} {% trans "Search for a book" as search_placeholder %}
{% endif %} {% 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' %}"> <div class="select" aria-label="{% trans 'Search type' %}">
<select name="type"> <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="author" {% if type == "author" %}selected{% endif %}>{% trans "Authors" %}</option>
{% if request.user.is_authenticated %} {% if request.user.is_authenticated %}
<option value="user" {% if type == "user" %}selected{% endif %}>{% trans "Users" %}</option> <option value="user" {% if type == "user" %}selected{% endif %}>{% trans "Users" %}</option>
{% endif %} {% endif %}
@ -42,6 +43,9 @@
<li{% if type == "book" %} class="is-active"{% endif %}> <li{% if type == "book" %} class="is-active"{% endif %}>
<a href="{% url 'search' %}?q={{ query }}&type=book">{% trans "Books" %}</a> <a href="{% url 'search' %}?q={{ query }}&type=book">{% trans "Books" %}</a>
</li> </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 %} {% if request.user.is_authenticated %}
<li{% if type == "user" %} class="is-active"{% endif %}> <li{% if type == "user" %} class="is-active"{% endif %}>
<a href="{% url 'search' %}?q={{ query }}&type=user">{% trans "Users" %}</a> <a href="{% url 'search' %}?q={{ query }}&type=user">{% trans "Users" %}</a>

View file

@ -1,4 +1,5 @@
""" search views""" """ search views"""
import re import re
from django.contrib.postgres.search import TrigramSimilarity from django.contrib.postgres.search import TrigramSimilarity
@ -39,6 +40,7 @@ class Search(View):
endpoints = { endpoints = {
"book": book_search, "book": book_search,
"author": author_search,
"user": user_search, "user": user_search,
"list": list_search, "list": list_search,
} }
@ -90,6 +92,31 @@ def book_search(request):
return TemplateResponse(request, "search/book.html", data) 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): def user_search(request):
"""user search: search for a user""" """user search: search for a user"""
viewer = request.user viewer = request.user