Gérer la fin de scolarité #9
1 changed files with 52 additions and 15 deletions
|
@ -2,7 +2,8 @@
|
|||
import ldap
|
||||
|
||||
from allauth.account.utils import user_email, user_field, user_username
|
||||
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
|
||||
from allauth.account.models import EmailAddress
|
||||
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter, get_account_adapter
|
||||
from allauth.socialaccount.models import SocialAccount
|
||||
|
||||
DEPARTMENTS_LIST = (
|
||||
|
@ -87,38 +88,74 @@ class LongTermClipperAccountAdapter(DefaultSocialAccountAdapter):
|
|||
# We need to check that the user is still the same as before
|
||||
ldap_data = get_ldap_infos(clipper)
|
||||
self._ldap_data = ldap_data
|
||||
if a.extra_data.get('annee', '-1') != ldap_data.get('annee', '00'):
|
||||
|
||||
if a.user.username != self.get_username(clipper, ldap_data):
|
||||
# The admission year is different
|
||||
# We need a new SocialAccount
|
||||
# But before that, we need to invalidate the email address of
|
||||
# the previous user
|
||||
email = ldap_data.get('email')
|
||||
print(email, 'deprecate')
|
||||
u_mails = EmailAddress.objects.filter(user=a.user)
|
||||
try:
|
||||
clipper_mail = u_mails.get(email=email)
|
||||
if clipper_mail.primary:
|
||||
n_mails = u_mails.filter(primary=False)
|
||||
if n_mails.exists():
|
||||
n_mails[0].set_as_primary()
|
||||
else:
|
||||
user_email(a.user, '')
|
||||
a.user.save()
|
||||
clipper_mail.delete()
|
||||
except EmailAddress.DoesNotExist:
|
||||
pass
|
||||
return
|
||||
|
||||
# The admission year is the same, we can update the model
|
||||
# The admission year is the same, we can update the model and keep
|
||||
# the previous SocialAccount instance
|
||||
a.provider = 'clipper'
|
||||
a.save()
|
||||
|
||||
# Redo the thing that had failed just before
|
||||
sociallogin.lookup()
|
||||
|
||||
except SocialAccount.DoesNotExist:
|
||||
return
|
||||
|
||||
def create_username(self, clipper, data):
|
||||
return "{}:{}".format(clipper, data.get('annee', '00'))
|
||||
def get_username(self, clipper, data):
|
||||
"""
|
||||
Util function to generate a unique username, by default 'clipper@promo'
|
||||
This is used to disambiguate and recognize if the person is the same
|
||||
"""
|
||||
return "{}@{}".format(clipper, data.get('annee', '00'))
|
||||
|
||||
def save_user(self, request, sociallogin, form=None):
|
||||
print("populate user", sociallogin.account.uid)
|
||||
user = sociallogin.user
|
||||
user.set_unusable_password()
|
||||
|
||||
def populate_user(self, request, sociallogin, data):
|
||||
clipper = sociallogin.account.uid
|
||||
ldap_data = self._ldap_data if hasattr(self, '_ldap_data') else get_ldap_infos(clipper)
|
||||
# Save extra data (only once)
|
||||
sociallogin.account.extra_data = sociallogin.extra_data = ldap_data
|
||||
username = self.create_username(clipper, data)
|
||||
first_name = data.get('first_name')
|
||||
last_name = data.get('last_name')
|
||||
ldap_data = self._ldap_data if hasattr(self, '_ldap_data') \
|
||||
else get_ldap_infos(clipper)
|
||||
|
||||
username = self.get_username(clipper, ldap_data)
|
||||
email = ldap_data.get('email')
|
||||
name = ldap_data.get('name')
|
||||
user = sociallogin.user
|
||||
user_username(user, username or '')
|
||||
user_email(user, email or '')
|
||||
name_parts = (name or '').split(' ')
|
||||
user_field(user, 'first_name', first_name or name_parts[0])
|
||||
user_field(user, 'last_name', last_name or ' '.join(name_parts[1:]))
|
||||
user_field(user, 'first_name', name_parts[0])
|
||||
user_field(user, 'last_name', ' '.join(name_parts[1:]))
|
||||
print(user.username, user)
|
||||
|
||||
# Ignore form
|
||||
get_account_adapter().populate_username(request, user)
|
||||
|
||||
# Save extra data (only once)
|
||||
sociallogin.account.extra_data = sociallogin.extra_data = ldap_data
|
||||
sociallogin.save(request)
|
||||
sociallogin.account.save()
|
||||
|
||||
return user
|
||||
|
||||
def deprecate_clippers():
|
||||
|
|
Loading…
Reference in a new issue