Permalink
Browse files

gender and search support

  • Loading branch information...
1 parent e2a9259 commit 6877c38e1a07463d9b8585d0d66a893b2d19f5d6 @tschellenbach committed Apr 6, 2012
Showing with 36 additions and 4 deletions.
  1. +1 −1 django_facebook/__init__.py
  2. +5 −1 django_facebook/api.py
  3. +29 −2 django_facebook/model_managers.py
  4. +1 −0 django_facebook/models.py
@@ -4,7 +4,7 @@
__license__ = 'BSD'
-__version__ = '3.6.0'
+__version__ = '3.6.1'
__maintainer__ = 'Thierry Schellenbach'
__email__ = 'thierryschellenbach@gmail.com'
__status__ = 'Production'
View
@@ -557,9 +557,13 @@ def _store_friends(self, user, friends):
base_queryset = FacebookUser.objects.filter(user_id=user.id)
global_defaults = dict(user_id=user.id)
default_dict = {}
+ gender_map = dict(female='F', male='M')
for f in friends:
name = f.get('name')
- default_dict[str(f['id'])] = dict(name=name)
+ gender = None
+ if f.get('sex'):
+ gender = gender_map[f.get('sex')]
+ default_dict[str(f['id'])] = dict(name=name, gender=gender)
id_field = 'facebook_id'
current_friends, inserted_friends = mass_get_or_create(
@@ -1,6 +1,8 @@
from django.db.models.query_utils import Q
+from django.core.cache import cache
from django.db import models
import operator
+import random
@@ -15,8 +17,33 @@ def find_users(self, queries, base_queryset=None):
base_queryset = self.all()
filters = []
for query in queries:
- filters.append(Q(name__icontains=query))
+ match = Q(name__istartswith=query) | Q(name__icontains=' %s' % query)
+ filters.append(match)
users = base_queryset.filter(reduce(operator.and_, filters))
- return users
+ return users
+
+ def random_facebook_friends(self, user, gender=None, limit=3):
+ '''
+ Returns a random sample of your FB friends
+
+ Limit = Number of friends
+ Gender = None, M or F
+ '''
+ 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)
+ profile_class = get_profile_class()
+ if not new_facebook_users:
+ 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)
+
+ random_facebook_users = random.sample(new_facebook_users, limit)
+
+ return random_facebook_users
+
@@ -62,6 +62,7 @@ class FacebookUser(models.Model):
user_id = models.IntegerField()
facebook_id = models.BigIntegerField()
name = models.TextField(blank=True, null=True)
+ gender = models.CharField(choices=(('F', 'female'),('M', 'male')), blank=True, null=True, max_length=1)
objects = model_managers.FacebookUserManager()

0 comments on commit 6877c38

Please sign in to comment.