import json import os import sys from datetime import datetime import django from django.contrib.auth import get_user_model # Configuration os.environ.setdefault("DJANGO_SETTINGS_MODULE", "annuaire.settings") django.setup() User = get_user_model() from fiches.management.commands._ldap import ClipperLDAP from fiches.models import Address, Department, Mail, Phone, Profile # Utilitaires def parse_date(s): try: return datetime.strptime(s, "%d/%m/%Y").date() except ValueError: return "" def get_text_field(data): text = "" if data["interets"]: text += "Mes intérêts :\n" + data["interets"] + "\n\n" if data["pages_web"]: text += "Mes pages web :\n" + data["pages_web"] + "\n\n" if data["commentaires"]: text += data["commentaires"] + "\n\n" return text def get_address(data): content = data["lignes"] + "\n" if data["code_postal"] and data["ville"]: content += data["code_postal"] + ", " + data["ville"] elif data["code_postal"] or data["ville"]: content += data["code_postal"] + data["ville"] if data["pays"]: content += ", " + data["pays"] return content def parse_list(s): return [p for p in s.split(",").split("\n").split(";") if p] def create_phones(s, profile, numeros, name="Téléphone"): for p in parse_list(s): numeros.append(Phone(profile=profile, name=name, content=p)) def create_mails(s, profile, mails, name="E-mail"): for m in parse_list(s): mails.append(Mail(profile=profile, name=name, content=m)) # On récupère la liste des élèves à créer ldap = ClipperLDAP() clipper_list = ldap.get_clipper_list(stdout=sys.stdout) # On vire les élèves déjà existants existing_users = set(User.objects.values_list("username", flat=True)) clippers = [clipper for clipper in clipper_list if clipper.uid not in existing_users] # On récupère la liste des départements depts = { dept: Department.objects.get_or_create(name=dept)[0].id for dept in ldap.verbose_depts.values() } users_to_create = [] users = {} profiles_to_create = [] dept_m2m_to_create = [] for clipper in clippers: user = User(username=clipper.uid, email=clipper.email) users["clipper.uid"] = (user, clipper) users_to_create.append(user) User.objects.bulk_create(users_to_create) fiches = {} references = {} adresses = [] devises = {} numeros = [] mails = [] # On recrée les profils with open("old_fiches.json") as json_file: data = json.load(json_file) for obj in data: obj_data = obj["fields"] user, clipper = users[obj_data["id"]] fiches[obj_data["n_id"]] = Profile( user=user, full_name=" ".join(obj_data["prenom"], obj_data["nom"]), promotion=clipper.promotion, birth_date=parse_date(obj_data["date_de_naissance"]), past_studies=obj_data["etudes_passees"] + "\n" + obj_data["etudes"], experiences=obj_data["experiences"], thurne=obj_data["thurne"], text_field=get_text_field(obj_data), ) create_phones(data["telephones"], fiches[obj_data["n_id"]], numeros) create_phones(data["portables"], fiches[obj_data["n_id"]], numeros, name="Portable") create_mails(data["emails"], fiches[obj_data["n_id"]], numeros) dept_m2m_to_create.append( Profile.department.through( profile=fiches[obj_data["n_id"]], department_id=depts[clipper.dept] ) ) Profile.objects.bulk_create(fiches.values()) for dept_m2m in dept_m2m_to_create: dept_m2m.profile_id = dept_m2m.profile.id Profile.department.through.objects.bulk_create(dept_m2m_to_create) # On récupère les adresses with open("old_adresses.json") as json_file: data = json.load(json_file) for obj in data: obj_data = obj["fields"] profile = fiches[obj_data["n_id"]] adresses.append( Address( profile=profile, name="Adresse", content=get_address(obj_data), ) ) create_phones(obj_data["telephones"], profile, numeros) for p in numeros: p.profile_id = p.profile.id Phone.objects.bulk_create(numeros) for m in mails: m.profile_id = m.profile.id Mail.objects.bulk_create(mails)