From bd718d456d7aad06581916a7bf6b359bc5bd9e57 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sun, 31 Jul 2022 21:44:39 +0200 Subject: [PATCH] =?UTF-8?q?API:=20modif=20r=C3=A9sultat=20de=20/etudiant(s?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/etudiants.py | 96 ++------------------------------- app/models/etudiants.py | 11 ++++ tests/api/test_api_etudiants.py | 1 - tests/api/tools_test_api.py | 14 +---- 4 files changed, 18 insertions(+), 104 deletions(-) diff --git a/app/api/etudiants.py b/app/api/etudiants.py index c2bd40da..208eba11 100644 --- a/app/api/etudiants.py +++ b/app/api/etudiants.py @@ -71,34 +71,8 @@ def etudiants_courants(long=False): ... ] - En format "long": - { - "boursier": True, - "civilite": "F", - "code_ine": "AP987654", - "code_nip": "1234567", - "codepostaldomicile": "92800", - "date_naissance": "21/06/2000", - "dept_acronym": "CJ", - "dept_id": 1, - "dept_naissance": "092", - "description": "infos portail", - "domicile": "Plaza Condell", - "email": "jeanne.dupont@xxx.fr", - "emailperso": "", - "etudid": 4853, - "id": 4863, - "lieu_naissance": "SEVRES", - "nationalite": "", - "nom": "DUPONT", - "nomprenom": "Mme Jeanne Dupont", - "paysdomicile": "FRANCE", - "prenom": "JEANNE", - "telephone": "0102030405", - "telephonemobile": "", - "typeadresse": "domicile", - "villedomicile": "VALPARAISO", - } + En format "long": voir documentation. + """ allowed_depts = current_user.get_depts_with_permission(Permission.ScoView) etuds = Identite.query.filter( @@ -113,7 +87,7 @@ def etudiants_courants(long=False): or_(Departement.acronym == acronym for acronym in allowed_depts) ) if long: - data = [etud.to_dict_bul(include_urls=False) for etud in etuds] + data = [etud.to_dict_api() for etud in etuds] else: data = [etud.to_dict_short() for etud in etuds] return jsonify(data) @@ -138,34 +112,6 @@ def etudiant(etudid: int = None, nip: str = None, ine: str = None): Les codes INE et NIP sont uniques au sein d'un département. Si plusieurs objets ont le même code, on ramène le plus récemment inscrit. - - Exemple de résultat : - { - "civilite": "X", - "code_ine": "1", - "code_nip": "1", - "date_naissance": "", - "email": "SACHA.COSTA@example.com", - "emailperso": "", - "etudid": 1, - "nom": "COSTA", - "prenom": "SACHA", - "nomprenom": "Sacha COSTA", - "lieu_naissance": "", - "dept_naissance": "", - "nationalite": "", - "boursier": "", - "id": 1, - "codepostaldomicile": "", - "paysdomicile": "", - "telephonemobile": "", - "typeadresse": "domicile", - "domicile": "", - "villedomicile": "", - "telephone": "", - "fax": "", - "description": "" - } """ etud = tools.get_etud(etudid, nip, ine) @@ -175,7 +121,7 @@ def etudiant(etudid: int = None, nip: str = None, ine: str = None): message="étudiant inconnu", ) - return jsonify(etud.to_dict_bul(include_urls=False)) + return jsonify(etud.to_dict_api()) @bp.route("/etudiants/etudid/", methods=["GET"]) @@ -211,7 +157,7 @@ def etudiants(etudid: int = None, nip: str = None, ine: str = None): etuds = etuds.join(Departement).filter( or_(Departement.acronym == acronym for acronym in allowed_depts) ) - return jsonify([etud.to_dict_bul(include_urls=False) for etud in query]) + return jsonify([etud.to_dict_api() for etud in query]) @bp.route("/etudiant/etudid//formsemestres") @@ -231,38 +177,6 @@ def etudiant_formsemestres(etudid: int = None, nip: int = None, ine: int = None) différents (si l'étudiant a changé de département). L'id du département est `dept_id`. Si accès par département, ne retourne que les formsemestre suivis dans le département. - - Exemple de résultat : - [ - { - "block_moyennes": false, - "bul_bgcolor": "white", - "bul_hide_xml": false, - "date_debut_iso": "2021-09-01", - "date_debut": "01/09/2021", - "date_fin_iso": "2022-08-31", - "date_fin": "31/08/2022", - "dept_id": 1, - "elt_annee_apo": null, - "elt_sem_apo": null, - "ens_can_edit_eval": false, - "etat": true, - "formation_id": 1, - "formsemestre_id": 1, - "gestion_compensation": false, - "gestion_semestrielle": false, - "id": 1, - "modalite": "FI", - "resp_can_change_ens": true, - "resp_can_edit": false, - "responsables": [] - "scodoc7_id": null, - "semestre_id": 1, - "titre_num": "Semestre test semestre 1", - "titre": "Semestre test", - }, - ... - ] """ if etudid is not None: q_etud = Identite.query.filter_by(id=etudid) diff --git a/app/models/etudiants.py b/app/models/etudiants.py index 63f023f1..c4754f8e 100644 --- a/app/models/etudiants.py +++ b/app/models/etudiants.py @@ -207,6 +207,17 @@ class Identite(db.Model): d["id"] = self.id # a été écrasé par l'id de adresse return d + def to_dict_api(self) -> dict: + """Représentation dictionnaire pour export API, avec adresses et admission.""" + e = dict(self.__dict__) + e.pop("_sa_instance_state", None) + admission = self.admission.first() + e["admission"] = admission.to_dict() if admission is not None else None + e["adresses"] = [adr.to_dict() for adr in self.adresses] + e["dept_acronym"] = self.departement.acronym + e.pop("departement", None) + return e + def inscriptions(self) -> list["FormSemestreInscription"]: "Liste des inscriptions à des formsemestres, triée, la plus récente en tête" from app.models.formsemestre import FormSemestre, FormSemestreInscription diff --git a/tests/api/test_api_etudiants.py b/tests/api/test_api_etudiants.py index 190a4b25..b7ed4f47 100644 --- a/tests/api/test_api_etudiants.py +++ b/tests/api/test_api_etudiants.py @@ -138,7 +138,6 @@ def test_etudiant(api_headers): ) assert r.status_code == 200 etud_ine = r.json() - assert len(etud) == 26 fields_ok = verify_fields(etud, ETUD_FIELDS) assert fields_ok is True diff --git a/tests/api/tools_test_api.py b/tests/api/tools_test_api.py index 6ee686e6..794095fe 100644 --- a/tests/api/tools_test_api.py +++ b/tests/api/tools_test_api.py @@ -45,31 +45,21 @@ DEPARTEMENT_FIELDS = [ ] ETUD_FIELDS = { + "admission", + "adresses", "boursier", "civilite", "code_ine", "code_nip", - "codepostaldomicile", "date_naissance", "dept_acronym", "dept_id", "dept_naissance", - "description", - "domicile", - "email", - "emailperso", - "etudid", "id", "lieu_naissance", "nationalite", "nom", - "nomprenom", - "paysdomicile", "prenom", - "telephone", - "telephonemobile", - "typeadresse", - "villedomicile", } FORMATION_FIELDS = {