API: fix /formsemestre/<int:formsemestre_id>/programme

This commit is contained in:
Emmanuel Viennet 2022-07-21 14:21:06 +02:00
parent 8330ce66ca
commit 72e69960a4
7 changed files with 39 additions and 13 deletions

View File

@ -178,7 +178,7 @@ def dept_formsemestres_courants(acronym: str):
FormSemestre.date_fin >= app.db.func.now(), FormSemestre.date_fin >= app.db.func.now(),
) )
return jsonify([d.to_dict(convert_parcours=True) for d in formsemestres]) return jsonify([d.to_dict(convert_objects=True) for d in formsemestres])
@bp.route("/departement/id/<int:dept_id>/formsemestres_courants", methods=["GET"]) @bp.route("/departement/id/<int:dept_id>/formsemestres_courants", methods=["GET"])
@ -198,4 +198,4 @@ def dept_formsemestres_courants_by_id(dept_id: int):
FormSemestre.date_fin >= app.db.func.now(), FormSemestre.date_fin >= app.db.func.now(),
) )
return jsonify([d.to_dict(convert_parcours=True) for d in formsemestres]) return jsonify([d.to_dict(convert_objects=True) for d in formsemestres])

View File

@ -217,7 +217,7 @@ def etudiant_formsemestres(etudid: int = None, nip: int = None, ine: int = None)
formsemestres = query.order_by(FormSemestre.date_debut) formsemestres = query.order_by(FormSemestre.date_debut)
return jsonify( return jsonify(
[formsemestre.to_dict(convert_parcours=True) for formsemestre in formsemestres] [formsemestre.to_dict(convert_objects=True) for formsemestre in formsemestres]
) )

View File

