2011-11-14 20:16:12 +00:00
|
|
|
#put logic for retrieving userlists here
|
|
|
|
|
|
|
|
import random
|
2011-11-14 20:40:33 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from regluit.core.models import Work
|
2011-11-14 20:16:12 +00:00
|
|
|
|
|
|
|
def other_users(user, how_many):
|
2012-07-09 14:53:44 +00:00
|
|
|
# do something more sophisticated later?
|
|
|
|
# limit to candidates with nonempty wishlists
|
2013-02-06 20:05:16 +00:00
|
|
|
candidates = User.objects.filter(wishlist__wishes__gte=1, is_staff=False).distinct()
|
2012-07-09 14:53:44 +00:00
|
|
|
count = candidates.count()
|
2011-11-14 20:16:12 +00:00
|
|
|
if count <= how_many :
|
2012-07-09 14:53:44 +00:00
|
|
|
user_list = candidates[0: count]
|
2011-11-14 20:16:12 +00:00
|
|
|
else :
|
|
|
|
slice = random.random() * (count - how_many)
|
2012-07-09 14:53:44 +00:00
|
|
|
user_list = candidates[slice: slice+how_many]
|
2011-11-14 20:16:12 +00:00
|
|
|
return user_list
|
2011-11-14 20:40:33 +00:00
|
|
|
|
|
|
|
def supporting_users(work, how_many):
|
2011-12-03 03:31:39 +00:00
|
|
|
# do something more sophisticated sometime later
|
2012-02-11 19:15:06 +00:00
|
|
|
count = work.num_wishes
|
2011-11-14 20:40:33 +00:00
|
|
|
if count <= how_many :
|
2013-02-06 20:05:16 +00:00
|
|
|
user_list = work.wished_by().filter(is_staff=False)[0: count]
|
2011-11-14 20:40:33 +00:00
|
|
|
else :
|
|
|
|
slice = random.random() * (count - how_many)
|
2013-02-06 20:05:16 +00:00
|
|
|
user_list = work.wished_by().filter(is_staff=False)[slice: slice+how_many]
|
2011-11-14 20:40:33 +00:00
|
|
|
return user_list
|
2011-12-03 03:31:39 +00:00
|
|
|
|
|
|
|
def work_list_users(work_list, how_many):
|
2012-01-21 00:35:13 +00:00
|
|
|
"""return up to how_many users with one of the works on work_list in their wishlist"""
|
|
|
|
#users = User.objects.filter(wishlist__works__in=work_list).distinct().reverse()
|
|
|
|
# for MySQL, avoiding a nested query is more efficient: https://docs.djangoproject.com/en/dev/ref/models/querysets/#in
|
2013-02-06 20:05:16 +00:00
|
|
|
users = User.objects.filter(wishlist__works__in=list(work_list[:100]), is_staff=False).distinct().reverse()
|
2012-01-21 00:35:13 +00:00
|
|
|
return users.all()[0:how_many]
|
2011-12-03 03:53:39 +00:00
|
|
|
|
|
|
|
def campaign_list_users(campaign_list, how_many):
|
2013-02-06 20:05:16 +00:00
|
|
|
users = User.objects.filter(wishlist__works__campaigns__in=list(campaign_list[:100]), is_staff=False).distinct().reverse()
|
2011-12-03 03:53:39 +00:00
|
|
|
count = users.count()
|
|
|
|
if count <= how_many :
|
|
|
|
user_list = users[0: count]
|
|
|
|
else :
|
2011-12-03 17:02:26 +00:00
|
|
|
user_list = users[0: how_many]
|
2011-12-03 03:53:39 +00:00
|
|
|
return user_list
|