Gérer la fin de scolarité #9

Merged
champeno merged 23 commits from Evarin/archicubes into master 2018-09-30 00:03:27 +02:00
Showing only changes of commit cdb9c3c722 - Show all commits

View file

@ -2,7 +2,8 @@
import ldap import ldap
from allauth.account.utils import user_email, user_field, user_username 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 from allauth.socialaccount.models import SocialAccount
DEPARTMENTS_LIST = ( DEPARTMENTS_LIST = (
@ -87,38 +88,74 @@ class LongTermClipperAccountAdapter(DefaultSocialAccountAdapter):
# We need to check that the user is still the same as before # We need to check that the user is still the same as before
ldap_data = get_ldap_infos(clipper) ldap_data = get_ldap_infos(clipper)
self._ldap_data = ldap_data 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 # The admission year is different
# We need a new SocialAccount # 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 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.provider = 'clipper'
a.save() a.save()
# Redo the thing that had failed just before
sociallogin.lookup() sociallogin.lookup()
except SocialAccount.DoesNotExist: except SocialAccount.DoesNotExist:
return return
def create_username(self, clipper, data): def get_username(self, clipper, data):
return "{}:{}".format(clipper, data.get('annee', '00')) """
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 clipper = sociallogin.account.uid
ldap_data = self._ldap_data if hasattr(self, '_ldap_data') else get_ldap_infos(clipper) ldap_data = self._ldap_data if hasattr(self, '_ldap_data') \
# Save extra data (only once) else get_ldap_infos(clipper)
sociallogin.account.extra_data = sociallogin.extra_data = ldap_data
username = self.create_username(clipper, data) username = self.get_username(clipper, ldap_data)
first_name = data.get('first_name')
last_name = data.get('last_name')
email = ldap_data.get('email') email = ldap_data.get('email')
name = ldap_data.get('name') name = ldap_data.get('name')
user = sociallogin.user
user_username(user, username or '') user_username(user, username or '')
user_email(user, email or '') user_email(user, email or '')
name_parts = (name or '').split(' ') name_parts = (name or '').split(' ')
user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'first_name', name_parts[0])
user_field(user, 'last_name', last_name or ' '.join(name_parts[1:])) user_field(user, 'last_name', ' '.join(name_parts[1:]))
print(user.username, user) 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 return user
def deprecate_clippers(): def deprecate_clippers():