mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2025-01-24 16:24:14 +03:00
Populate user name field in social auth (#3968)
* Populate user name field in social auth * Add docstring for populate_user * Fix missing def * Fix missing def * Add some type hints to the SocialAccountAdapter class --------- Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com> Co-authored-by: Bruno Alla <alla.brunoo@gmail.com>
This commit is contained in:
parent
d13e4270c2
commit
5a7de40b83
|
@ -1,16 +1,37 @@
|
|||
from typing import Any
|
||||
from __future__ import annotations
|
||||
|
||||
import typing
|
||||
|
||||
from allauth.account.adapter import DefaultAccountAdapter
|
||||
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
|
||||
from django.conf import settings
|
||||
from django.http import HttpRequest
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from allauth.socialaccount.models import SocialLogin
|
||||
from {{cookiecutter.project_slug}}.users.models import User
|
||||
|
||||
|
||||
class AccountAdapter(DefaultAccountAdapter):
|
||||
def is_open_for_signup(self, request: HttpRequest):
|
||||
def is_open_for_signup(self, request: HttpRequest) -> bool:
|
||||
return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)
|
||||
|
||||
|
||||
class SocialAccountAdapter(DefaultSocialAccountAdapter):
|
||||
def is_open_for_signup(self, request: HttpRequest, sociallogin: Any):
|
||||
def is_open_for_signup(self, request: HttpRequest, sociallogin: SocialLogin) -> bool:
|
||||
return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)
|
||||
|
||||
def populate_user(self, request: HttpRequest, sociallogin: SocialLogin, data: dict[str, typing.Any]) -> User:
|
||||
"""
|
||||
Populates user information from social provider info.
|
||||
|
||||
See: https://django-allauth.readthedocs.io/en/latest/advanced.html?#creating-and-populating-user-instances
|
||||
"""
|
||||
user = sociallogin.user
|
||||
if name := data.get("name"):
|
||||
user.name = name
|
||||
elif first_name := data.get("first_name"):
|
||||
user.name = first_name
|
||||
if last_name := data.get("last_name"):
|
||||
user.name += f" {last_name}"
|
||||
return super().populate_user(request, sociallogin, data)
|
||||
|
|
Loading…
Reference in New Issue
Block a user