Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-allauth retrieve avatar profile pictures

I use django-rest-auth and django-allauth about user registration/login using user's Facebook profile.

Now, I can take some standard information from Facebook (default for all auth).

Is possible to retrieve also list of avatar-pictures? How can make it in allAuth?

like image 415
Safari Avatar asked Mar 16 '15 16:03

Safari


2 Answers

If you want to get avatar pictures in a django template:

<img src="{{ user.socialaccount_set.all.0.get_avatar_url }}" />

Is that what you were trying to do?

like image 194
medullaskyline Avatar answered Oct 15 '22 11:10

medullaskyline


Let me quickly described how i did it (using all_auth and rest_auth).

Basically, 'allauth => socialaccount' provide user_signed_up and user_logged_in signals. So you need to catch them and get sociallogin object's extra data and populate your avatar_url.

Step-1: Create a UserProfile model: (to store avatar_url)

#models.py
try:
    from django.utils.encoding import force_text
except ImportError:
    from django.utils.encoding import force_unicode as force_text

class UserProfile(models.Model):
    user = models.OneToOneField(User, primary_key=True, verbose_name='user', related_name='profile')
    avatar_url = models.CharField(max_length=256, blank=True, null=True)

    def __str__(self):
        return force_text(self.user.email)

    class Meta():
        db_table = 'user_profile'

Step-2: Catch 'user_signed_up' signal and populate `avatar_url'

# signals.py

from allauth.account.signals import user_signed_up, user_logged_in

@receiver(user_signed_up)
def social_login_fname_lname_profilepic(sociallogin, user):
    preferred_avatar_size_pixels=256

    picture_url = "http://www.gravatar.com/avatar/{0}?s={1}".format(
        hashlib.md5(user.email.encode('UTF-8')).hexdigest(),
        preferred_avatar_size_pixels
    )

    if sociallogin:
        # Extract first / last names from social nets and store on User record
        if sociallogin.account.provider == 'twitter':
            name = sociallogin.account.extra_data['name']
            user.first_name = name.split()[0]
            user.last_name = name.split()[1]

        if sociallogin.account.provider == 'facebook':
            f_name = sociallogin.account.extra_data['first_name']
            l_name = sociallogin.account.extra_data['last_name']
            if f_name:
                user.first_name = f_name
            if l_name:
                user.last_name = l_name

            #verified = sociallogin.account.extra_data['verified']
            picture_url = "http://graph.facebook.com/{0}/picture?width={1}&height={1}".format(
                sociallogin.account.uid, preferred_avatar_size_pixels)

        if sociallogin.account.provider == 'google':
            f_name = sociallogin.account.extra_data['given_name']
            l_name = sociallogin.account.extra_data['family_name']
            if f_name:
                user.first_name = f_name
            if l_name:
                user.last_name = l_name
            #verified = sociallogin.account.extra_data['verified_email']
            picture_url = sociallogin.account.extra_data['picture']

    user.save()
    profile = UserProfile(user=user, avatar_url=picture_url)
    profile.save()        

Hope this will help you.

like image 11
Jadav Bheda Avatar answered Oct 15 '22 12:10

Jadav Bheda