From 4e683f62e1567360e0e4a5e7abe9c811cacf2489 Mon Sep 17 00:00:00 2001 From: Robin Champenois Date: Sat, 23 Jan 2021 21:17:52 +0100 Subject: [PATCH] Move to django_simple_email_confirmation --- .../migrations/0004_allauth_to_authens.py | 46 +++++++++++-------- experiENS/settings_base.py | 5 ++ requirements.txt | 2 +- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/avisstage/migrations/0004_allauth_to_authens.py b/avisstage/migrations/0004_allauth_to_authens.py index 0693a74..164e9ab 100644 --- a/avisstage/migrations/0004_allauth_to_authens.py +++ b/avisstage/migrations/0004_allauth_to_authens.py @@ -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 @ # 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')) diff --git a/experiENS/settings_base.py b/experiENS/settings_base.py index 7208020..a4ba634 100644 --- a/experiENS/settings_base.py +++ b/experiENS/settings_base.py @@ -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', diff --git a/requirements.txt b/requirements.txt index e6ee626..d36d5a3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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