diff --git a/AuthApp/models.py b/AuthApp/models.py index 7447648..d8fdac9 100644 --- a/AuthApp/models.py +++ b/AuthApp/models.py @@ -74,8 +74,23 @@ class UserProfile(BaseModel): def create_user_profile(sender, instance, created, **kwargs): + user_profile = None 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') @@ -85,6 +100,7 @@ def preSaveUser(sender, instance, **kwargs): if not instance.email: instance.email = str(instance.username).lower() + try: instance.user_profile.modifiedDT = datetime.now() except: diff --git a/GeneralApp/allauth_funcs.py b/GeneralApp/allauth_funcs.py new file mode 100644 index 0000000..a632c74 --- /dev/null +++ b/GeneralApp/allauth_funcs.py @@ -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 \ No newline at end of file diff --git a/TWB/settings.py b/TWB/settings.py index 3bd81c4..595a5b4 100644 --- a/TWB/settings.py +++ b/TWB/settings.py @@ -48,6 +48,8 @@ LOGIN_REDIRECT_URL = '/profile/page/dashboard/' LOGOUT_REDIRECT_URL = '/profile/login/' ACCOUNT_SIGNUP_REDIRECT_URL = '/profile/login/' ACCOUNT_LOGOUT_ON_GET = True +# SOCIALACCOUNT_ADAPTER = 'GeneralApp.allauth_funcs.MyAccountAdapter' +ACCOUNT_ADAPTER = 'GeneralApp.allauth_funcs.MyAccountAdapter' AUTHENTICATION_BACKENDS = [ 'django.contrib.auth.backends.ModelBackend',