Skip to content

Instantly share code, notes, and snippets.

@yrik
Created December 30, 2013 11:08
Show Gist options
  • Save yrik/8180755 to your computer and use it in GitHub Desktop.
Save yrik/8180755 to your computer and use it in GitHub Desktop.
def get_avatars(backend, details, response, user=None, is_new=False,
*args, **kwargs):
"""Update user details using data from provider."""
if user is None:
return
changed = False # flag to track changes
try:
url = None
if backend.name == 'facebook' and "id" in response:
url = "http://graph.facebook.com/%s/picture?type=large" \
% response["id"]
elif backend.name == 'twitter':
url = response.get('profile_image_url', '').replace('_normal', '')
elif backend.name == 'google-oauth2' and "picture" in response:
url = response["picture"]
elif backend.name == 'linkedin' and "picture-url" in response:
url = response["picture-url"]
if url:
avatar = urlopen(url)
image_basename = slugify(user.username + " social")
image_name = '%s%s.jpg' % (int(time.time()), image_basename)
user.avatar.save(image_name, ContentFile(avatar.read()))
changed = True
except URLError, HTTPError:
pass
if changed:
user.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment