How to extract profile_pic of a user when they are signing in from Google

Viewed 64

I'm making a django website, I have 2 login methods. One is the default email password login and another is by logging in from Google.

What I want is when the user logs in from google django should automatically fetch the profile_pic of that user and save it in my user model.

Here's my models.py for accounts app

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager, Group


# Custom User Manager
class CustomUserManager(BaseUserManager):
    def _create_user(self, email, password, first_name, last_name=None, **extra_fields):
        if (not email):
            raise ValueError("Email Must Be Provided")
        if (not password):
            raise ValueError("Password is not Provided")

        user = self.model(
            email=self.normalize_email(email),
            first_name=first_name,
            last_name=last_name,
            **extra_fields
        )

        user.set_password(password)
        user.save(using=self._db)

        return user

    def create_user(self, email, password, first_name, last_name=None, **extra_fields):
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_active', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, first_name, last_name, **extra_fields)

    def create_user_with_groups(self, email, password, first_name, last_name=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_active', True)
        extra_fields.setdefault('is_superuser', False)

        user = self._create_user(email, password, first_name, last_name)
        group = Group.objects.get(name='Content Writer')

        user.groups.add(group)
        print(user.groups)
        return user

    def create_superuser(self, email, password, first_name, last_name=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_active', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, first_name, last_name, **extra_fields)


# Custom user Model
class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(db_index=True, unique=True, max_length=254)
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255, null=True, blank=True)
    mobile = models.CharField(max_length=50, null=True, blank=True)
    address = models.CharField(max_length=250, null=True, blank=True)
    profile_pic = models.ImageField(null=True, blank=True, upload_to='profile_pics/users',
                                    default='default_profile_pic.svg')

    is_staff = models.BooleanField(default=True)
    is_active = models.BooleanField(default=True)
    is_superuser = models.BooleanField(default=False)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name']

    class Meta:
        verbose_name = 'User'
        verbose_name_plural = 'Users'

Is there any way of achieving this? If there is, then it would be preferred to have a solution using SCOPES

Here is my settings.py file for SCOPES

...
SOCIALACCOUNT_ADAPTER = 'MyApp.adapter.MySocialAccountAdapter'
SOCIALACCOUNT_QUERY_EMAIL = ACCOUNT_EMAIL_REQUIRED
SOCIALACCOUNT_LOGIN_ON_GET = True
SOCIALACCOUNT_AUTO_SIGNUP = True
SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        }
    }
}
...

EDIT: I found the 'picture' field in the extra data of my social account profile in admin panel. How do I make use of that value in 'picture' field in the 'profile_pic' field of my User model. What function should I write to overwrite the default functionality.

enter image description here

2 Answers

When you are using allauth in django . You get 3 more models. Go to your admin panel , under Social Accounts you will find all these models.

  1. Social Account
  2. Social Application Token
  3. Social Application

The first one ( Social Account ) object has a field of extra data . Go to any of the social account object and see the field extra data . You will have a text form dictionary . This will have the key profile_image ( or some other field name probably ) which will have the profile image url of the google logged in user .

Then just make a simple Django ORM . profile_image = dict(SocialAccount.objects.get(id = some_id).extra_data)["profile_image"] you will have the url .

I have used DJ REST Auth Package for social sign-in and signup https://pypi.org/project/dj-rest-auth/

# in you setting file
# >>>  Social Authentication Settings

SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_ENABLED = True
LOGIN_REDIRECT_URL = "/"
ACCOUNT_EMAIL_REQUIRED=True
ACCOUNT_USERNAME_REQURIED=True
ACCOUNT_ALLOW_REGISTRATION = env.bool("ACCOUNT_ALLOW_REGISTRATION", True)
SOCIALACCOUNT_ALLOW_REGISTRATION = env.bool("SOCIALACCOUNT_ALLOW_REGISTRATION", True)
SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        }
    }
    }
 
#I have written a method to get image from a url 
import requests
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
def save_image_from_url(model, url, name):
    r = requests.get(url)
    img_temp = NamedTemporaryFile(delete=True)
    img_temp.write(r.content)
    img_temp.flush()
    model.name = name
    model.profile_picture.save("{}.jpg".format(model.username), File(img_temp), save=True)

# Viewset for google login
class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter
    client_class = OAuth2Client
    permission_classes = [AllowAny, ]
    callback_url = "https://developers.google.com/oauthplayground"

    def get_response(self):
        serializer_class = self.get_response_serializer()
        user = self.user
        user_extra_data = SocialAccount.objects.filter(user=self.request.user).first().extra_data
        name = user_extra_data["name"]
        profile_image_url = user_extra_data["picture"]
        if not user.profile_picture:
            save_image_from_url(user, profile_image_url, name)
        user_detail = UserSerializer(user, many=False, context={"request": self.request})
        serializer = serializer_class(instance=self.token, context={'request': self.request})
        resp = serializer.data
        resp["token"] = resp["key"]
        resp.pop("key")
        resp["user"] = user_detail.data
        response = Response(resp, status=status.HTTP_200_OK)
        return response
Related