Skip to content

Instantly share code, notes, and snippets.

@kouroshshafi
Created January 23, 2012 12:39
Show Gist options
  • Save kouroshshafi/1662930 to your computer and use it in GitHub Desktop.
Save kouroshshafi/1662930 to your computer and use it in GitHub Desktop.
Extending User class to save profile picture in django-social-auth when registering
# This is models.py for a new user profile that you would like to create.
"""
this gist gets an id from django-social-auth and based on that saves the photo from social networks into your model. This is one of the best ways to extend User model because this way, you don't need to redefine a CustomUser as explained in the doc for django-social-auth. this is a new implementation based on https://gist.github.com/1248728
"""
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.db import models
class UserProfile(models.Model):
user = models.OneToOneField(User)
profile_photo = models.ImageField(upload_to='profiles')
def __str__(self):
return "%s's profile" % self.user
from social_auth.backends.facebook import FacebookBackend
from social_auth.backends import google
from social_auth.signals import socialauth_registered
def new_users_handler(sender, user, response, details, **kwargs):
user.is_new = True
if user.is_new:
if "id" in response:
from urllib2 import urlopen, HTTPError
from django.template.defaultfilters import slugify
from django.core.files.base import ContentFile
try:
url = None
if sender == FacebookBackend:
url = "http://graph.facebook.com/%s/picture?type=large" \
% response["id"]
elif sender == google.GoogleOAuth2Backend and "picture" in response:
url = response["picture"]
if url:
avatar = urlopen(url)
profile = UserProfile(user=user)
profile.profile_photo.save(slugify(user.username + " social") + '.jpg',
ContentFile(avatar.read()))
profile.save()
except HTTPError:
pass
return False
socialauth_registered.connect(new_users_handler, sender=None)
@dmeehan
Copy link

dmeehan commented Jan 29, 2012

Thanks for this snippet. I'm having a problem with it where it is creating two new users when I call the signal. Without the signal, the behavior is correct. Have you run into anything like this with your code?

@kouroshshafi
Copy link
Author

@dmeehan. No I have not faced such a problem; try commenting this line! because if I turn this line on, I get some other kind of errors.

SOCIAL_AUTH_USER_MODEL = 'myapp.CustomUser'

@hacknaked
Copy link

(from https://django-social-auth.readthedocs.org/en/v0.7.18/signals.html)

"Take into account that when defining a custom User model and declaring signal handler in models.py, the imports and handler definition must be made after the custom User model is defined or circular imports issues will be raised."

@hacknaked
Copy link

Signals are deprecated is Django SocialAuth, pipeline methods have to be used now.

from social_auth.signals.py:

"This module is deprecated, this signals aren't used by the code anymore and it's functionality should be replaced by pipeline methods."

@rafael-moontools
Copy link

Is this still working? I can't get profile picture from Goolge. Im I have to update something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment