Màj du script de transfert, et correction de l'affichage de certaines infos

This commit is contained in:
Tom Hubrecht 2021-10-08 17:58:41 +02:00
parent b1e591ce2c
commit ebab4f5cb2
4 changed files with 176 additions and 77 deletions

View file

@ -40,6 +40,10 @@ class Profile(models.Model):
def __str__(self): def __str__(self):
return self.full_name return self.full_name
@property
def age(self):
return self.day.year - self.birth_date.year
def birthday(self): def birthday(self):
return self.birth_date.strftime("%d%m") return self.birth_date.strftime("%d%m")

View file

@ -106,7 +106,7 @@
<div class="studies"> <div class="studies">
<span class="label">{% trans "Études passées" %}</span> <span class="label">{% trans "Études passées" %}</span>
<span class="separator"></span> <span class="separator"></span>
<span class="value">{{ profile.past_studies }}</span> <span class="value">{{ profile.past_studies|linebreaksbr }}</span>
</div> </div>
{% endif %} {% endif %}
@ -114,7 +114,7 @@
<div class="studies"> <div class="studies">
<span class="label">{% trans "Expériences passées" %}</span> <span class="label">{% trans "Expériences passées" %}</span>
<span class="separator"></span> <span class="separator"></span>
<span class="value">{{ profile.experiences }}</span> <span class="value">{{ profile.experiences|linebreaksbr }}</span>
</div> </div>
{% endif %} {% endif %}
</div> </div>
@ -122,7 +122,7 @@
{% if profile.text_field %} {% if profile.text_field %}
<div class="free-text"> <div class="free-text">
<span class="label">{% trans "Champ libre" %}</span> <span class="label">{% trans "Champ libre" %}</span>
<p class="value">{{ profile.text_field }}</p> <p class="value">{{ profile.text_field|linebreaksbr }}</p>
</div> </div>
{% endif %} {% endif %}

View file

@ -2,11 +2,9 @@ from datetime import date, timedelta
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.mail import send_mail from django.core.mail import send_mail
from django.db.models import Q from django.db.models import DateTimeField, Q, Value
from django.forms import formset_factory
from django.forms.models import model_to_dict
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.utils import timezone from django.utils import timezone
@ -23,7 +21,7 @@ from fiches.forms import (
SearchForm, SearchForm,
SocialFormSet, SocialFormSet,
) )
from fiches.models import Address, Department, Mail, Phone, Profile, Social from fiches.models import Department, Profile
from fiches.utils import get_ldap_infos from fiches.utils import get_ldap_infos
@ -151,14 +149,14 @@ class BirthdayView(ListView):
context["result"] = list( context["result"] = list(
Profile.objects.filter( Profile.objects.filter(
birth_date__day=today.day, birth_date__month=today.month birth_date__day=today.day, birth_date__month=today.month
) ).annotate(day=Value(today, output_field=DateTimeField()))
) )
for i in range(1, 7): for i in range(1, 7):
today = today + timedelta(days=1) today = today + timedelta(days=1)
context["result"] += list( context["result"] += list(
Profile.objects.filter( Profile.objects.filter(
birth_date__day=today.day, birth_date__month=today.month birth_date__day=today.day, birth_date__month=today.month
) ).annotate(day=Value(today, output_field=DateTimeField()))
) )
return context return context

View file

