2021-12-08 22:33:32 +01:00
|
|
|
"""ScoDoc 9 models : Formations
|
2021-08-07 15:20:30 +02:00
|
|
|
"""
|
2022-05-29 17:34:03 +02:00
|
|
|
import flask_sqlalchemy
|
2021-08-07 15:20:30 +02:00
|
|
|
|
2022-01-22 11:34:57 +01:00
|
|
|
import app
|
2021-08-07 15:20:30 +02:00
|
|
|
from app import db
|
2021-12-16 16:27:35 +01:00
|
|
|
from app.comp import df_cache
|
2021-08-07 15:20:30 +02:00
|
|
|
from app.models import SHORT_STR_LEN
|
2022-05-29 17:34:03 +02:00
|
|
|
from app.models.but_refcomp import (
|
|
|
|
ApcAnneeParcours,
|
2022-06-09 07:39:58 +02:00
|
|
|
ApcCompetence,
|
2022-05-29 17:34:03 +02:00
|
|
|
ApcNiveau,
|
|
|
|
ApcParcours,
|
|
|
|
ApcParcoursNiveauCompetence,
|
|
|
|
)
|
2022-01-08 19:53:17 +01:00
|
|
|
from app.models.modules import Module
|
2022-06-24 03:34:52 +02:00
|
|
|
from app.models.moduleimpls import ModuleImpl
|
2022-01-08 20:07:13 +01:00
|
|
|
from app.models.ues import UniteEns
|
2021-12-16 16:27:35 +01:00
|
|
|
from app.scodoc import sco_cache
|
2021-11-14 18:09:20 +01:00
|
|
|
from app.scodoc import sco_codes_parcours
|
2021-12-16 16:27:35 +01:00
|
|
|
from app.scodoc import sco_utils as scu
|
2022-06-22 14:09:08 +02:00
|
|
|
from app.scodoc.sco_codes_parcours import UE_STANDARD
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
|
2021-11-12 22:17:46 +01:00
|
|
|
class Formation(db.Model):
|
2021-08-07 15:20:30 +02:00
|
|
|
"""Programme pédagogique d'une formation"""
|
|
|
|
|
|
|
|
__tablename__ = "notes_formations"
|
2021-09-19 21:31:35 +02:00
|
|
|
__table_args__ = (db.UniqueConstraint("dept_id", "acronyme", "titre", "version"),)
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
formation_id = db.synonym("id")
|
2021-08-13 00:34:58 +02:00
|
|
|
dept_id = db.Column(db.Integer, db.ForeignKey("departement.id"), index=True)
|
|
|
|
|
2021-08-15 22:33:09 +02:00
|
|
|
acronyme = db.Column(db.Text(), nullable=False)
|
2021-08-07 15:20:30 +02:00
|
|
|
titre = db.Column(db.Text(), nullable=False)
|
2021-08-08 16:01:10 +02:00
|
|
|
titre_officiel = db.Column(db.Text(), nullable=False)
|
2021-08-10 00:23:30 +02:00
|
|
|
version = db.Column(db.Integer, default=1, server_default="1")
|
2021-08-08 16:01:10 +02:00
|
|
|
formation_code = db.Column(
|
|
|
|
db.String(SHORT_STR_LEN),
|
|
|
|
server_default=db.text("notes_newid_fcod()"),
|
|
|
|
nullable=False,
|
|
|
|
)
|
|
|
|
# nb: la fonction SQL notes_newid_fcod doit être créée à part
|
2021-08-10 00:23:30 +02:00
|
|
|
type_parcours = db.Column(db.Integer, default=0, server_default="0")
|
2021-08-07 15:20:30 +02:00
|
|
|
code_specialite = db.Column(db.String(SHORT_STR_LEN))
|
|
|
|
|
2021-12-02 12:08:03 +01:00
|
|
|
# Optionnel, pour les formations type BUT
|
|
|
|
referentiel_competence_id = db.Column(
|
|
|
|
db.Integer, db.ForeignKey("apc_referentiel_competences.id")
|
|
|
|
)
|
2021-11-12 22:17:46 +01:00
|
|
|
ues = db.relationship("UniteEns", backref="formation", lazy="dynamic")
|
2021-08-13 00:34:58 +02:00
|
|
|
formsemestres = db.relationship("FormSemestre", lazy="dynamic", backref="formation")
|
2021-11-12 22:17:46 +01:00
|
|
|
ues = db.relationship("UniteEns", lazy="dynamic", backref="formation")
|
2021-11-17 10:28:51 +01:00
|
|
|
modules = db.relationship("Module", lazy="dynamic", backref="formation")
|
2021-08-13 00:34:58 +02:00
|
|
|
|
2021-09-26 11:28:13 +02:00
|
|
|
def __repr__(self):
|
2022-05-22 03:26:39 +02:00
|
|
|
return f"<{self.__class__.__name__}(id={self.id}, dept_id={self.dept_id}, acronyme='{self.acronyme!r}')>"
|
2021-09-26 11:28:13 +02:00
|
|
|
|
2022-06-26 09:37:50 +02:00
|
|
|
def to_html(self) -> str:
|
|
|
|
"titre complet pour affichage"
|
|
|
|
return f"""Formation {self.titre} ({self.acronyme}) [version {self.version}] code {self.formation_code}"""
|
|
|
|
|
2022-10-23 23:28:24 +02:00
|
|
|
def to_dict(self, with_refcomp_attrs=False):
|
|
|
|
""" "as a dict.
|
|
|
|
Si with_refcomp_attrs, ajoute attributs permettant de retrouver le ref. de comp.
|
|
|
|
"""
|
2021-11-30 10:54:12 +01:00
|
|
|
e = dict(self.__dict__)
|
|
|
|
e.pop("_sa_instance_state", None)
|
2022-12-19 00:56:17 +01:00
|
|
|
if "referentiel_competence" in e:
|
|
|
|
e.pop("referentiel_competence")
|
2022-07-27 16:03:14 +02:00
|
|
|
e["departement"] = self.departement.to_dict()
|
2022-10-23 23:28:24 +02:00
|
|
|
e["formation_id"] = self.id # ScoDoc7 backward compat
|
|
|
|
if with_refcomp_attrs and self.referentiel_competence:
|
|
|
|
e["refcomp_version_orebut"] = self.referentiel_competence.version_orebut
|
|
|
|
e["refcomp_specialite"] = self.referentiel_competence.specialite
|
|
|
|
e["refcomp_type_titre"] = self.referentiel_competence.type_titre
|
|
|
|
|
2021-11-30 10:54:12 +01:00
|
|
|
return e
|
|
|
|
|
2021-11-14 18:09:20 +01:00
|
|
|
def get_parcours(self):
|
2022-06-02 03:14:13 +02:00
|
|
|
"""get l'instance de TypeParcours de cette formation
|
|
|
|
(le TypeParcours définit le genre de formation, à ne pas confondre
|
|
|
|
avec les parcours du BUT).
|
|
|
|
"""
|
2021-11-14 18:09:20 +01:00
|
|
|
return sco_codes_parcours.get_parcours_from_code(self.type_parcours)
|
|
|
|
|
2022-03-14 00:01:08 +01:00
|
|
|
def get_titre_version(self) -> str:
|
|
|
|
"""Titre avec version"""
|
|
|
|
return f"{self.acronyme} {self.titre} v{self.version}"
|
|
|
|
|
2021-12-05 20:21:51 +01:00
|
|
|
def is_apc(self):
|
|
|
|
"True si formation APC avec SAE (BUT)"
|
|
|
|
return self.get_parcours().APC_SAE
|
|
|
|
|
2021-11-29 22:18:37 +01:00
|
|
|
def get_module_coefs(self, semestre_idx: int = None):
|
|
|
|
"""Les coefs des modules vers les UE (accès via cache)"""
|
|
|
|
from app.comp import moy_ue
|
|
|
|
|
|
|
|
if semestre_idx is None:
|
|
|
|
key = f"{self.id}"
|
|
|
|
else:
|
|
|
|
key = f"{self.id}.{semestre_idx}"
|
|
|
|
|
|
|
|
modules_coefficients = df_cache.ModuleCoefsCache.get(key)
|
|
|
|
if modules_coefficients is None:
|
|
|
|
modules_coefficients, _, _ = moy_ue.df_load_module_coefs(
|
|
|
|
self.id, semestre_idx
|
|
|
|
)
|
|
|
|
df_cache.ModuleCoefsCache.set(key, modules_coefficients)
|
|
|
|
return modules_coefficients
|
|
|
|
|
2021-12-16 16:27:35 +01:00
|
|
|
def has_locked_sems(self):
|
|
|
|
"True if there is a locked formsemestre in this formation"
|
|
|
|
return len(self.formsemestres.filter_by(etat=False).all()) > 0
|
|
|
|
|
2021-11-29 22:18:37 +01:00
|
|
|
def invalidate_module_coefs(self, semestre_idx: int = None):
|
2022-07-13 18:52:07 +02:00
|
|
|
"""Invalide le cache des coefficients de modules.
|
|
|
|
Si semestre_idx est None, invalide les coefs de tous les semestres,
|
2021-11-29 22:18:37 +01:00
|
|
|
sinon invalide le semestre indiqué et le cache de la formation.
|
2022-07-13 18:52:07 +02:00
|
|
|
|
|
|
|
Dans tous les cas, invalide tous les formsemestres.
|
2021-11-29 22:18:37 +01:00
|
|
|
"""
|
|
|
|
if semestre_idx is None:
|
|
|
|
keys = {f"{self.id}.{m.semestre_id}" for m in self.modules}
|
|
|
|
else:
|
|
|
|
keys = f"{self.id}.{semestre_idx}"
|
|
|
|
df_cache.ModuleCoefsCache.delete_many(keys | {f"{self.id}"})
|
2022-06-24 03:34:52 +02:00
|
|
|
# Invalidate aussi les poids de toutes les évals de la formation
|
|
|
|
for modimpl in ModuleImpl.query.filter(
|
|
|
|
ModuleImpl.module_id == Module.id,
|
|
|
|
Module.formation_id == self.id,
|
|
|
|
):
|
|
|
|
modimpl.invalidate_evaluations_poids()
|
|
|
|
|
2021-12-16 16:27:35 +01:00
|
|
|
sco_cache.invalidate_formsemestre()
|
|
|
|
|
|
|
|
def invalidate_cached_sems(self):
|
|
|
|
for sem in self.formsemestres:
|
|
|
|
sco_cache.invalidate_formsemestre(formsemestre_id=sem.id)
|
|
|
|
|
2022-01-08 14:01:16 +01:00
|
|
|
def sanitize_old_formation(self) -> None:
|
2021-12-16 16:27:35 +01:00
|
|
|
"""
|
2022-01-08 14:01:16 +01:00
|
|
|
Corrige si nécessaire certains champs issus d'anciennes versions de ScoDoc:
|
2022-09-08 01:20:04 +02:00
|
|
|
Pour les formations APC (BUT) seulement:
|
2022-01-08 14:01:16 +01:00
|
|
|
- affecte à chaque module de cette formation le semestre de son UE de rattachement,
|
2021-12-16 16:27:35 +01:00
|
|
|
si elle en a une.
|
2022-01-08 14:01:16 +01:00
|
|
|
- si le module_type n'est pas renseigné, le met à STANDARD.
|
2022-09-08 01:20:04 +02:00
|
|
|
- on n'utilise pas de matières: réaffecte tous les modules
|
|
|
|
à la première matière de leur UE.
|
2022-01-08 14:01:16 +01:00
|
|
|
|
2022-09-08 01:20:04 +02:00
|
|
|
Devrait être appelé lorsqu'on change le type de formation vers le BUT,
|
|
|
|
et aussi lorsqu'on change le semestre d'une UE BUT.
|
2021-12-16 16:27:35 +01:00
|
|
|
Utile pour la migration des anciennes formations vers le BUT.
|
2022-01-08 14:01:16 +01:00
|
|
|
|
|
|
|
En cas de changement, invalide les caches coefs/poids.
|
2021-12-16 16:27:35 +01:00
|
|
|
"""
|
|
|
|
if not self.is_apc():
|
|
|
|
return
|
|
|
|
change = False
|
|
|
|
for mod in self.modules:
|
2022-01-08 19:53:17 +01:00
|
|
|
# --- Indices de semestres:
|
2021-12-16 16:27:35 +01:00
|
|
|
if (
|
|
|
|
mod.ue.semestre_idx is not None
|
|
|
|
and mod.ue.semestre_idx > 0
|
|
|
|
and mod.semestre_id != mod.ue.semestre_idx
|
|
|
|
):
|
|
|
|
mod.semestre_id = mod.ue.semestre_idx
|
|
|
|
db.session.add(mod)
|
|
|
|
change = True
|
2022-01-08 19:53:17 +01:00
|
|
|
# --- Types de modules
|
2022-01-08 14:01:16 +01:00
|
|
|
if mod.module_type is None:
|
|
|
|
mod.module_type = scu.ModuleType.STANDARD
|
|
|
|
db.session.add(mod)
|
|
|
|
change = True
|
2022-01-08 19:53:17 +01:00
|
|
|
# --- Numéros de modules
|
2022-01-08 20:07:13 +01:00
|
|
|
if Module.query.filter_by(formation_id=self.id, numero=None).count() > 0:
|
2022-01-08 19:53:17 +01:00
|
|
|
scu.objects_renumber(db, self.modules.all())
|
2022-09-08 01:20:04 +02:00
|
|
|
|
2022-01-08 20:07:13 +01:00
|
|
|
# --- Types d'UE (avant de rendre le type non nullable)
|
|
|
|
ues_sans_type = UniteEns.query.filter_by(formation_id=self.id, type=None)
|
|
|
|
if ues_sans_type.count() > 0:
|
|
|
|
for ue in ues_sans_type:
|
|
|
|
ue.type = 0
|
|
|
|
db.session.add(ue)
|
2022-01-08 19:53:17 +01:00
|
|
|
|
2022-09-08 01:20:04 +02:00
|
|
|
# --- Réaffectation des matières:
|
|
|
|
for ue in self.ues:
|
|
|
|
mat = ue.matieres.first()
|
|
|
|
if mat is None:
|
|
|
|
mat = Matiere()
|
|
|
|
ue.matieres.append(mat)
|
|
|
|
db.session.add(mat)
|
|
|
|
for module in ue.modules:
|
|
|
|
if module.matiere_id != mat.id:
|
|
|
|
module.matiere = mat
|
|
|
|
db.session.add(module)
|
|
|
|
change = True
|
|
|
|
|
2021-12-16 16:27:35 +01:00
|
|
|
db.session.commit()
|
2022-04-16 15:34:40 +02:00
|
|
|
if change:
|
|
|
|
app.clear_scodoc_cache()
|
2021-11-29 22:18:37 +01:00
|
|
|
|
2022-05-30 17:23:45 +02:00
|
|
|
def query_ues_parcour(self, parcour: ApcParcours) -> flask_sqlalchemy.BaseQuery:
|
2022-05-29 17:34:03 +02:00
|
|
|
"""Les UEs d'un parcours de la formation.
|
2022-12-05 21:59:10 +01:00
|
|
|
Si parcour est None, les UE sans parcours.
|
2022-05-29 17:34:03 +02:00
|
|
|
Exemple: pour avoir les UE du semestre 3, faire
|
|
|
|
`formation.query_ues_parcour(parcour).filter_by(semestre_idx=3)`
|
|
|
|
"""
|
2022-12-05 21:59:10 +01:00
|
|
|
if parcour is None:
|
|
|
|
return UniteEns.query.filter_by(
|
|
|
|
formation=self, type=UE_STANDARD, parcour_id=None
|
|
|
|
)
|
|
|
|
return UniteEns.query.filter_by(formation=self, type=UE_STANDARD).filter(
|
2022-10-30 16:07:06 +01:00
|
|
|
UniteEns.niveau_competence_id == ApcNiveau.id,
|
|
|
|
(UniteEns.parcour_id == parcour.id) | (UniteEns.parcour_id == None),
|
2022-05-29 17:34:03 +02:00
|
|
|
ApcParcoursNiveauCompetence.competence_id == ApcNiveau.competence_id,
|
|
|
|
ApcParcoursNiveauCompetence.annee_parcours_id == ApcAnneeParcours.id,
|
|
|
|
ApcAnneeParcours.parcours_id == parcour.id,
|
|
|
|
)
|
|
|
|
|
2022-06-09 07:39:58 +02:00
|
|
|
def query_competences_parcour(
|
|
|
|
self, parcour: ApcParcours
|
|
|
|
) -> flask_sqlalchemy.BaseQuery:
|
|
|
|
"""Les ApcCompetences d'un parcours de la formation.
|
|
|
|
None si pas de référentiel de compétences.
|
|
|
|
"""
|
|
|
|
if self.referentiel_competence_id is None:
|
|
|
|
return None
|
|
|
|
return (
|
|
|
|
ApcCompetence.query.filter_by(referentiel_id=self.referentiel_competence_id)
|
|
|
|
.join(
|
|
|
|
ApcParcoursNiveauCompetence,
|
|
|
|
ApcParcoursNiveauCompetence.competence_id == ApcCompetence.id,
|
|
|
|
)
|
|
|
|
.join(
|
|
|
|
ApcAnneeParcours,
|
|
|
|
ApcParcoursNiveauCompetence.annee_parcours_id == ApcAnneeParcours.id,
|
|
|
|
)
|
|
|
|
.filter(ApcAnneeParcours.parcours_id == parcour.id)
|
|
|
|
)
|
|
|
|
|
2022-10-29 08:22:17 +02:00
|
|
|
def refcomp_desassoc(self):
|
|
|
|
"""Désassocie la formation de son ref. de compétence"""
|
|
|
|
self.referentiel_competence = None
|
|
|
|
db.session.add(self)
|
|
|
|
# Niveaux des UE
|
|
|
|
for ue in self.ues:
|
|
|
|
ue.niveau_competence = None
|
|
|
|
db.session.add(ue)
|
|
|
|
# Parcours et AC des modules
|
|
|
|
for mod in self.modules:
|
|
|
|
mod.parcours = []
|
|
|
|
mod.app_critiques = []
|
|
|
|
db.session.add(mod)
|
|
|
|
db.session.commit()
|
|
|
|
|
2021-08-07 15:20:30 +02:00
|
|
|
|
2021-11-12 22:17:46 +01:00
|
|
|
class Matiere(db.Model):
|
2021-08-07 15:20:30 +02:00
|
|
|
"""Matières: regroupe les modules d'une UE
|
|
|
|
La matière a peu d'utilité en dehors de la présentation des modules
|
|
|
|
d'une UE.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__tablename__ = "notes_matieres"
|
|
|
|
__table_args__ = (db.UniqueConstraint("ue_id", "titre"),)
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
matiere_id = db.synonym("id")
|
|
|
|
ue_id = db.Column(db.Integer, db.ForeignKey("notes_ue.id"))
|
|
|
|
titre = db.Column(db.Text())
|
|
|
|
numero = db.Column(db.Integer) # ordre de présentation
|
|
|
|
|
2021-11-12 22:17:46 +01:00
|
|
|
modules = db.relationship("Module", lazy="dynamic", backref="matiere")
|
2022-01-29 22:45:39 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"""<{self.__class__.__name__}(id={self.id}, ue_id={
|
2022-05-22 03:26:39 +02:00
|
|
|
self.ue_id}, titre='{self.titre!r}')>"""
|
2022-01-29 22:45:39 +01:00
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
"""as a dict, with the same conversions as in ScoDoc7"""
|
|
|
|
e = dict(self.__dict__)
|
|
|
|
e.pop("_sa_instance_state", None)
|
|
|
|
# ScoDoc7 output_formators
|
|
|
|
e["ue_id"] = self.id
|
|
|
|
e["numero"] = e["numero"] if e["numero"] else 0
|
|
|
|
return e
|