5

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.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180
  • what is DjangoStrategy in your project? is a model? – elmonkeylp Sep 17 '14 at 17:42
  • No. Django Strategy is an object used by Python Social Auth. – Alejandro Veintimilla Sep 17 '14 at 17:53
  • Did you update `python-social-auth` to version `0.2.2`? If that's the case, then you need to update your pipeline, just drop the `strategy.backend` usage, and add a `backend` parameter to the function and use that one. – omab Sep 17 '14 at 19:24
  • No, I am on python-social-auth 0.2.1. It is weird because yesterday I did `pip-review --auto` that updates all the packages. Anyway I solved the problem using `kwargs['backend'].redirect_uri` and checking for the backend name there. I'm not sure if it is the best way to do it, but it worked. – Alejandro Veintimilla Sep 17 '14 at 20:02
  • Still my comment applies, the ``backend`` parameter was added at version ``0.2.0``. – omab Oct 02 '14 at 23:38

2 Answers2

5

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()
JEnriquePS
  • 81
  • 4
  • Hi. This is similar to what I had. Im courious, where do you define "backend"? Does this work with the latest version of python social auth? – Alejandro Veintimilla Oct 01 '14 at 03:24
  • Awesome, I was following code samples that used `strategy` and `strategy.backend` instead of just a `backend` argument. This works much better in modern Python Social Auth. – Oli Dec 03 '14 at 12:14
0

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.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180