Skip to content

Commit

Permalink
Merge pull request #3274 from Minnozz/author-search
Browse files Browse the repository at this point in the history
Add search for author
  • Loading branch information
mouse-reeve authored Feb 29, 2024
2 parents 1fabe51 + 6c9ca0b commit ec52460
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bookwyrm/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
17 changes: 17 additions & 0 deletions bookwyrm/templates/search/author.html
Original file line number Diff line number Diff line change
@@ -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 %}
4 changes: 4 additions & 0 deletions bookwyrm/templates/search/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ <h1 class="title">
<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 %}
Expand All @@ -42,6 +43,9 @@ <h1 class="title">
<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>
Expand Down
27 changes: 27 additions & 0 deletions bookwyrm/views/search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" search views"""

import re

from django.contrib.postgres.search import TrigramSimilarity
Expand Down Expand Up @@ -39,6 +40,7 @@ def get(self, request):

endpoints = {
"book": book_search,
"author": author_search,
"user": user_search,
"list": list_search,
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ec52460

Please sign in to comment.