0.11.19 Google Auth
This commit is contained in:
@@ -74,8 +74,23 @@ class UserProfile(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
def create_user_profile(sender, instance, created, **kwargs):
|
def create_user_profile(sender, instance, created, **kwargs):
|
||||||
|
user_profile = None
|
||||||
if created:
|
if created:
|
||||||
UserProfile.objects.create(user=instance)
|
user_profile = UserProfile.objects.create(user=instance)
|
||||||
|
|
||||||
|
# if user_profile and not user_profile.avatar:
|
||||||
|
# from allauth.socialaccount.models import SocialAccount
|
||||||
|
# # try:
|
||||||
|
# social_accounts = SocialAccount.objects.filter(user=instance)
|
||||||
|
# if social_accounts:
|
||||||
|
# for social_account in social_accounts:
|
||||||
|
# if 'picture' in social_account.account.extra_data and social_account.account.extra_data['picture']:
|
||||||
|
# with open(social_account.account.extra_data['picture'], 'rb') as fd:
|
||||||
|
# user_profile.avatar.save(f'avatar_{instance.id}.jpeg', fd.read(), True)
|
||||||
|
#
|
||||||
|
# # except Exception as e:
|
||||||
|
# # msg = f'post_save create_user_profile Error = {str(e)}'
|
||||||
|
# # print(msg)
|
||||||
|
|
||||||
|
|
||||||
post_save.connect(create_user_profile, sender=User, dispatch_uid='post_save_connect')
|
post_save.connect(create_user_profile, sender=User, dispatch_uid='post_save_connect')
|
||||||
@@ -85,6 +100,7 @@ def preSaveUser(sender, instance, **kwargs):
|
|||||||
if not instance.email:
|
if not instance.email:
|
||||||
instance.email = str(instance.username).lower()
|
instance.email = str(instance.username).lower()
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
instance.user_profile.modifiedDT = datetime.now()
|
instance.user_profile.modifiedDT = datetime.now()
|
||||||
except:
|
except:
|
||||||
|
|||||||
51
GeneralApp/allauth_funcs.py
Normal file
51
GeneralApp/allauth_funcs.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
|
||||||
|
from allauth.account.utils import user_field
|
||||||
|
from django.conf import settings
|
||||||
|
from allauth.account.adapter import DefaultAccountAdapter
|
||||||
|
import requests
|
||||||
|
from django.core.files import File
|
||||||
|
from django.core.files.temp import NamedTemporaryFile
|
||||||
|
|
||||||
|
class MyAccountAdapter(DefaultAccountAdapter):
|
||||||
|
|
||||||
|
def get_login_redirect_url(self, request):
|
||||||
|
path = super(MyAccountAdapter, self).get_login_redirect_url(request)
|
||||||
|
|
||||||
|
try:
|
||||||
|
user = request.user
|
||||||
|
user_profile = user.user_profile
|
||||||
|
if user_profile and not user_profile.avatar:
|
||||||
|
social_accounts = user.socialaccount_set.all()
|
||||||
|
if social_accounts:
|
||||||
|
for social_account in social_accounts:
|
||||||
|
if 'picture' in social_account.extra_data and social_account.extra_data['picture']:
|
||||||
|
r = requests.get(social_account.extra_data['picture'])
|
||||||
|
|
||||||
|
img_temp = NamedTemporaryFile()
|
||||||
|
img_temp.write(r.content)
|
||||||
|
img_temp.flush()
|
||||||
|
user_profile.avatar.save(f'avatar_{user.id}.jpeg', File(img_temp), True)
|
||||||
|
break
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
msg = f'post_save create_user_profile Error = {str(e)}'
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
return path
|
||||||
|
|
||||||
|
# class CustomSocialAccountAdapter(DefaultSocialAccountAdapter):
|
||||||
|
# def populate_user(self, request, sociallogin, data):
|
||||||
|
# from AuthApp.models import UserProfile
|
||||||
|
#
|
||||||
|
# user = super().populate_user(request, sociallogin, data)
|
||||||
|
# try:
|
||||||
|
# picture = sociallogin.account.extra_data['picture']
|
||||||
|
# user_profile = UserProfile.objects.get_or_create(user=user)
|
||||||
|
# with open(picture, 'rb') as fd:
|
||||||
|
# user_profile.avatar.save(f'user_{user.id}_avatar.jpeg', fd.read(), True)
|
||||||
|
# # user_field(user, "profile_photo", picture)
|
||||||
|
# except Exception as e:
|
||||||
|
# msg = f'CustomSocialAccountAdapter populate_user Error = {str(e)}'
|
||||||
|
# print(msg)
|
||||||
|
#
|
||||||
|
# return user
|
||||||
@@ -48,6 +48,8 @@ LOGIN_REDIRECT_URL = '/profile/page/dashboard/'
|
|||||||
LOGOUT_REDIRECT_URL = '/profile/login/'
|
LOGOUT_REDIRECT_URL = '/profile/login/'
|
||||||
ACCOUNT_SIGNUP_REDIRECT_URL = '/profile/login/'
|
ACCOUNT_SIGNUP_REDIRECT_URL = '/profile/login/'
|
||||||
ACCOUNT_LOGOUT_ON_GET = True
|
ACCOUNT_LOGOUT_ON_GET = True
|
||||||
|
# SOCIALACCOUNT_ADAPTER = 'GeneralApp.allauth_funcs.MyAccountAdapter'
|
||||||
|
ACCOUNT_ADAPTER = 'GeneralApp.allauth_funcs.MyAccountAdapter'
|
||||||
|
|
||||||
AUTHENTICATION_BACKENDS = [
|
AUTHENTICATION_BACKENDS = [
|
||||||
'django.contrib.auth.backends.ModelBackend',
|
'django.contrib.auth.backends.ModelBackend',
|
||||||
|
|||||||
Reference in New Issue
Block a user