Skip to content

Commit

Permalink
Implement User#synchronize
Browse files Browse the repository at this point in the history
  • Loading branch information
jgorset committed Apr 26, 2011
1 parent dfa764b commit b0d2263
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Instances of the `User` model have the following properties:
* `created_at` - A datetime object describing when the user was registered.
* `last_seen_at` - A datetime object describing when the user was last seen.

You may synchronize these properties with Facebook at any time with the model's `synchronize` method.

`oauth_token` is an instance of the `OAuthToken` model, which has the following properties:

* `token` - A string describing the OAuth token itself.
Expand Down
28 changes: 28 additions & 0 deletions fandjango/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from django.db import models

from utils import get_facebook_profile

class Facebook:
"""
Facebook instances hold information on the current user and
Expand Down Expand Up @@ -78,6 +80,32 @@ def picture(self):
response = connection.getresponse()
return response.getheader('Location')

def synchronize(self):
"""Synchronize the user with Facebook's Graph API."""
if self.oauth_token.expired:
raise ValueError('Signed request expired.')

profile = get_facebook_profile(self.oauth_token.token)

self.facebook_id = profile.get('id')
self.facebook_username = profile.get('username')
self.first_name = profile.get('first_name')
self.last_name = profile.get('last_name')
self.profile_url = profile.get('link')
self.gender = profile.get('gender')
self.hometown = profile['hometown']['name'] if profile.has_key('hometown') else None
self.location = profile['location']['name'] if profile.has_key('location') else None
self.bio = profile.get('bio')
self.relationship_status = profile.get('relationship_status')
self.political_views = profile.get('political')
self.email = profile.get('email')
self.website = profile.get('website')
self.locale = profile.get('locale')
self.verified = profile.get('verified')
self.birthday = datetime.strptime(profile['birthday'], '%m/%d/%Y') if profile.get('birthday') else None

self.save()

def __unicode__(self):
return self.full_name

Expand Down

0 comments on commit b0d2263

Please sign in to comment.