Helper for suggested users panel

This commit is contained in:
Mouse Reeve 2021-04-01 19:56:53 -07:00
parent 732c7a190b
commit 7121b74a6e
4 changed files with 20 additions and 25 deletions

View file

@ -6,7 +6,7 @@ from django.template.response import TemplateResponse
from django.views import View from django.views import View
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from .helpers import get_suggested_users from .helpers import get_annotated_users
# pylint: disable=no-self-use # pylint: disable=no-self-use
@method_decorator(login_required, name="dispatch") @method_decorator(login_required, name="dispatch")
@ -29,7 +29,7 @@ class Directory(View):
if scope == "local": if scope == "local":
filters["local"] = True filters["local"] = True
users = get_suggested_users(request.user, **filters) users = get_annotated_users(request.user, **filters)
sort = request.GET.get("sort") sort = request.GET.get("sort")
if sort == "recent": if sort == "recent":
users = users.order_by("-last_active_date") users = users.order_by("-last_active_date")

View file

@ -34,17 +34,7 @@ class Feed(View):
paginated = Paginator(activities, PAGE_LENGTH) paginated = Paginator(activities, PAGE_LENGTH)
suggested_users = ( suggested_users = get_suggested_users(request.user)
get_suggested_users(
request.user,
~Q(id=request.user.id),
~Q(followers=request.user),
~Q(follower_requests=request.user),
bookwyrm_user=True,
)
.order_by("-mutuals", "-last_active_date")
.all()[:5]
)
data = { data = {
**feed_page_data(request.user), **feed_page_data(request.user),

View file

@ -120,17 +120,8 @@ class GetStartedUsers(View):
) )
if user_results.count() < 5: if user_results.count() < 5:
suggested_users = ( suggested_users = get_suggested_users(request.user)
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": list(user_results) + list(suggested_users), "suggested_users": list(user_results) + list(suggested_users),
} }

View file

@ -192,7 +192,21 @@ def get_discover_books():
) )
def get_suggested_users(user, *args, **kwargs): def get_suggested_users(user):
""" bookwyrm users you don't already know """
return (
get_annotated_users(
user,
~Q(id=user.id),
~Q(followers=user),
~Q(follower_requests=user),
bookwyrm_user=True,
)
.order_by("-mutuals", "-last_active_date")
.all()[:5]
)
def get_annotated_users(user, *args, **kwargs):
""" Users, annotated with things they have in common """ """ Users, annotated with things they have in common """
return ( return (
models.User.objects.filter(discoverable=True, is_active=True, *args, **kwargs) models.User.objects.filter(discoverable=True, is_active=True, *args, **kwargs)