Migrate django_cas_ng accounts to authens

This commit is contained in:
Martin Pépin 2020-09-22 20:53:27 +02:00
parent 0815f79739
commit 3967831e92
No known key found for this signature in database
GPG key ID: E7520278B1774448

View file

@ -0,0 +1,40 @@
import sys
from authens.shortcuts import fetch_cas_account
from django.db import migrations
def to_authens(apps, schema_editor):
User = apps.get_model("auth", "User")
CASAccount = apps.get_model("authens", "CASAccount")
failures = []
for user in User.objects.select_related("profile").all():
login_clipper = user.profile.login_clipper
if login_clipper:
ldap_info = fetch_cas_account(login_clipper)
if ldap_info is None:
failures.append(user)
else:
entrance_year = ldap_info["entrance_year"]
CASAccount.objects.create(
user=user, cas_login=login_clipper, entrance_year=entrance_year
)
if failures:
sys.stderr.write(" ---------- \n")
sys.stderr.write("Some accounts could not be linked to a CAS account:\n")
for user in failures:
sys.stderr.write(f"- {user.username}\n")
sys.stderr.write(" ---------- \n")
class Migration(migrations.Migration):
dependencies = [
("gestioncof", "0017_petitscours_uniqueness"),
]
operations = [
migrations.RunPython(to_authens, migrations.RunPython.noop),
]