2021-12-08 22:33:32 +01:00
|
|
|
"""ScoDoc 9 models : Formations
|
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-01-08 19:53:17 +01:00
|
|
|
from app.models.modules import Module
|
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
|
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):
|
|
|
|
return f"<{self.__class__.__name__}(id={self.id}, dept_id={self.dept_id}, acronyme='{self.acronyme}')>"
|
|
|
|
|
2021-11-30 10:54:12 +01:00
|
|
|
def to_dict(self):
|
|
|
|
e = dict(self.__dict__)
|
|
|
|
e.pop("_sa_instance_state", None)
|
|
|
|
# ScoDoc7 output_formators: (backward compat)
|
|
|
|
e["formation_id"] = self.id
|
|
|
|
return e
|
|
|
|
|
2021-11-14 18:09:20 +01:00
|
|
|
def get_parcours(self):
|
|
|
|
"""get l'instance de TypeParcours de cette formation"""
|
|
|
|
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):
|
|
|
|
"""Invalide les coefficients de modules cachés.
|
|
|
|
Si semestre_idx est None, invalide tous les semestres,
|
|
|
|
sinon invalide le semestre indiqué et le cache de la formation.
|
|
|
|
"""
|
|
|
|
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}"})
|
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:
|
|
|
|
- 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.
|
|
|
|
|
2021-12-16 16:27:35 +01: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.
|
|
|
|
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-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
|
|
|
|
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
|
|
|
|
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={
|
|
|
|
self.ue_id}, titre='{self.titre}')>"""
|
|
|
|
|
|
|
|
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
|