From 54e6877998d6d28fd3dfd2d24301ea1bd2a97da9 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Mon, 1 Feb 2021 11:46:37 +0100 Subject: [PATCH] =?UTF-8?q?Utilitaire=20pour=20transf=C3=A9rer=20les=20anc?= =?UTF-8?q?iennes=20fiches?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fiches/management/commands/get_photos.py | 2 +- transfert.py | 147 +++++++++++++++++++++++ 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 transfert.py diff --git a/fiches/management/commands/get_photos.py b/fiches/management/commands/get_photos.py index cb9c30f..cc10bd4 100644 --- a/fiches/management/commands/get_photos.py +++ b/fiches/management/commands/get_photos.py @@ -13,7 +13,7 @@ from ._ldap import AnnuaireLDAP class Command(BaseCommand): - help = "Si possible, import les photos des conscrit·e·s dans l'annuaire." + help = "Si possible, importe les photos des conscrit·e·s dans l'annuaire." def add_arguments(self, parser): group = parser.add_mutually_exclusive_group() diff --git a/transfert.py b/transfert.py new file mode 100644 index 0000000..4da0416 --- /dev/null +++ b/transfert.py @@ -0,0 +1,147 @@ +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) +