问题
This worked last week. Maybe I did something wrong and messed it up somewhere else, or maybe it is a bug, or maybe it is just an update and I missed it while reading the docs.
I have a pipeline that gets the user's avatar and saves the URL:
def get_avatar(strategy, details, response, user, *args, **kwargs):
url = None
if strategy.backend.name == 'facebook':
url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
elif strategy.backend.name == "twitter":
if response['profile_image_url'] != '':
url = response['profile_image_url']
elif strategy.backend.name == "google-oauth2":
if response['image'].get('url') is not None:
url = response['image'].get('url')
It used to work, now, it gives me the error:
'DjangoStrategy' object has no attribute 'backend'
Please help, some beta users are already using my website and for the moment, they don't have a profile image.
回答1:
other solution :
def get_profile_picture(backend, user, response, details, *args, **kwargs):
url = None
profile = UserProfile.objects.get_or_create(user = user)[0]
if backend.name == 'facebook':
profile.photo = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
elif backend.name == "twitter":
if response['profile_image_url'] != '':
if not response.get('default_profile_image'):
avatar_url = response.get('profile_image_url_https')
if avatar_url:
avatar_url = avatar_url.replace('_normal.', '_bigger.')
profile.photo = avatar_url
elif backend.name == "google-oauth2":
if response['image'].get('url') is not None:
profile.photo = response['image'].get('url')
profile.save()
回答2:
Ok, so I will post the solution I found just in case someone has the same problem. I am not sure if this is the best way to do it, but it works:
if "facebook" in kwargs['backend'].redirect_uri:
url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
elif "twitter" in kwargs['backend'].redirect_uri:
if response['profile_image_url'] != '':
url = response['profile_image_url']
elif "google" in kwargs['backend'].redirect_uri:
if response['image'].get('url') is not None:
url = response['image'].get('url')
Other solutions are welcome.
来源:https://stackoverflow.com/questions/25896165/attributeerror-djangostrategy-object-has-no-attribute-backend-python-social