Adds books in common as a recommendation metric

This commit is contained in:
Mouse Reeve 2021-03-27 08:15:39 -07:00
parent 0ef33d2acb
commit 25a3a7763c
2 changed files with 31 additions and 23 deletions

View file

@ -1,7 +1,7 @@
""" non-interactive pages """
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.db.models import Count, Q
from django.db.models import Q
from django.http import HttpResponseNotFound
from django.template.response import TemplateResponse
from django.utils import timezone
@ -34,9 +34,16 @@ class Feed(View):
paginated = Paginator(activities, PAGE_LENGTH)
suggested_users = get_suggested_users(
request.user, ~Q(id=request.user.id), bookwyrm_user=True
).order_by("-mutuals", "-last_active_date").all()[:5]
suggested_users = (
get_suggested_users(
request.user,
~Q(id=request.user.id),
~Q(followers=request.user),
bookwyrm_user=True,
)
.order_by("-mutuals", "-last_active_date")
.all()[:5]
)
data = {
**feed_page_data(request.user),

View file

@ -194,25 +194,26 @@ def get_discover_books():
def get_suggested_users(user, *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=user.blocks.all()) | Q(blocks=user)
).annotate(
mutuals=Count(
"following",
filter=Q(
~Q(id=user.id),
~Q(id__in=user.following.all()),
following__in=user.following.all()
return (
models.User.objects.filter(discoverable=True, is_active=True, *args, **kwargs)
.exclude(Q(id__in=user.blocks.all()) | Q(blocks=user))
.annotate(
mutuals=Count(
"following",
filter=Q(
~Q(id=user.id),
~Q(id__in=user.following.all()),
following__in=user.following.all(),
),
),
shared_books=Count(
"shelfbook",
filter=Q(
~Q(id=user.id),
shelfbook__book__parent_work__in=[
s.book.parent_work for s in user.shelfbook_set.all()
],
),
),
),
shared_books=Count(
"shelfbook",
filter=Q(
~Q(id=user.id),
shelfbook__book__parent_work__in=[
s.book.parent_work for s in user.shelfbook_set.all()]
)
)
)