From d36a2950d38cafbf1d2cd1d758e569011e771a77 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 21 Jul 2022 14:44:19 +0200 Subject: [PATCH] API: fix /formsemestre//programme --- app/api/formsemestres.py | 1 - app/models/moduleimpls.py | 17 ++++++++++------- app/models/modules.py | 37 +++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/app/api/formsemestres.py b/app/api/formsemestres.py index 603b694c9..bc43431e8 100644 --- a/app/api/formsemestres.py +++ b/app/api/formsemestres.py @@ -217,7 +217,6 @@ def formsemestre_programme(formsemestre_id: int): for modimpl in formsemestre.modimpls_sorted: d = modimpl.to_dict(convert_objects=True) m_list[modimpl.module.module_type].append(d) - return jsonify( { "ues": [ue.to_dict(convert_objects=True) for ue in ues], diff --git a/app/models/moduleimpls.py b/app/models/moduleimpls.py index 20dd0c36e..071ab97ea 100644 --- a/app/models/moduleimpls.py +++ b/app/models/moduleimpls.py @@ -79,20 +79,23 @@ class ModuleImpl(db.Model): self.module.formation.get_module_coefs(self.module.semestre_id), ) - def to_dict(self, convert_objects=False): + def to_dict(self, convert_objects=False, with_module=True): """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) + d = dict(self.__dict__) + d.pop("_sa_instance_state", None) # ScoDoc7 output_formators: (backward compat) - e["moduleimpl_id"] = self.id - e["ens"] = [ + d["moduleimpl_id"] = self.id + d["ens"] = [ {"moduleimpl_id": self.id, "ens_id": e.id} for e in self.enseignants ] - e["module"] = self.module.to_dict(convert_objects=convert_objects) - return e + if with_module: + d["module"] = self.module.to_dict(convert_objects=convert_objects) + else: + d.pop("module", None) + return d # Enseignants (chargés de TD ou TP) d'un moduleimpl diff --git a/app/models/modules.py b/app/models/modules.py index 91e25ba88..87cfe4450 100644 --- a/app/models/modules.py +++ b/app/models/modules.py @@ -67,24 +67,33 @@ class Module(db.Model): def __repr__(self): return f"" - def to_dict(self, convert_objects=False) -> dict: + def to_dict(self, convert_objects=False, with_matiere=False, with_ue=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) + d = dict(self.__dict__) + d.pop("_sa_instance_state", None) if convert_objects: - e["parcours"] = [p.to_dict() for p in self.parcours] + d["parcours"] = [p.to_dict() for p in self.parcours] + if not with_matiere: + d.pop("matiere", None) + if not with_ue: + d.pop("ue", None) + if convert_objects and with_matiere: + d["matiere"] = self.matiere.to_dict(convert_objects=True) + if convert_objects and with_ue: + d["ue"] = self.ue.to_dict(convert_objects=True) + # 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 - e["heures_td"] = 0.0 if self.heures_td is None else self.heures_td - e["heures_tp"] = 0.0 if self.heures_tp is None else self.heures_tp - e["numero"] = 0 if self.numero is None else self.numero - e["coefficient"] = 0.0 if self.coefficient is None else self.coefficient - e["module_type"] = 0 if self.module_type is None else self.module_type - e["code_apogee"] = e["code_apogee"] or "" # pas de None - return e + d["module_id"] = self.id + d["heures_cours"] = 0.0 if self.heures_cours is None else self.heures_cours + d["heures_td"] = 0.0 if self.heures_td is None else self.heures_td + d["heures_tp"] = 0.0 if self.heures_tp is None else self.heures_tp + d["numero"] = 0 if self.numero is None else self.numero + d["coefficient"] = 0.0 if self.coefficient is None else self.coefficient + d["module_type"] = 0 if self.module_type is None else self.module_type + d["code_apogee"] = d["code_apogee"] or "" # pas de None + return d def is_apc(self): "True si module SAÉ ou Ressource" @@ -227,7 +236,7 @@ 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). + (suitable for json encoding). """ d = dict(self.__dict__) d.pop("_sa_instance_state", None)