Merge pull request #469 from mouse-reeve/user-search

Search on username and localname in user search
This commit is contained in:
Mouse Reeve 2021-01-03 08:31:46 -08:00 committed by GitHub
commit 1187016aba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View file

@ -211,6 +211,19 @@ class Views(TestCase):
response.context_data['book_results'][0].title, 'Gideon the Ninth') response.context_data['book_results'][0].title, 'Gideon the Ninth')
def test_search_html_response_users(self):
''' searches remote connectors '''
request = self.factory.get('', {'q': 'mouse'})
with patch('bookwyrm.views.is_api_request') as is_api:
is_api.return_value = False
with patch('bookwyrm.connectors.connector_manager.search'):
response = views.search(request)
self.assertIsInstance(response, TemplateResponse)
self.assertEqual(response.template_name, 'search_results.html')
self.assertEqual(
response.context_data['user_results'][0], self.local_user)
def test_import_page(self): def test_import_page(self):
''' there are so many views, this just makes sure it LOADS ''' ''' there are so many views, this just makes sure it LOADS '''
request = self.factory.get('') request = self.factory.get('')

View file

@ -5,6 +5,7 @@ from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.postgres.search import TrigramSimilarity from django.contrib.postgres.search import TrigramSimilarity
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db.models import Avg, Q from django.db.models import Avg, Q
from django.db.models.functions import Greatest
from django.http import HttpResponseNotFound, JsonResponse from django.http import HttpResponseNotFound, JsonResponse
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
@ -234,7 +235,10 @@ def search(request):
# do a local user search # do a local user search
user_results = models.User.objects.annotate( user_results = models.User.objects.annotate(
similarity=TrigramSimilarity('username', query), similarity=Greatest(
TrigramSimilarity('username', query),
TrigramSimilarity('localname', query),
)
).filter( ).filter(
similarity__gt=0.5, similarity__gt=0.5,
).order_by('-similarity')[:10] ).order_by('-similarity')[:10]