experiENS/scripts/initalize_allauth.py

54 lines
1.2 KiB
Python
Raw Normal View History

2018-12-29 16:30:28 +01:00
import sys
2021-02-08 02:27:37 +01:00
from collections import defaultdict
2018-12-29 00:42:35 +01:00
from allauth.account.models import EmailAddress
from allauth.socialaccount.models import SocialAccount
2018-12-29 16:30:28 +01:00
from avisstage.models import Normalien
2018-12-29 00:42:35 +01:00
2018-12-29 16:30:28 +01:00
accounts = SocialAccount.objects.all().prefetch_related("user")
profils = Normalien.objects.all()
addresses = EmailAddress.objects.all()
addr_dict = defaultdict(set)
for addr in addresses:
addr_dict[addr.user_id].add(addr.email)
profil_dict = {profil.user_id: profil for profil in profils}
addr_to_create = []
for acc in accounts:
2018-12-29 00:42:35 +01:00
u = acc.user
2018-12-29 16:30:28 +01:00
2018-12-29 00:42:35 +01:00
try:
2018-12-29 16:30:28 +01:00
profil = profil_dict[u.id]
2018-12-29 00:42:35 +01:00
except KeyError:
2018-12-29 16:30:28 +01:00
continue
to_addr = set()
if profil.mail:
to_addr.add(profil.mail)
cp_ml = "%s@clipper.ens.fr" % acc.uid
try:
cp_ml = acc.extra_data["ldap"]["email"]
except KeyError:
pass
to_addr.add(cp_ml)
addrs = addr_dict[u.id]
print(u.username, ";".join(list(to_addr)), ";".join(list(addrs)))
to_addr -= addrs
has_prim = len(addrs) > 0
for addr in to_addr:
ml = EmailAddress(email=addr, user=u, verified=True, primary=has_prim)
has_prim = False
addr_to_create.append(ml)
if "--fake" not in sys.argv:
EmailAddress.objects.bulk_create(addr_to_create)