Skip to content

Instantly share code, notes, and snippets.

@dima-kov
Created July 19, 2017 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dima-kov/24d1a7acd3eaff36d28f38b300cb8866 to your computer and use it in GitHub Desktop.
Save dima-kov/24d1a7acd3eaff36d28f38b300cb8866 to your computer and use it in GitHub Desktop.
Django Social Login Avatar Pipeline
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