mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-23 10:01:04 +00:00
Uses single query to get suggested users
This commit is contained in:
parent
1bd813c6f7
commit
c28fbbeb09
1 changed files with 11 additions and 18 deletions
|
@ -2,7 +2,7 @@
|
||||||
import math
|
import math
|
||||||
import logging
|
import logging
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.db.models import signals, Count, Q
|
from django.db.models import signals, Count, Q, Case, When, IntegerField
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
from bookwyrm.redis_store import RedisStore, r
|
from bookwyrm.redis_store import RedisStore, r
|
||||||
|
@ -84,24 +84,17 @@ class SuggestedUsers(RedisStore):
|
||||||
def get_suggestions(self, user, local=False):
|
def get_suggestions(self, user, local=False):
|
||||||
"""get suggestions"""
|
"""get suggestions"""
|
||||||
values = self.get_store(self.store_id(user), withscores=True)
|
values = self.get_store(self.store_id(user), withscores=True)
|
||||||
results = []
|
annotations = [
|
||||||
|
When(pk=int(pk), then=self.get_counts_from_rank(score)["mutuals"])
|
||||||
|
for (pk, score) in values
|
||||||
|
]
|
||||||
# annotate users with mutuals and shared book counts
|
# annotate users with mutuals and shared book counts
|
||||||
for user_id, rank in values:
|
users = models.User.objects.filter(
|
||||||
counts = self.get_counts_from_rank(rank)
|
is_active=True, bookwyrm_user=True, id__in=[pk for (pk, _) in values]
|
||||||
try:
|
).annotate(mutuals=Case(*annotations, output_field=IntegerField(), default=0))
|
||||||
user = models.User.objects.get(
|
if local:
|
||||||
id=user_id, is_active=True, bookwyrm_user=True
|
users = users.filter(local=True)
|
||||||
)
|
return users[:5]
|
||||||
except models.User.DoesNotExist as err:
|
|
||||||
# if this happens, the suggestions are janked way up
|
|
||||||
logger.exception(err)
|
|
||||||
continue
|
|
||||||
user.mutuals = counts["mutuals"]
|
|
||||||
if (local and user.local) or not local:
|
|
||||||
results.append(user)
|
|
||||||
if len(results) >= 5:
|
|
||||||
break
|
|
||||||
return results
|
|
||||||
|
|
||||||
|
|
||||||
def get_annotated_users(viewer, *args, **kwargs):
|
def get_annotated_users(viewer, *args, **kwargs):
|
||||||
|
|
Loading…
Reference in a new issue