kpsul/gestioncof/migrations/0018_django_cas_to_authens.py
2020-09-22 21:42:10 +02:00

41 lines
1.2 KiB
Python

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),
]