From e88e280994b8d875e06c1e262263bc42d81f5acf Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 2 Dec 2021 21:43:48 +0100 Subject: [PATCH] =?UTF-8?q?Import=20ref.=20comp.=20(fichier=20oubli=C3=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/but/import_refcomp.py | 113 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 app/but/import_refcomp.py diff --git a/app/but/import_refcomp.py b/app/but/import_refcomp.py new file mode 100644 index 00000000..6433ae54 --- /dev/null +++ b/app/but/import_refcomp.py @@ -0,0 +1,113 @@ +from xml.etree import ElementTree +from typing import TextIO + +from app import db + +from app.models.but_refcomp import ( + ApcReferentielCompetences, + ApcCompetence, + ApcSituationPro, + ApcAppCritique, + ApcComposanteEssentielle, + ApcNiveau, + ApcParcours, + ApcAnneeParcours, + ApcParcoursNiveauCompetence, +) +from app.scodoc.sco_exceptions import FormatError + + +def orebut_import_refcomp(xml_file: TextIO): + tree = ElementTree.parse(xml_file) + root = tree.getroot() + if root.tag != "referentiel_competence": + raise FormatError("élément racine 'referentiel_competence' manquant") + ref = ApcReferentielCompetences( + **ApcReferentielCompetences.attr_from_xml(root.attrib) + ) + db.session.add(ref) + competences = root.find("competences") + if not competences: + raise FormatError("élément 'competences' manquant") + for competence in competences.findall("competence"): + c = ApcCompetence(**ApcCompetence.attr_from_xml(competence.attrib)) + ref.competences.append(c) + # --- SITUATIONS + situations = competence.find("situations") + for situation in situations: + libelle = "".join(situation.itertext()).strip() + s = ApcSituationPro(competence_id=c.id, libelle=libelle) + c.situations.append(s) + # --- COMPOSANTES ESSENTIELLES + composantes = competence.find("composantes_essentielles") + for composante in composantes: + libelle = "".join(composante.itertext()).strip() + ce = ApcComposanteEssentielle(libelle=libelle) + c.composantes_essentielles.append(ce) + # --- NIVEAUX (années) + niveaux = competence.find("niveaux") + for niveau in niveaux: + niv = ApcNiveau(**ApcNiveau.attr_from_xml(niveau.attrib)) + c.niveaux.append(niv) + acs = niveau.find("acs") + for ac in acs: + libelle = "".join(ac.itertext()).strip() + code = ac.attrib["code"] + niv.app_critiques.append(ApcAppCritique(code=code, libelle=libelle)) + # --- PARCOURS + parcours = root.find("parcours") + if not parcours: + raise FormatError("élément 'parcours' manquant") + for parcour in parcours.findall("parcour"): + parc = ApcParcours(**ApcParcours.attr_from_xml(parcour.attrib)) + ref.parcours.append(parc) + for annee in parcour.findall("annee"): + a = ApcAnneeParcours(**ApcAnneeParcours.attr_from_xml(annee.attrib)) + parc.annees.append(a) + for competence in annee.findall("competence"): + nom = competence.attrib["nom"] + niveau = int(competence.attrib["niveau"]) + # Retrouve la competence + comp = ref.competences.filter_by(titre=nom).all() + if len(comp) == 0: + raise FormatError(f"competence {nom} référencée mais on définie") + elif len(comp) > 1: + raise FormatError(f"competence {nom} ambigüe") + ass = ApcParcoursNiveauCompetence( + niveau=niveau, annee_parcours=a, competence=comp[0] + ) + db.session.add(ass) + + db.session.commit() + return ref + + +""" +xmlfile = open("but-RT-refcomp-30112021.xml") +tree = ElementTree.parse(xmlfile) +# get root element +root = tree.getroot() +assert root.tag == "referentiel_competence" + +ref = ApcReferentielCompetences(**ApcReferentielCompetences.attr_from_xml(root.attrib)) + +competences = root.find("competences") +if not competences: + raise FormatError("élément 'competences' manquant") + +competence = competences.findall("competence")[0] # XXX + +from app.but.import_refcomp import * +f = open("but-RT-refcomp-30112021.xml") +ref = orebut_import_refcomp(f) +#------ +from app.but.import_refcomp import * +ref = ApcReferentielCompetences.query.first() +p = ApcParcours(code="PARC", libelle="Parcours Test") +ref.parcours.append(p) +annee = ApcAnneeParcours(numero=1) +p.annees.append(annee) +annee.competences +c = ref.competences.filter_by(titre="Administrer").first() +annee.competences.append(c) +"""