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.
You can try by getting user extra_data and after getting profile picture link:
from allauth.socialaccount.models import SocialAccount
extra_data = SocialAccount.objects.get(user=request.user).extra_data
print(extra_data['picture'])
In this way you get extra_data as a dictionary so you can simply extract profile picture