A round of changes to speed up /list/popular

pull/1/head
Raymond Yee 2012-01-20 16:35:13 -08:00
parent 2a0c572211
commit a1e944b795
2 changed files with 7 additions and 8 deletions

View File

@ -25,13 +25,11 @@ def supporting_users(work, how_many):
return user_list return user_list
def work_list_users(work_list, how_many): def work_list_users(work_list, how_many):
users = User.objects.filter(wishlist__works__in=work_list).distinct().reverse() """return up to how_many users with one of the works on work_list in their wishlist"""
count = users.count() #users = User.objects.filter(wishlist__works__in=work_list).distinct().reverse()
if count <= how_many : # for MySQL, avoiding a nested query is more efficient: https://docs.djangoproject.com/en/dev/ref/models/querysets/#in
user_list = users[0: count] users = User.objects.filter(wishlist__works__in=list(work_list)).distinct().reverse()
else : return users.all()[0:how_many]
user_list = users[0: how_many]
return user_list
def campaign_list_users(campaign_list, how_many): def campaign_list_users(campaign_list, how_many):
users = User.objects.filter(wishlist__works__campaigns__in=campaign_list).distinct().reverse() users = User.objects.filter(wishlist__works__campaigns__in=campaign_list).distinct().reverse()

View File

@ -188,7 +188,8 @@ class WorkListView(ListView):
def work_set_counts(self,work_set): def work_set_counts(self,work_set):
counts={} counts={}
counts['unglued'] = work_set.annotate(ebook_count=Count('editions__ebooks')).filter(ebook_count__gt=0).count() # counts['unglued'] = work_set.annotate(ebook_count=Count('editions__ebooks')).filter(ebook_count__gt=0).count()
counts['unglued'] = work_set.filter(editions__ebooks__isnull=False).distinct().count()
counts['unglueing'] = work_set.filter(campaigns__status='ACTIVE').count() counts['unglueing'] = work_set.filter(campaigns__status='ACTIVE').count()
counts['wished'] = work_set.count() - counts['unglued'] - counts['unglueing'] counts['wished'] = work_set.count() - counts['unglued'] - counts['unglueing']
return counts return counts