1
0
forked from ScoDoc/ScoDoc
ScoDoc/app/but/import_refcomp.py

127 lines
4.7 KiB
Python
Raw Normal View History

2021-12-03 14:13:49 +01:00
##############################################################################
# ScoDoc
2022-01-01 14:49:42 +01:00
# Copyright (c) 1999 - 2022 Emmanuel Viennet. All rights reserved.
2021-12-03 14:13:49 +01:00
# See LICENSE
##############################################################################
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,
)
2021-12-03 14:13:49 +01:00
from app.scodoc.sco_exceptions import ScoFormatError
2021-12-02 21:43:48 +01:00
2021-12-03 14:13:49 +01:00
def orebut_import_refcomp(xml_data: str, dept_id: int, orig_filename=None):
"""Importation XML Orébut
peut lever TypeError ou ScoFormatError
Résultat: instance de ApcReferentielCompetences
"""
try:
root = ElementTree.XML(xml_data)
except ElementTree.ParseError:
raise ScoFormatError("fichier XML Orébut invalide")
2021-12-02 21:43:48 +01:00
if root.tag != "referentiel_competence":
2021-12-03 14:13:49 +01:00
raise ScoFormatError("é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:
2021-12-03 14:13:49 +01:00
raise ScoFormatError("élément 'competences' manquant")
2021-12-02 21:43:48 +01:00
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:
2021-12-03 14:13:49 +01:00
raise ScoFormatError("élément 'parcours' manquant")
2021-12-02 21:43:48 +01:00
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:
2021-12-03 14:13:49 +01:00
raise ScoFormatError(f"competence {nom} référencée mais on définie")
2021-12-02 21:43:48 +01:00
elif len(comp) > 1:
2021-12-03 14:13:49 +01:00
raise ScoFormatError(f"competence {nom} ambigüe")
2021-12-02 21:43:48 +01:00
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:
2021-12-03 14:13:49 +01:00
raise ScoFormatError("élément 'competences' manquant")
2021-12-02 21:43:48 +01:00
competence = competences.findall("competence")[0] # XXX
from app.but.import_refcomp import *
2021-12-03 14:13:49 +01:00
dept_id = models.Departement.query.first().id
data = open("tests/data/but-RT-refcomp-exemple.xml").read()
ref = orebut_import_refcomp(data, dept_id)
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)
"""