From 72e69960a431bdc9beb506ce4bb5c9dddb701a40 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 21 Jul 2022 14:21:06 +0200 Subject: [PATCH] API: fix /formsemestre//programme --- app/api/departements.py | 4 ++-- app/api/etudiants.py | 2 +- app/api/formsemestres.py | 4 ++-- app/models/formsemestre.py | 9 ++++++--- app/models/moduleimpls.py | 9 ++++++--- app/models/modules.py | 15 ++++++++++++++- app/models/ues.py | 9 ++++++++- 7 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app/api/departements.py b/app/api/departements.py index 4721a8063..16ad902a2 100644 --- a/app/api/departements.py +++ b/app/api/departements.py @@ -178,7 +178,7 @@ def dept_formsemestres_courants(acronym: str): 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//formsemestres_courants", methods=["GET"]) @@ -198,4 +198,4 @@ def dept_formsemestres_courants_by_id(dept_id: int): 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]) diff --git a/app/api/etudiants.py b/app/api/etudiants.py index bb4805dee..2979ac732 100644 --- a/app/api/etudiants.py +++ b/app/api/etudiants.py @@ -217,7 +217,7 @@ def etudiant_formsemestres(etudid: int = None, nip: int = None, ine: int = None) formsemestres = query.order_by(FormSemestre.date_debut) return jsonify( - [formsemestre.to_dict(convert_parcours=True) for formsemestre in formsemestres] + [formsemestre.to_dict(convert_objects=True) for formsemestre in formsemestres] ) diff --git a/app/api/formsemestres.py b/app/api/formsemestres.py index 8ae9967e1..603b694c9 100644 --- a/app/api/formsemestres.py +++ b/app/api/formsemestres.py @@ -215,12 +215,12 @@ def formsemestre_programme(formsemestre_id: int): ModuleType.STANDARD: [], } 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) 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], "saes": m_list[ModuleType.SAE], "modules": m_list[ModuleType.STANDARD], diff --git a/app/models/formsemestre.py b/app/models/formsemestre.py index 4016986f9..9ff939d4c 100644 --- a/app/models/formsemestre.py +++ b/app/models/formsemestre.py @@ -146,8 +146,11 @@ class FormSemestre(db.Model): def __repr__(self): return f"<{self.__class__.__name__} {self.id} {self.titre_num()}>" - def to_dict(self, convert_parcours=False): - "dict (compatible ScoDoc7)" + def to_dict(self, convert_objects=False) -> dict: + """dict (compatible ScoDoc7). + If convert_objects, convert all attributes to native types + (suitable jor json encoding). + """ d = dict(self.__dict__) d.pop("_sa_instance_state", None) # ScoDoc7 output_formators: (backward compat) @@ -165,7 +168,7 @@ class FormSemestre(db.Model): d["date_fin"] = d["date_fin_iso"] = "" d["responsables"] = [u.id for u in self.responsables] d["titre_formation"] = self.titre_formation() - if convert_parcours: + if convert_objects: d["parcours"] = [p.to_dict() for p in self.parcours] return d diff --git a/app/models/moduleimpls.py b/app/models/moduleimpls.py index 7574ed7e8..20dd0c36e 100644 --- a/app/models/moduleimpls.py +++ b/app/models/moduleimpls.py @@ -79,8 +79,11 @@ class ModuleImpl(db.Model): self.module.formation.get_module_coefs(self.module.semestre_id), ) - def to_dict(self): - """as a dict, with the same conversions as in ScoDoc7, including module""" + def to_dict(self, convert_objects=False): + """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.pop("_sa_instance_state", None) # ScoDoc7 output_formators: (backward compat) @@ -88,7 +91,7 @@ class ModuleImpl(db.Model): e["ens"] = [ {"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 diff --git a/app/models/modules.py b/app/models/modules.py index b36557722..91e25ba88 100644 --- a/app/models/modules.py +++ b/app/models/modules.py @@ -67,9 +67,14 @@ class Module(db.Model): def __repr__(self): return f"" - 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.pop("_sa_instance_state", None) + if convert_objects: + e["parcours"] = [p.to_dict() for p in self.parcours] # ScoDoc7 output_formators: (backward compat) e["module_id"] = self.id 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): """Tag sur un module""" diff --git a/app/models/ues.py b/app/models/ues.py index 01579dc14..a806a7320 100644 --- a/app/models/ues.py +++ b/app/models/ues.py @@ -59,18 +59,25 @@ class UniteEns(db.Model): self.semestre_idx} { '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 (except ECTS: keep None) + If convert_objects, convert all attributes to native types + (suitable jor json encoding). """ e = dict(self.__dict__) e.pop("_sa_instance_state", None) + e.pop("evaluation_ue_poids", None) # ScoDoc7 output_formators e["ue_id"] = self.id e["numero"] = e["numero"] if e["numero"] else 0 e["ects"] = e["ects"] e["coefficient"] = e["coefficient"] if e["coefficient"] else 0.0 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 def is_locked(self):