@ -1,72 +1,82 @@
import json import json
import os import os
import sys import sys
from datetime import datetime from datetime import datetime
import django import django
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
print("\nTransfert des fiches annuaires :")
# Configuration # Configuration
print("Paramétrage de Django...", end=" ")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "annuaire.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "annuaire.settings")
django.setup() django.setup()
User = get_user_model() User = get_user_model()
from fiches.management.commands._ldap import ClipperLDAP from authens.models import CASAccount # noqa
from fiches.models import Address, Department, Mail, Phone, Profile
from fiches.management.commands._ldap import ClipperLDAP # noqa
from fiches.models import Address, Department, Mail, Phone, Profile # noqa
print("[ok]")
# Utilitaires # Utilitaires
def parse_date(s): def parse_date(s):
try: try:
return datetime.strptime(s, "%d/%m/%Y").date() return datetime.strptime(s, "%Y-%m-%d").date()
except ValueError: except (ValueError, TypeError):
return "" return None
def get_text_field(data): def get_text_field(data):
text = "" text = ""
if data["interets"]: if data["interests"]:
text += "Mes intérêts :\n" + data["interets"] + "\n\n" text += "Mes intérêts :\n" + data["interests"] + "\n\n"
if data["pages_web"]: if data["websites"]:
text += "Mes pages web :\n" + data["pages_web"] + "\n\n" text += "Mes pages web :\n\n" + data["websites"] + "\n\n"
if data["commentaires"]: if data["comments"]:
text += data["commentaires"] + "\n\n" text += data["comments"] + "\n\n"
return text return text
def get_address(data): def get_address(data):
content = data["lignes"] + "\n" content = data["lines"] + "\n"
if data["code_postal"] and data["ville"]: if data["zip"] and data["city"]:
content += data["code_postal"] + ", " + data["ville"] content += data["zip"] + ", " + data["city"]
elif data["code_postal"] or data["ville"]: elif data["zip"] or data["city"]:
content += data["code_postal"] + data["ville"] content += data["zip"] + data["city"]
if data["pays"]: if data["country"]:
content += ", " + data["pays"] content += ", " + data["country"]
return content return content
def parse_list(s): def parse_list(s):
return [p for p in s.split(",").split("\n").split(";") if p] s = s or ""
return [p for p in s.replace(",", ";").replace("\n", ";").split(";") if p]
def create_phones(s, profile, numeros, name="Téléphone"): def create_phones(s, profile, numeros, name="Téléphone"):
for p in parse_list(s): for p in parse_list(s):
numeros.append(Phone(profile=profile, name=name, content=p)) numeros.append(Phone(profile=profile, name=name, number=p))
def create_mails(s, profile, mails, name="E-mail"): def create_mails(s, profile, mails, name="E-mail"):
for m in parse_list(s): for m in parse_list(s):
mails.append(Mail(profile=profile, name=name, content=m)) mails.append(Mail(profile=profile, name=name, mail=m))
# On récupère la liste des élèves à créer print("Récupération des comptes clipper...", end=" ")
ldap = ClipperLDAP() ldap = ClipperLDAP()
# Pas besoin de filtrer les users déjà existants clippers = {c.uid: c for c in ldap.get_clipper_list(stdout=sys.stdout)}
clipper = ldap.get_clipper_list(stdout=sys.stdout)
print("[ok]")
# On récupère la liste des départements
depts = { depts = {
dept: Department.objects.get_or_create(name=dept)[0].id dept: Department.objects.get_or_create(name=dept)[0].id
for dept in ldap.verbose_depts.values() for dept in ldap.verbose_depts.values()
@ -77,12 +87,30 @@ users = {}
profiles_to_create = [] profiles_to_create = []
dept_m2m_to_create = [] dept_m2m_to_create = []
for clipper in clippers:
print("Création des comptes...", end=" ")
for clipper in clippers.values():
user = User(username=clipper.uid, email=clipper.email) user = User(username=clipper.uid, email=clipper.email)
users["clipper.uid"] = (user, clipper)
users_to_create.append(user) users_to_create.append(user)
User.objects.bulk_create(users_to_create) User.objects.bulk_create(users_to_create)
users = {u.username: (u, clippers[u.username]) for u in User.objects.all()}
print("[ok]")
print("Création des comptes Authens...", end=" ")
cas_accounts = []
for (u, c) in users.values():
cas_accounts.append(CASAccount(user=u, cas_login=c.uid, entrance_year=c.year))
CASAccount.objects.bulk_create(cas_accounts)
print("[ok]")
fiches = {} fiches = {}
references = {} references = {}
@ -90,58 +118,127 @@ adresses = []
devises = {} devises = {}
numeros = [] numeros = []
mails = [] mails = []
fiches_pk = {}
print("Récupération des anciennes fiches :")
# On recrée les profils # On recrée les profils
with open("old_fiches.json") as json_file: with open("old_fiches.json") as json_file:
data = json.load(json_file) data = json.load(json_file)
for obj in data: for obj in data:
if obj["model"] == "annuaire.fiches2021":
obj_data = obj["fields"] obj_data = obj["fields"]
user, clipper = users[obj_data["id"]] try:
fiches[obj_data["n_id"]] = Profile( user, clipper = users[obj_data["login"]]
fiches_pk[obj["pk"]] = user.username
fiches[obj_data["login"]] = Profile(
user=user, user=user,
full_name=" ".join(obj_data["prenom"], obj_data["nom"]), full_name=" ".join((obj_data["firstname"], obj_data["lastname"])),
promotion=clipper.promotion, promotion=clipper.year,
birth_date=parse_date(obj_data["date_de_naissance"]), birth_date=parse_date((obj_data["birthdate"] or "")),
past_studies=obj_data["etudes_passees"] + "\n" + obj_data["etudes"], past_studies=(
experiences=obj_data["experiences"], (obj_data["past_studies"] or "")
thurne=obj_data["thurne"], + "\n"
+ (obj_data["studies"] or "")
).strip(),
experiences=(obj_data["experiences"] or ""),
thurne=(obj_data["room"] or ""),
text_field=get_text_field(obj_data), text_field=get_text_field(obj_data),
) )
create_phones(data["telephones"], fiches[obj_data["n_id"]], numeros) create_phones(obj_data["phones"], fiches[obj_data["login"]], numeros)
create_phones( create_phones(
data["portables"], fiches[obj_data["n_id"]], numeros, name="Portable" obj_data["mobiles"],
fiches[obj_data["login"]],
numeros,
name="Portable",
) )
create_mails(data["emails"], fiches[obj_data["n_id"]], numeros) create_mails(obj_data["emails"], fiches[obj_data["login"]], mails)
dept_m2m_to_create.append( dept_m2m_to_create.append(
Profile.department.through( Profile.department.through(
profile=fiches[obj_data["n_id"]], department_id=depts[clipper.dept] profile=fiches[obj_data["login"]],
department_id=depts[clipper.dept],
) )
) )
except KeyError:
print(f"\tLogin inconnu : {obj_data['login']}")
elif obj["model"] == "annuaire.lesreferences2021":
try:
username = fiches_pk[obj["pk"]]
surnom = obj["fields"]["nickname"]
if surnom:
fiche = fiches[username]
if fiche.nickname:
fiche.nickname += ", "
fiche.nickname += surnom
except KeyError:
pass
elif obj["model"] == "annuaire.adresses2021":
try:
username = fiches_pk[obj["pk"]]
obj_data = obj["fields"]
fiche = fiches[username]
a = get_address(obj_data).strip()
if a:
adresses.append(Address(profile=fiche, name="Adresse", content=a))
except KeyError:
pass
elif obj["model"] == "annuaire.devises2021":
try:
username = fiches_pk[obj["pk"]]
obj_data = obj["fields"]
fiche = fiches[username]
if obj_data["quote"]:
fiche.text_field += f"\n{obj_data['quote']}"
if obj_data["source"]:
fiche.text_field += f" ({obj_data['source']})"
if obj_data["author"]:
fiche.text_field += f", ({obj_data['author']})"
if a:
adresses.append(Address(profile=fiche, name="Adresse", content=a))
except KeyError:
pass
print("Création des nouvelles fiches fiches...", end=" ")
Profile.objects.bulk_create(fiches.values()) Profile.objects.bulk_create(fiches.values())
profils = {p.user.username: p for p in Profile.objects.select_related("user")}
print("[ok]")
print("Rattachement des départements...", end=" ")
for dept_m2m in dept_m2m_to_create: for dept_m2m in dept_m2m_to_create:
dept_m2m.profile_id = dept_m2m.profile.id dept_m2m.profile = profils[dept_m2m.profile.user.username]
Profile.department.through.objects.bulk_create(dept_m2m_to_create) Profile.department.through.objects.bulk_create(dept_m2m_to_create)
# On récupère les adresses print("[ok]")
with open("old_adresses.json") as json_file:
data = json.load(json_file)
for obj in data: print("Création des numéros de téléphone...", end=" ")
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: for p in numeros:
p.profile_id = p.profile.id p.profile = profils[p.profile.user.username]
Phone.objects.bulk_create(numeros) Phone.objects.bulk_create(numeros)
print("[ok]")
print("Création des adresses mail...", end=" ")
for m in mails: for m in mails:
m.profile_id = m.profile.id m.profile = profils[m.profile.user.username]
Mail.objects.bulk_create(mails) Mail.objects.bulk_create(mails)
print("[ok]")
print("Création des adresses...", end=" ")
for a in adresses:
a.profile = profils[a.profile.user.username]
Address.objects.bulk_create(adresses)
print("[ok]")