Created
July 19, 2017 17:56
-
-
Save dima-kov/24d1a7acd3eaff36d28f38b300cb8866 to your computer and use it in GitHub Desktop.
Django Social Login Avatar Pipeline
This file contains hidden or 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 django.core.files.base import ContentFile | |
from urllib.request import urlopen | |
def avatar(backend, strategy, details, response, user=None, *args, **kwargs): | |
url = None | |
if backend.name == 'facebook': | |
url = "http://graph.facebook.com/{}/picture?type=large".format( | |
response['id']) | |
filename = 'fb_avatar_{}.jpg'.format(user.username) | |
if backend.name == 'twitter': | |
url = response.get('profile_image_url', '').replace('_normal', '') | |
filename = url.split('/')[-1] | |
if backend.name == 'google-oauth2': | |
if response.get('image') and response['image'].get('url'): | |
url = response['image'].get('url') | |
filename = 'google_avatar_{}.jpg'.format(user.username) | |
if url: | |
user.avatar.save( | |
filename, | |
ContentFile(urlopen(url).read()), | |
save=False, | |
) | |
user.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment