Oublis : script et ldap
This commit is contained in:
parent
b9e128cafb
commit
04f56ec0af
2 changed files with 48 additions and 20 deletions
|
@ -50,3 +50,6 @@ ELASTICSEARCH_DSL = {
|
||||||
'hosts': '127.0.0.1:9200'
|
'hosts': '127.0.0.1:9200'
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
CLIPPER_LDAP_SERVER = 'ldaps://ldap.spi.ens.fr:636'
|
||||||
|
|
|
@ -1,29 +1,54 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
from allauth.account.models import EmailAddress
|
from allauth.account.models import EmailAddress
|
||||||
from allauth.socialaccount.models import SocialAccount
|
from allauth.socialaccount.models import SocialAccount
|
||||||
|
|
||||||
accts = SocialAccount.objects.all()
|
from avisstage.models import Normalien
|
||||||
|
|
||||||
# Create email addresses
|
from collections import defaultdict
|
||||||
|
|
||||||
for acc in accts:
|
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:
|
||||||
u = acc.user
|
u = acc.user
|
||||||
print(u.username)
|
|
||||||
emls = EmailAddress.objects.filter(user=u, email_endswith="clipper.ens.fr")
|
|
||||||
try:
|
try:
|
||||||
cpeml = acc.extra_data["ldap"]["email"]
|
profil = profil_dict[u.id]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
cpeml = "%s@clipper.ens.fr" % acc.uid
|
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)
|
||||||
|
|
||||||
ml = EmailAddress(email=cpeml, user=u, verified=True, primary=True)
|
addrs = addr_dict[u.id]
|
||||||
if u.profil.mail != cpeml and u.profil.mail:
|
|
||||||
ml.primary = False
|
print(u.username, ";".join(list(to_addr)), ";".join(list(addrs)))
|
||||||
print(" Creating other primary address", u.profil.mail)
|
to_addr -= addrs
|
||||||
ml2 = EmailAddress(email=u.profil.mail, user=u, verified=True, primary=True)
|
has_prim = len(addrs) > 0
|
||||||
ml2.save()
|
|
||||||
if emls.exists():
|
for addr in to_addr:
|
||||||
print(" Clipper already exists", emls.values_list("email", flat=True))
|
ml = EmailAddress(email=addr, user=u, verified=True, primary=has_prim)
|
||||||
else:
|
has_prim = False
|
||||||
print(" Saving Clipper", cpeml)
|
addr_to_create.append(ml)
|
||||||
ml.save()
|
|
||||||
|
if "--fake" not in sys.argv:
|
||||||
|
EmailAddress.objects.bulk_create(addr_to_create)
|
||||||
|
|
Loading…
Reference in a new issue