Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
faster insert flow for likes and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
tschellenbach authored and wolph committed Jul 13, 2011
1 parent 90cffc1 commit a6db082
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
2 changes: 2 additions & 0 deletions django_facebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
- Retry on facebook connection errors
- errors op email already in use op register flow (auth backend issues?)
'''


40 changes: 32 additions & 8 deletions django_facebook/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hmac
import logging
import sys
from django_facebook.utils import mass_get_or_create

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -342,19 +343,34 @@ def store_likes(self, user, limit=1000, store=facebook_settings.FACEBOOK_STORE_L

if likes and store:
logger.info('storing likes to db')
base_queryset = FacebookLike.objects.filter(user=user)
base_queryset.all().delete()
global_defaults = dict(user=user)
id_field = 'facebook_id'
default_dict = {}
for like in likes:
created_time = datetime.datetime.strptime(like['created_time'], "%Y-%m-%dT%H:%M:%S+0000")
defaults = dict(created_time=created_time, name=like['name'])
FacebookLike.objects.get_or_create(
user=user, facebook_id=like['id'], defaults=defaults
default_dict[like['id']] = dict(
created_time = created_time,
category = like['category'],
name=like['name']
)
current_likes, inserted_likes = mass_get_or_create(
FacebookLike, base_queryset, id_field, default_dict, global_defaults
)
logger.debug('found %s likes and inserted %s new likes', len(current_likes), len(inserted_likes))




return likes

def store_friends(self, user, limit=1000, store=facebook_settings.FACEBOOK_STORE_FRIENDS):
'''
Sends a request to the facebook api to retrieve a users friends and stores them locally
'''
return []

from django_facebook.models import FacebookUser

#get the users friends
Expand All @@ -367,11 +383,19 @@ def store_friends(self, user, limit=1000, store=facebook_settings.FACEBOOK_STORE
#store the users for later retrieval
if store and friends:
logger.info('storing friends to db')
for friend in friends:
defaults = dict(name=friend['name'])
FacebookUser.objects.get_or_create(
user=user, facebook_id=friend['id'], defaults=defaults
)
#see which ids this user already stored
base_queryset = FacebookUser.objects.filter(user=user)
global_defaults = dict(user=user)
default_dict = {}
for f in friends:
default_dict[f['id']] = dict(name=f['name'])
id_field = 'facebook_id'

current_friends, inserted_friends = mass_get_or_create(
FacebookUser, base_queryset, id_field, default_dict, global_defaults
)
logger.debug('found %s friends and inserted %s new ones', len(current_friends), len(inserted_friends))


return friends

Expand Down
5 changes: 2 additions & 3 deletions django_facebook/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect


class FacebookProfileModel(models.Model):
'''
Abstract class to add to your profile model.
Expand Down Expand Up @@ -37,9 +38,6 @@ def post_facebook_registration(self, request):

return response




class FacebookUser(models.Model):
'''
Model for storing a users friends
Expand All @@ -65,5 +63,6 @@ class Meta:
unique_together = ['user', 'facebook_id']





2 changes: 2 additions & 0 deletions django_facebook/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
#url(r'^canvas/$', 'canvas', name='facebook_canvas'),
#url(r'^canvas/my_style/$', 'my_style', name='facebook_my_style'),

#TODO: figure out why autodiscover doenst seem to detect the admin
from django_facebook import admin
33 changes: 33 additions & 0 deletions django_facebook/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ def get_profile_class():
return profile_class


def mass_get_or_create(model_class, base_queryset, id_field, default_dict, global_defaults):
'''
Updates the data by inserting all not found records
Doesnt delete records if not in the new data
example usage
>>> model_class = ListItem #the class for which you are doing the insert
>>> base_query_set = ListItem.objects.filter(user=request.user, list=1) #query for retrieving currently stored items
>>> id_field = 'user_id' #the id field on which to check
>>> default_dict = {'12': dict(comment='my_new_item'), '13': dict(comment='super')} #list of default values for inserts
>>> global_defaults = dict(user=request.user, list_id=1) #global defaults
'''
current_instances = list(base_queryset)
current_ids = [unicode(getattr(c, id_field)) for c in current_instances]
print model_class
print 'current', current_ids
given_ids = map(unicode, default_dict.keys())
print 'give', given_ids
new_ids = [g for g in given_ids if g not in current_ids]
print 'new', new_ids
inserted_model_instances = []
for new_id in new_ids:
defaults = default_dict[new_id]
defaults[id_field] = new_id
defaults.update(global_defaults)
model_instance = model_class.objects.create(
**defaults
)
inserted_model_instances.append(model_instance)

#returns a list of existing and new items
return current_instances, inserted_model_instances



0 comments on commit a6db082

Please sign in to comment.