Reformatage #29
3 changed files with 33 additions and 20 deletions
|
@ -1,5 +1,7 @@
|
|||
from django.apps import apps as global_apps
|
||||
from django.db import migrations
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
def forwards(apps, schema_editor):
|
||||
User = apps.get_model('auth', 'User')
|
||||
|
@ -11,7 +13,7 @@ def forwards(apps, schema_editor):
|
|||
|
||||
try:
|
||||
SocialAccount = apps.get_model('socialaccount', 'SocialAccount')
|
||||
EmailAddress = apps.get_model('account', 'EmailAddress')
|
||||
OldEmailAddress = apps.get_model('account', 'EmailAddress')
|
||||
except LookupError:
|
||||
# Allauth not installed
|
||||
# Simply create CAS accounts for every profile
|
||||
|
@ -31,6 +33,11 @@ def forwards(apps, schema_editor):
|
|||
migrate_user(user)
|
||||
return
|
||||
|
||||
|
||||
NewEmailAddress = apps.get_model('simple_email_confirmation',
|
||||
'EmailAddress')
|
||||
from simple_email_confirmation.models import EmailAddressManager
|
||||
|
||||
# Transfer from allauth to authens
|
||||
# Assumes usernames have the format <clipper>@<promo>
|
||||
# Assumes no clashing clipper accounts have ever been found
|
||||
|
@ -42,25 +49,22 @@ def forwards(apps, schema_editor):
|
|||
is_ens_mail = lambda mail: (
|
||||
mail is not None and (mail.endswith(".ens.fr") or mail.endswith(".ens.psl.eu")))
|
||||
new_conns = []
|
||||
new_mails = []
|
||||
|
||||
for user in oldusers:
|
||||
# Sanitize mail addresses
|
||||
# Move EmailAddress to new model
|
||||
addresses = user.emailaddress_set.all()
|
||||
if len(addresses) > 0:
|
||||
email = user.email
|
||||
is_email_ens = is_ens_mail(email)
|
||||
for address in addresses:
|
||||
if is_ens_mail(address.email):
|
||||
if email is None:
|
||||
email = address.email
|
||||
is_email_ens = True
|
||||
continue
|
||||
if address.primary or email is None or is_email_ens:
|
||||
email = address.email
|
||||
is_email_ens = False
|
||||
if email != user.email:
|
||||
user.email = email
|
||||
user.save()
|
||||
for addr in addresses:
|
||||
newaddr = NewEmailAddress(
|
||||
user=user, email=addr.email,
|
||||
set_at=timezone.now(),
|
||||
confirmed_at=(timezone.now() if addr.verified else None),
|
||||
key=EmailAddressManager().generate_key(),
|
||||
)
|
||||
if addr.primary and user.email != addr.email:
|
||||
print("Adresse principale inconsistante",
|
||||
user.email, addr.email)
|
||||
new_mails.append(newaddr)
|
||||
|
||||
# Create new CASAccount connexion
|
||||
saccounts = user.socialaccount_set.all()
|
||||
|
@ -71,13 +75,14 @@ def forwards(apps, schema_editor):
|
|||
saccount = saccounts[0]
|
||||
clipper = saccount.uid
|
||||
if "@" not in user.username:
|
||||
print(saccount)
|
||||
print(user.username)
|
||||
continue
|
||||
entrance_year = "20" + saccount.extra_data.get(
|
||||
"entrance_year", user.username.split("@")[1])
|
||||
new_conns.append(CASAccount(user=user, cas_login=clipper,
|
||||
entrance_year=int(entrance_year)))
|
||||
|
||||
|
||||
NewEmailAddress.objects.bulk_create(new_mails)
|
||||
CASAccount.objects.bulk_create(new_conns)
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
@ -91,3 +96,6 @@ class Migration(migrations.Migration):
|
|||
|
||||
if global_apps.is_installed('allauth'):
|
||||
dependencies.append(('socialaccount', '0003_extra_data_default_dict'))
|
||||
|
||||
if global_apps.is_installed('simple_email_confirmation'):
|
||||
dependencies.append(('simple_email_confirmation', '0001_initial'))
|
||||
|
|
|
@ -35,8 +35,13 @@ INSTALLED_APPS = [
|
|||
|
||||
'django_elasticsearch_dsl',
|
||||
|
||||
#'allauth',
|
||||
#'allauth.account', # Uncomment for transition
|
||||
#'allauth.socialaccount', # Allauth -> Authens
|
||||
|
||||
'widget_tweaks',
|
||||
|
||||
'simple_email_confirmation',
|
||||
'authens',
|
||||
'tastypie',
|
||||
'braces',
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
django==2.2.*
|
||||
django-cas-ng==3.5.*
|
||||
django-taggit==0.22.*
|
||||
python-ldap==3.0.*
|
||||
django-tinymce==2.7.*
|
||||
|
@ -10,3 +9,4 @@ django-tastypie==0.14.*
|
|||
lxml==4.2.*
|
||||
django-elasticsearch-dsl==0.4.*
|
||||
django-allauth-ens==1.1.*
|
||||
django-simple-email-confirmation
|
||||
|
|
Loading…
Reference in a new issue