MonScoDocEssai/app/models/but_refcomp.py

317 lines
10 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 12:08:03 +01:00
"""ScoDoc 9 models : Référentiel Compétence BUT 2021
"""
2021-12-03 11:03:33 +01:00
from datetime import datetime
2021-12-02 12:08:03 +01:00
from enum import unique
from typing import Any
from sqlalchemy.orm import class_mapper
import sqlalchemy
2021-12-02 12:08:03 +01:00
from app import db
from app.scodoc.sco_utils import ModuleType
# from https://stackoverflow.com/questions/2537471/method-of-iterating-over-sqlalchemy-models-defined-columns
def attribute_names(cls):
"liste ids (noms de colonnes) d'un modèle"
return [
prop.key
for prop in class_mapper(cls).iterate_properties
if isinstance(prop, sqlalchemy.orm.ColumnProperty)
]
2021-12-02 12:08:03 +01:00
class XMLModel:
_xml_attribs = {} # to be overloaded
id = "_"
@classmethod
def attr_from_xml(cls, args: dict) -> dict:
"""dict with attributes imported from Orébut XML
and renamed for our models.
The mapping is specified by the _xml_attribs
attribute in each model class.
Keep only attributes corresponding to columns in our model:
other XML attributes are simply ignored.
2021-12-02 12:08:03 +01:00
"""
columns = attribute_names(cls)
renamed_attributes = {cls._xml_attribs.get(k, k): v for (k, v) in args.items()}
return {k: renamed_attributes[k] for k in renamed_attributes if k in columns}
2021-12-02 12:08:03 +01:00
def __repr__(self):
return f'<{self.__class__.__name__} {self.id} "{self.titre if hasattr(self, "titre") else ""}">'
class ApcReferentielCompetences(db.Model, XMLModel):
id = db.Column(db.Integer, primary_key=True)
dept_id = db.Column(db.Integer, db.ForeignKey("departement.id"), index=True)
annexe = db.Column(db.Text())
2021-12-02 12:08:03 +01:00
specialite = db.Column(db.Text())
specialite_long = db.Column(db.Text())
type_titre = db.Column(db.Text())
type_structure = db.Column(db.Text())
type_departement = db.Column(db.Text()) # "secondaire", "tertiaire"
version_orebut = db.Column(db.Text())
2021-12-03 11:03:33 +01:00
_xml_attribs = { # Orébut xml attrib : attribute
2021-12-02 12:08:03 +01:00
"type": "type_titre",
"version": "version_orebut",
2021-12-02 12:08:03 +01:00
}
2021-12-03 11:03:33 +01:00
# ScoDoc specific fields:
scodoc_date_loaded = db.Column(db.DateTime, default=datetime.utcnow)
scodoc_orig_filename = db.Column(db.Text())
# Relations:
2021-12-02 12:08:03 +01:00
competences = db.relationship(
"ApcCompetence",
backref="referentiel",
lazy="dynamic",
cascade="all, delete-orphan",
)
parcours = db.relationship(
"ApcParcours",
backref="referentiel",
lazy="dynamic",
cascade="all, delete-orphan",
)
2021-12-03 15:44:57 +01:00
formations = db.relationship("Formation", backref="referentiel_competence")
2021-12-02 12:08:03 +01:00
def to_dict(self):
"""Représentation complète du ref. de comp.
comme un dict.
"""
2021-12-02 17:36:32 +01:00
return {
"dept_id": self.dept_id,
"annexe": self.annexe,
2021-12-02 17:36:32 +01:00
"specialite": self.specialite,
"specialite_long": self.specialite_long,
"type_structure": self.type_structure,
"type_departement": self.type_departement,
2021-12-02 17:36:32 +01:00
"type_titre": self.type_titre,
"version_orebut": self.version_orebut,
2021-12-03 15:44:57 +01:00
"scodoc_date_loaded": self.scodoc_date_loaded.isoformat() + "Z"
if self.scodoc_date_loaded
else "",
2021-12-03 11:03:33 +01:00
"scodoc_orig_filename": self.scodoc_orig_filename,
2021-12-02 17:36:32 +01:00
"competences": {x.titre: x.to_dict() for x in self.competences},
"parcours": {x.code: x.to_dict() for x in self.parcours},
}
2021-12-02 12:08:03 +01:00
class ApcCompetence(db.Model, XMLModel):
2021-12-03 11:03:33 +01:00
__table_args__ = (
# les compétences dans Orébut sont identifiées par leur "titre"
# unique au sein d'un référentiel:
db.UniqueConstraint(
"referentiel_id", "titre", name="apc_competence_referentiel_id_titre_key"
),
)
2021-12-02 12:08:03 +01:00
id = db.Column(db.Integer, primary_key=True)
referentiel_id = db.Column(
db.Integer, db.ForeignKey("apc_referentiel_competences.id"), nullable=False
)
id_orebut = db.Column(db.Integer, nullable=True, index=True, unique=True)
2021-12-03 11:03:33 +01:00
titre = db.Column(db.Text(), nullable=False, index=True)
2021-12-02 12:08:03 +01:00
titre_long = db.Column(db.Text())
couleur = db.Column(db.Text())
numero = db.Column(db.Integer) # ordre de présentation
_xml_attribs = { # xml_attrib : attribute
"id": "id_orebut",
"nom_court": "titre", # was name
2021-12-02 12:08:03 +01:00
"libelle_long": "titre_long",
}
situations = db.relationship(
"ApcSituationPro",
backref="competence",
lazy="dynamic",
cascade="all, delete-orphan",
)
composantes_essentielles = db.relationship(
"ApcComposanteEssentielle",
backref="competence",
lazy="dynamic",
cascade="all, delete-orphan",
)
niveaux = db.relationship(
"ApcNiveau",
backref="competence",
lazy="dynamic",
cascade="all, delete-orphan",
)
2021-12-02 17:36:32 +01:00
def to_dict(self):
return {
"id_orebut": self.id_orebut,
2021-12-02 17:36:32 +01:00
"titre": self.titre,
"titre_long": self.titre_long,
"couleur": self.couleur,
"numero": self.numero,
"situations": [x.to_dict() for x in self.situations],
"composantes_essentielles": [
x.to_dict() for x in self.composantes_essentielles
],
"niveaux": {x.annee: x.to_dict() for x in self.niveaux},
}
2021-12-02 12:08:03 +01:00
class ApcSituationPro(db.Model, XMLModel):
id = db.Column(db.Integer, primary_key=True)
competence_id = db.Column(
db.Integer, db.ForeignKey("apc_competence.id"), nullable=False
)
libelle = db.Column(db.Text(), nullable=False)
# aucun attribut (le text devient le libellé)
2021-12-02 17:36:32 +01:00
def to_dict(self):
return {"libelle": self.libelle}
2021-12-02 12:08:03 +01:00
class ApcComposanteEssentielle(db.Model, XMLModel):
id = db.Column(db.Integer, primary_key=True)
competence_id = db.Column(
db.Integer, db.ForeignKey("apc_competence.id"), nullable=False
)
libelle = db.Column(db.Text(), nullable=False)
2021-12-02 17:36:32 +01:00
def to_dict(self):
return {"libelle": self.libelle}
2021-12-02 12:08:03 +01:00
class ApcNiveau(db.Model, XMLModel):
id = db.Column(db.Integer, primary_key=True)
competence_id = db.Column(
db.Integer, db.ForeignKey("apc_competence.id"), nullable=False
)
libelle = db.Column(db.Text(), nullable=False)
annee = db.Column(db.Text(), nullable=False) # "BUT2"
# L'ordre est l'année d'apparition de ce niveau
ordre = db.Column(db.Integer, nullable=False) # 1, 2, 3
app_critiques = db.relationship(
"ApcAppCritique",
backref="niveau",
lazy="dynamic",
cascade="all, delete-orphan",
)
2021-12-02 17:36:32 +01:00
def to_dict(self):
return {
"libelle": self.libelle,
"annee": self.annee,
"ordre": self.ordre,
"app_critiques": {x.code: x.to_dict() for x in self.app_critiques},
}
2021-12-02 12:08:03 +01:00
class ApcAppCritique(db.Model, XMLModel):
"Apprentissage Critique BUT"
id = db.Column(db.Integer, primary_key=True)
niveau_id = db.Column(db.Integer, db.ForeignKey("apc_niveau.id"), nullable=False)
2021-12-03 11:03:33 +01:00
code = db.Column(db.Text(), nullable=False, index=True)
2021-12-02 12:08:03 +01:00
libelle = db.Column(db.Text())
modules = db.relationship(
"Module",
secondary="apc_modules_acs",
lazy="dynamic",
backref=db.backref("app_critiques", lazy="dynamic"),
)
def to_dict(self):
2021-12-03 15:44:57 +01:00
return {"libelle": self.libelle}
2021-12-02 12:08:03 +01:00
def get_label(self):
return self.code + " - " + self.titre
def __repr__(self):
2021-12-03 11:03:33 +01:00
return "<AppCritique {}>".format(self.code)
2021-12-02 12:08:03 +01:00
def get_saes(self):
"""Liste des SAE associées"""
return [m for m in self.modules if m.module_type == ModuleType.SAE]
2021-12-03 11:03:33 +01:00
ApcAppCritiqueModules = db.Table(
2021-12-02 12:08:03 +01:00
"apc_modules_acs",
db.Column("module_id", db.ForeignKey("notes_modules.id")),
db.Column("app_crit_id", db.ForeignKey("apc_app_critique.id")),
)
class ApcParcours(db.Model, XMLModel):
id = db.Column(db.Integer, primary_key=True)
referentiel_id = db.Column(
db.Integer, db.ForeignKey("apc_referentiel_competences.id"), nullable=False
)
numero = db.Column(db.Integer) # ordre de présentation
code = db.Column(db.Text(), nullable=False)
libelle = db.Column(db.Text(), nullable=False)
annees = db.relationship(
"ApcAnneeParcours",
backref="parcours",
lazy="dynamic",
cascade="all, delete-orphan",
)
2021-12-02 17:36:32 +01:00
def to_dict(self):
return {
"code": self.code,
"numero": self.numero,
"libelle": self.libelle,
"annees": {x.ordre: x.to_dict() for x in self.annees},
}
2021-12-02 12:08:03 +01:00
class ApcAnneeParcours(db.Model, XMLModel):
id = db.Column(db.Integer, primary_key=True)
parcours_id = db.Column(
db.Integer, db.ForeignKey("apc_parcours.id"), nullable=False
)
2021-12-02 17:36:32 +01:00
ordre = db.Column(db.Integer)
2021-12-02 12:08:03 +01:00
2021-12-02 17:36:32 +01:00
def to_dict(self):
return {
"ordre": self.ordre,
"competences": {
2021-12-03 15:44:57 +01:00
x.competence.titre: {"niveau": x.niveau}
2021-12-02 17:36:32 +01:00
for x in self.niveaux_competences
},
}
2021-12-02 12:08:03 +01:00
class ApcParcoursNiveauCompetence(db.Model):
"""Association entre année de parcours et compétence.
Le "niveau" de la compétence est donné ici
(convention Orébut)
"""
competence_id = db.Column(
db.Integer,
db.ForeignKey("apc_competence.id", ondelete="CASCADE"),
primary_key=True,
)
annee_parcours_id = db.Column(
db.Integer,
db.ForeignKey("apc_annee_parcours.id", ondelete="CASCADE"),
primary_key=True,
)
niveau = db.Column(db.Integer, nullable=False) # 1, 2, 3
competence = db.relationship(
ApcCompetence,
backref=db.backref(
"annee_parcours",
passive_deletes=True,
cascade="save-update, merge, delete, delete-orphan",
),
)
annee_parcours = db.relationship(
ApcAnneeParcours,
backref=db.backref(
2021-12-02 17:36:32 +01:00
"niveaux_competences",
2021-12-02 12:08:03 +01:00
passive_deletes=True,
cascade="save-update, merge, delete, delete-orphan",
),
)