Skip to content

Commit

Permalink
fixed the random fb friends function
Browse files Browse the repository at this point in the history
  • Loading branch information
tschellenbach committed Apr 6, 2012
1 parent d3c8bfb commit 8cce32c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion django_facebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


__license__ = 'BSD'
__version__ = '3.6.2'
__version__ = '3.6.3'
__maintainer__ = 'Thierry Schellenbach'
__email__ = '[email protected]'
__status__ = 'Production'
Expand Down
17 changes: 10 additions & 7 deletions django_facebook/model_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ def random_facebook_friends(self, user, gender=None, limit=3):
assert gender in (None, 'M', 'F'), 'Gender %s wasnt recognized' % gender

from django_facebook.utils import get_profile_class
facebook_cache_key = 'fusers_%s' % user.id
new_facebook_users = cache.get(facebook_cache_key)
facebook_cache_key = 'facebook_users_%s' % user.id
non_members = cache.get(facebook_cache_key)
profile_class = get_profile_class()
if not new_facebook_users:
if not non_members:
facebook_users = list(self.filter(user_id=user.id, gender=gender)[:50])
facebook_ids = [u.facebook_id for u in facebook_users]
new_facebook_users = list(profile_class.objects.filter(facebook_id__in=facebook_ids).select_related('user'))
cache.set(facebook_cache_key, new_facebook_users, 60*60)
members = list(profile_class.objects.filter(facebook_id__in=facebook_ids).select_related('user'))
member_ids = [p.facebook_id for p in members]
non_members = [u for u in facebook_users if u.facebook_id not in member_ids]

random_limit = min(len(new_facebook_users), 3)
cache.set(facebook_cache_key, non_members, 60*60)

random_limit = min(len(non_members), 3)
random_facebook_users = []
if random_limit:
random_facebook_users = random.sample(new_facebook_users, limit)
random_facebook_users = random.sample(non_members, limit)

return random_facebook_users

3 changes: 3 additions & 0 deletions django_facebook/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class FacebookUser(models.Model):

class Meta:
unique_together = ['user_id', 'facebook_id']

def __unicode__(self):
return u'Facebook user %s' % self.name


class FacebookLike(models.Model):
Expand Down

0 comments on commit 8cce32c

Please sign in to comment.