Adds user search to get started view

This commit is contained in:
Mouse Reeve 2021-04-01 09:39:05 -07:00
parent 776f1768d9
commit fa17d9018f

View file

@ -2,6 +2,8 @@
import re import re
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.postgres.search import TrigramSimilarity
from django.db.models.functions import Greatest
from django.db.models import Count, Q from django.db.models import Count, Q
from django.http import HttpResponseNotFound from django.http import HttpResponseNotFound
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
@ -102,17 +104,34 @@ class GetStartedUsers(View):
def get(self, request): def get(self, request):
""" basic profile info """ """ basic profile info """
suggested_users = ( query = request.GET.get("query")
get_suggested_users( user_results = (
request.user, models.User.viewer_aware_objects(request.user)
~Q(id=request.user.id), .annotate(
~Q(followers=request.user), similarity=Greatest(
bookwyrm_user=True, TrigramSimilarity("username", query),
TrigramSimilarity("localname", query),
)
) )
.order_by("shared_books", "-mutuals", "-last_active_date") .filter(
.all()[:5] similarity__gt=0.5,
)
.order_by("-similarity")[:5]
) )
if user_results.count() < 5:
suggested_users = (
get_suggested_users(
request.user,
~Q(id=request.user.id),
~Q(followers=request.user),
~Q(id__in=user_results),
bookwyrm_user=True,
)
.order_by("shared_books", "-mutuals", "-last_active_date")
.all()[:5 - user_results.count()]
)
data = { data = {
"suggested_users": suggested_users, "suggested_users": list(user_results) + list(suggested_users),
} }
return TemplateResponse(request, "get_started/users.html", data) return TemplateResponse(request, "get_started/users.html", data)