Move anntotated users quuery into suggested users module

This commit is contained in:
Mouse Reeve
2021-04-23 18:26:48 -07:00
parent dda21195de
commit 9880bdc75b
4 changed files with 35 additions and 52 deletions

View File

@ -190,47 +190,3 @@ def get_discover_books():
.order_by("-review__published_date__max")[:6]
)
)
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(viewer, *args, **kwargs):
""" Users, annotated with things they have in common """
return (
models.User.objects.filter(discoverable=True, is_active=True, *args, **kwargs)
.exclude(Q(id__in=viewer.blocks.all()) | Q(blocks=viewer))
.annotate(
mutuals=Count(
"following",
filter=Q(
~Q(id=viewer.id),
~Q(id__in=viewer.following.all()),
following__in=viewer.following.all(),
),
distinct=True,
),
shared_books=Count(
"shelfbook",
filter=Q(
~Q(id=viewer.id),
shelfbook__book__parent_work__in=[
s.book.parent_work for s in viewer.shelfbook_set.all()
],
),
distinct=True,
),
)
)