From 9b0dec8675d6cc190959be0677b6f518e8345a23 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 2 Aug 2022 17:13:13 +0200 Subject: [PATCH] Fixes #466 --- app/api/formsemestres.py | 5 ++--- app/models/moduleimpls.py | 4 ++++ app/models/modules.py | 5 +++++ app/models/ues.py | 13 ++++++++----- tests/api/README.md | 4 ++++ tests/api/test_api_formsemestre.py | 29 ++++++++++++++--------------- tests/api/test_api_partitions.py | 2 -- 7 files changed, 37 insertions(+), 25 deletions(-) diff --git a/app/api/formsemestres.py b/app/api/formsemestres.py index 88c157eb..e05a7ed3 100644 --- a/app/api/formsemestres.py +++ b/app/api/formsemestres.py @@ -347,8 +347,8 @@ def etat_evals(formsemestre_id: int): result = [] for modimpl_id in nt.modimpls_results: modimpl_results: ModuleImplResults = nt.modimpls_results[modimpl_id] - modimpl = ModuleImpl.query.get_or_404(modimpl_id) - modimpl_dict = modimpl.to_dict() + modimpl: ModuleImpl = ModuleImpl.query.get_or_404(modimpl_id) + modimpl_dict = modimpl.to_dict(convert_objects=True) list_eval = [] for evaluation_id in modimpl_results.evaluations_etat: @@ -403,7 +403,6 @@ def etat_evals(formsemestre_id: int): modimpl_dict["evaluations"] = list_eval result.append(modimpl_dict) - return jsonify(result) diff --git a/app/models/moduleimpls.py b/app/models/moduleimpls.py index 071ab97e..13b4f542 100644 --- a/app/models/moduleimpls.py +++ b/app/models/moduleimpls.py @@ -86,6 +86,10 @@ class ModuleImpl(db.Model): """ d = dict(self.__dict__) d.pop("_sa_instance_state", None) + if convert_objects: + # on n'exporte pas le formsemestre et les inscriptions + d.pop("formsemestre", None) + d.pop("inscriptions", None) # ScoDoc7 output_formators: (backward compat) d["moduleimpl_id"] = self.id d["ens"] = [ diff --git a/app/models/modules.py b/app/models/modules.py index 87cfe445..6cf23f9c 100644 --- a/app/models/modules.py +++ b/app/models/modules.py @@ -75,6 +75,9 @@ class Module(db.Model): d.pop("_sa_instance_state", None) if convert_objects: d["parcours"] = [p.to_dict() for p in self.parcours] + d["ue_coefs"] = [ + c.to_dict(convert_objects=convert_objects) for c in self.ue_coefs + ] if not with_matiere: d.pop("matiere", None) if not with_ue: @@ -240,6 +243,8 @@ class ModuleUECoef(db.Model): """ d = dict(self.__dict__) d.pop("_sa_instance_state", None) + if convert_objects: + d["ue"] = self.ue.to_dict(with_module_ue_coefs=False, convert_objects=True) return d diff --git a/app/models/ues.py b/app/models/ues.py index a806a732..dcc4aabb 100644 --- a/app/models/ues.py +++ b/app/models/ues.py @@ -59,7 +59,7 @@ class UniteEns(db.Model): self.semestre_idx} { 'EXTERNE' if self.is_external else ''})>""" - def to_dict(self, convert_objects=False): + def to_dict(self, convert_objects=False, with_module_ue_coefs=True): """as a dict, with the same conversions as in ScoDoc7 (except ECTS: keep None) If convert_objects, convert all attributes to native types @@ -74,10 +74,13 @@ class UniteEns(db.Model): 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 - ] + if with_module_ue_coefs: + if convert_objects: + e["module_ue_coefs"] = [ + c.to_dict(convert_objects=True) for c in self.module_ue_coefs + ] + else: + e.pop("module_ue_coefs", None) return e def is_locked(self): diff --git a/tests/api/README.md b/tests/api/README.md index d41c017b..c8ae7883 100644 --- a/tests/api/README.md +++ b/tests/api/README.md @@ -20,6 +20,10 @@ Démarche générale: flask init-test-database ``` + en plus court: ``` + tools/create_database.sh --drop SCODOC_TEST_API && flask db upgrade &&flask sco-db-init --erase && flask init-test-database + ``` + 2. On lance le serveur ScoDoc sur cette base ``` flask run --host 0.0.0.0 diff --git a/tests/api/test_api_formsemestre.py b/tests/api/test_api_formsemestre.py index c17b01fe..e0168b13 100644 --- a/tests/api/test_api_formsemestre.py +++ b/tests/api/test_api_formsemestre.py @@ -19,7 +19,13 @@ Utilisation : import requests -from tests.api.setup_test_api import API_URL, CHECK_CERTIFICATE, api_headers +from tests.api.setup_test_api import ( + API_URL, + CHECK_CERTIFICATE, + GET, + POST_JSON, + api_headers, +) from tests.api.tools_test_api import ( verify_fields, @@ -652,20 +658,13 @@ def test_formsemestre_programme(api_headers): assert verify_fields(sae, MODIMPL_FIELDS) -def test_etat_evals( - api_headers, -): +def test_etat_evals(api_headers): """ Route : /formsemestre//etat_evals """ - r = requests.get( - API_URL + "/formsemestre/1/etat_evals", - headers=api_headers, - verify=CHECK_CERTIFICATE, - ) - assert r.status_code == 200 - - etat_evals = r.json() + formsemestre_id = 1 + headers = api_headers + etat_evals = GET(f"/formsemestre/{formsemestre_id}/etat_evals", headers=headers) assert len(etat_evals) == 21 @@ -728,10 +727,10 @@ def test_etat_evals( ) ##### ERROR ##### - fake_eval_id = 153165161656849846516511321651651 + invalid_eval_id = 153165161656849846516511321651651 r = requests.get( - f"{API_URL}/formsemestre/{fake_eval_id}/etat_evals", - headers=api_headers, + f"{API_URL}/formsemestre/{invalid_eval_id}/etat_evals", + headers=headers, verify=CHECK_CERTIFICATE, ) assert r.status_code == 404 diff --git a/tests/api/test_api_partitions.py b/tests/api/test_api_partitions.py index bf9e6b39..0d1f7b11 100644 --- a/tests/api/test_api_partitions.py +++ b/tests/api/test_api_partitions.py @@ -17,8 +17,6 @@ Utilisation : pytest tests/api/test_api_partitions.py """ -import requests - from tests.api.setup_test_api import ( API_URL, CHECK_CERTIFICATE,