@ -215,12 +215,12 @@ def formsemestre_programme(formsemestre_id: int):
ModuleType.STANDARD: [], ModuleType.STANDARD: [],
} }
for modimpl in formsemestre.modimpls_sorted: for modimpl in formsemestre.modimpls_sorted:
d = modimpl.to_dict() d = modimpl.to_dict(convert_objects=True)
m_list[modimpl.module.module_type].append(d) m_list[modimpl.module.module_type].append(d)
return jsonify( return jsonify(
{ {
"ues": [ue.to_dict() for ue in ues], "ues": [ue.to_dict(convert_objects=True) for ue in ues],
"ressources": m_list[ModuleType.RESSOURCE], "ressources": m_list[ModuleType.RESSOURCE],
"saes": m_list[ModuleType.SAE], "saes": m_list[ModuleType.SAE],
"modules": m_list[ModuleType.STANDARD], "modules": m_list[ModuleType.STANDARD],

View File

@ -146,8 +146,11 @@ class FormSemestre(db.Model):
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__name__} {self.id} {self.titre_num()}>" return f"<{self.__class__.__name__} {self.id} {self.titre_num()}>"
def to_dict(self, convert_parcours=False): def to_dict(self, convert_objects=False) -> dict:
"dict (compatible ScoDoc7)" """dict (compatible ScoDoc7).
If convert_objects, convert all attributes to native types
(suitable jor json encoding).
"""
d = dict(self.__dict__) d = dict(self.__dict__)
d.pop("_sa_instance_state", None) d.pop("_sa_instance_state", None)
# ScoDoc7 output_formators: (backward compat) # ScoDoc7 output_formators: (backward compat)
@ -165,7 +168,7 @@ class FormSemestre(db.Model):
d["date_fin"] = d["date_fin_iso"] = "" d["date_fin"] = d["date_fin_iso"] = ""
d["responsables"] = [u.id for u in self.responsables] d["responsables"] = [u.id for u in self.responsables]
d["titre_formation"] = self.titre_formation() d["titre_formation"] = self.titre_formation()
if convert_parcours: if convert_objects:
d["parcours"] = [p.to_dict() for p in self.parcours] d["parcours"] = [p.to_dict() for p in self.parcours]
return d return d

View File

@ -79,8 +79,11 @@ class ModuleImpl(db.Model):
self.module.formation.get_module_coefs(self.module.semestre_id), self.module.formation.get_module_coefs(self.module.semestre_id),
) )
def to_dict(self): def to_dict(self, convert_objects=False):
"""as a dict, with the same conversions as in ScoDoc7, including module""" """as a dict, with the same conversions as in ScoDoc7, including module.
If convert_objects, convert all attributes to native types
(suitable jor json encoding).
"""
e = dict(self.__dict__) e = dict(self.__dict__)
e.pop("_sa_instance_state", None) e.pop("_sa_instance_state", None)
# ScoDoc7 output_formators: (backward compat) # ScoDoc7 output_formators: (backward compat)
@ -88,7 +91,7 @@ class ModuleImpl(db.Model):
e["ens"] = [ e["ens"] = [
{"moduleimpl_id": self.id, "ens_id": e.id} for e in self.enseignants {"moduleimpl_id": self.id, "ens_id": e.id} for e in self.enseignants
] ]
e["module"] = self.module.to_dict() e["module"] = self.module.to_dict(convert_objects=convert_objects)
return e return e

View File

@ -67,9 +67,14 @@ class Module(db.Model):
def __repr__(self): def __repr__(self):
return f"<Module{ModuleType(self.module_type or ModuleType.STANDARD).name} id={self.id} code={self.code!r}>" return f"<Module{ModuleType(self.module_type or ModuleType.STANDARD).name} id={self.id} code={self.code!r}>"
def to_dict(self): def to_dict(self, convert_objects=False) -> dict:
"""If convert_objects, convert all attributes to native types
(suitable jor json encoding).
"""
e = dict(self.__dict__) e = dict(self.__dict__)
e.pop("_sa_instance_state", None) e.pop("_sa_instance_state", None)
if convert_objects:
e["parcours"] = [p.to_dict() for p in self.parcours]
# ScoDoc7 output_formators: (backward compat) # ScoDoc7 output_formators: (backward compat)
e["module_id"] = self.id e["module_id"] = self.id
e["heures_cours"] = 0.0 if self.heures_cours is None else self.heures_cours e["heures_cours"] = 0.0 if self.heures_cours is None else self.heures_cours
@ -220,6 +225,14 @@ class ModuleUECoef(db.Model):
), ),
) )
def to_dict(self, convert_objects=False) -> dict:
"""If convert_objects, convert all attributes to native types
(suitable jor json encoding).
"""
d = dict(self.__dict__)
d.pop("_sa_instance_state", None)
return d
class NotesTag(db.Model): class NotesTag(db.Model):
"""Tag sur un module""" """Tag sur un module"""

View File

@ -59,18 +59,25 @@ class UniteEns(db.Model):
self.semestre_idx} { self.semestre_idx} {
'EXTERNE' if self.is_external else ''})>""" 'EXTERNE' if self.is_external else ''})>"""
def to_dict(self): def to_dict(self, convert_objects=False):
"""as a dict, with the same conversions as in ScoDoc7 """as a dict, with the same conversions as in ScoDoc7
(except ECTS: keep None) (except ECTS: keep None)
If convert_objects, convert all attributes to native types
(suitable jor json encoding).
""" """
e = dict(self.__dict__) e = dict(self.__dict__)
e.pop("_sa_instance_state", None) e.pop("_sa_instance_state", None)
e.pop("evaluation_ue_poids", None)
# ScoDoc7 output_formators # ScoDoc7 output_formators
e["ue_id"] = self.id e["ue_id"] = self.id
e["numero"] = e["numero"] if e["numero"] else 0 e["numero"] = e["numero"] if e["numero"] else 0
e["ects"] = e["ects"] e["ects"] = e["ects"]
e["coefficient"] = e["coefficient"] if e["coefficient"] else 0.0 e["coefficient"] = e["coefficient"] if e["coefficient"] else 0.0
e["code_apogee"] = e["code_apogee"] or "" # pas de None e["code_apogee"] = e["code_apogee"] or "" # pas de None
if convert_objects:
e["module_ue_coefs"] = [
c.to_dict(convert_objects=True) for c in self.module_ue_coefs
]
return e return e
def is_locked(self): def is_locked(self):