ScoDoc-PE/app/but/import_refcomp.py

115 lines
4.2 KiB
Python
Raw Normal View History

2021-12-02 21:43:48 +01:00
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
2021-12-03 11:03:33 +01:00
def orebut_import_refcomp(xml_file: TextIO, dept_id: int, orig_filename=None):
2021-12-02 21:43:48 +01:00
tree = ElementTree.parse(xml_file)
root = tree.getroot()
if root.tag != "referentiel_competence":
raise FormatError("élément racine 'referentiel_competence' manquant")
2021-12-03 11:03:33 +01:00
args = ApcReferentielCompetences.attr_from_xml(root.attrib)
args["dept_id"] = dept_id
args["scodoc_orig_filename"] = orig_filename
ref = ApcReferentielCompetences(**args)
2021-12-02 21:43:48 +01:00
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")
2021-12-03 11:03:33 +01:00
ref = orebut_import_refcomp(f, 0)
2021-12-02 21:43:48 +01:00
#------
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)
"""