-
-
Save ekinertac/5874113 to your computer and use it in GitHub Desktop.
Django social auth avatar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from social_auth.signals import pre_update | |
from social_auth.backends.facebook import FacebookBackend | |
from social_auth.backends import google | |
def get_user_avatar(sender, user, response, details, **kwargs): | |
result = False | |
if "id" in response: | |
from apps.photo.models import PhotoModel | |
from urllib2 import urlopen, HTTPError | |
from django.template.defaultfilters import slugify | |
from apps.account.utils import user_display | |
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 == TwitterBackend: | |
url = response["profile_image_url"] | |
if url: | |
avatar = urlopen(url) | |
photo = PhotoModel(author = user, is_avatar = True) | |
photo.picture.save(slugify(user.username + "_social") + '.jpg', | |
ContentFile(avatar.read())) | |
photo.save() | |
except HTTPError: | |
pass | |
result = True | |
return result | |
pre_update.connect(social_extra_values, sender=None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment