Skip to content

Instantly share code, notes, and snippets.

@esseti
Last active December 13, 2015 22:48
Show Gist options
  • Save esseti/4986758 to your computer and use it in GitHub Desktop.
Save esseti/4986758 to your computer and use it in GitHub Desktop.
Pipe for django-social-auth for having a user profile populated with facebook data
class Language(models.Model):
name = models.CharField(max_length=100, default='')
fb_id = models.CharField(max_length=100, default='0')
def __unicode__(self):
return str(self.fb_id)
class UserProfile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=100, default='')
surname = models.CharField(max_length=100, default='')
birthday = models.DateField(default=datetime.now, blank=True)
email = models.CharField(max_length=100, default='')
locale = models.CharField(max_length=100, default='')
picture = models.CharField(max_length=255, default='')
gender = models.CharField(max_length=100, default='')
hometown = models.CharField(max_length=255, default='')
languages = models.ManyToManyField(Language, blank=True, null=True)
'''
Created on Oct 5, 2012
@author: stefanotranquillini
'''
from social_auth.backends.facebook import FacebookBackend
from models import UserProfile, Language
import time
import logging
def get_user_addinfo(backend, details, response, social_user, uid,\
user, *args, **kwargs):
log = logging.getLogger(__name__)
log.debug('here we are')
#load profile, defaults when creation empty
#it returns a touple, profile is the object, created is the boolean if it's created or exists
profile, created = UserProfile.objects.get_or_create(user=user)
if created:
log.debug('created')
else:
log.debug('name: %s' %(user.username))
#if facebook then steal data
if backend.__class__ == FacebookBackend:
url = "http://graph.facebook.com/%s/picture?type=large" % response['id']
profile.picture = url # depends on where you saved it
log.debug(profile.picture)
#get name surname
first_name = response.get('first_name')
log.debug(first_name)
if first_name:
profile.name=first_name
log.debug(profile.name)
surname =response.get('last_name')
if surname:
profile.surname=surname
log.debug(profile.surname)
birthday =response.get('birthday')
if birthday:
date=birthday
date = date.replace(u'\xa0', '') # removes \xa0 char (&nbsp)
date = date.encode() # encode to asccii from unicode
date = time.strptime(date, "%m/%d/%Y")
profile.birthday=time.strftime("%Y-%m-%d", date);
log.debug(profile.birthday)
email =response.get('email')
if email:
profile.email=email
log.debug(profile.email)
locale =response.get('locale')
if locale:
profile.locale=locale
log.debug(profile.locale)
gender =response.get('gender')
if gender:
profile.gender=gender
log.debug(profile.gender)
#must parse hometown to exrtract the name of the town.
hometown =response.get('hometown')
if hometown:
profile.hometown=hometown.get('name')
log.debug(profile.hometown)
languages = response.get('languages')
log.debug(languages)
if languages:
for lang in languages:
l, created = Language.objects.get_or_create(fb_id=lang.get('id'), defaults={'fb_id':lang.get('id'),'name':lang.get('name')})
l.save()
profile.languages.add(l)
profile.save()
SOCIAL_AUTH_PIPELINE = (
'social_auth.backends.pipeline.social.social_auth_user',
#'social_auth.backends.pipeline.associate.associate_by_email',
'social_auth.backends.pipeline.user.get_username',
'social_auth.backends.pipeline.user.create_user',
'social_auth.backends.pipeline.social.associate_user',
'social_auth.backends.pipeline.user.update_user_details',
'social_auth.backends.pipeline.social.load_extra_data',
'general.pipes.get_user_addinfo',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment