Search lists

This commit is contained in:
Mouse Reeve 2021-02-01 11:50:47 -08:00
parent ec7a9f68c2
commit e865530142
2 changed files with 45 additions and 13 deletions

View file

@ -63,17 +63,36 @@
</div>
</div>
<div class="column">
<h2 class="title">Matching Users</h2>
{% if not user_results %}
<p>No users found for "{{ query }}"</p>
{% endif %}
{% for result in user_results %}
<div class="block">
{% include 'snippets/avatar.html' with user=result %}</h2>
{% include 'snippets/username.html' with user=result show_full=True %}</h2>
{% include 'snippets/follow_button.html' with user=result %}
</div>
{% endfor %}
<section class="block">
<h2 class="title">Matching Users</h2>
{% if not user_results %}
<p>No users found for "{{ query }}"</p>
{% endif %}
<ul>
{% for result in user_results %}
<li class="block">
{% include 'snippets/avatar.html' with user=result %}</h2>
{% include 'snippets/username.html' with user=result show_full=True %}</h2>
{% include 'snippets/follow_button.html' with user=result %}
</li>
{% endfor %}
</ul>
</section>
<section class="block">
<h2 class="title">Lists</h2>
{% if not list_results %}
<p>No lists found for "{{ query }}"</p>
{% endif %}
{% for result in list_results %}
<div class="block">
<ul>
<li>
<a href="{% url 'list' result.id %}">{{ result.name }}</a>
</li>
</ul>
</div>
{% endfor %}
</section>
</div>
</div>
{% endwith %}

View file

@ -10,7 +10,7 @@ from django.views import View
from bookwyrm import models
from bookwyrm.connectors import connector_manager
from bookwyrm.utils import regex
from .helpers import is_api_request
from .helpers import is_api_request, privacy_filter
from .helpers import handle_remote_webfinger
@ -32,7 +32,7 @@ class Search(View):
if re.match(r'\B%s' % regex.full_username, query):
handle_remote_webfinger(query)
# do a local user search
# do a user search
user_results = models.User.objects.annotate(
similarity=Greatest(
TrigramSimilarity('username', query),
@ -42,12 +42,25 @@ class Search(View):
similarity__gt=0.5,
).order_by('-similarity')[:10]
# any relevent lists?
list_results = privacy_filter(
request.user, models.List.objects, ['public', 'followers']
).annotate(
similarity=Greatest(
TrigramSimilarity('name', query),
TrigramSimilarity('description', query),
)
).filter(
similarity__gt=0.1,
).order_by('-similarity')[:10]
book_results = connector_manager.search(
query, min_confidence=min_confidence)
data = {
'title': 'Search Results',
'book_results': book_results,
'user_results': user_results,
'list_results': list_results,
'query': query,
}
return TemplateResponse(request, 'search_results.html', data)