From ae2a56cad35b7476afdb10c55482653a12706c6d Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 10 Aug 2022 07:24:54 +0200 Subject: [PATCH] Fix API: /group//etudiants/query --- app/api/partitions.py | 16 +++++++++------- tests/api/test_api_partitions.py | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/api/partitions.py b/app/api/partitions.py index abef92e8c..a1229387b 100644 --- a/app/api/partitions.py +++ b/app/api/partitions.py @@ -136,20 +136,22 @@ def etud_in_group(group_id: int): def etud_in_group_query(group_id: int): """Etudiants du groupe, filtrés par état""" etat = request.args.get("etat") - if etat not in {scu.INSCRIT, scu.DEMISSION, scu.DEF}: + if etat not in {None, scu.INSCRIT, scu.DEMISSION, scu.DEF}: return json_error(404, "etat: valeur invalide") query = GroupDescr.query.filter_by(id=group_id) if g.scodoc_dept: query = ( query.join(Partition).join(FormSemestre).filter_by(dept_id=g.scodoc_dept_id) ) - group = query.first_or_404() # just tro ckeck that group exists in accessible dept - query = ( - Identite.query.join(FormSemestreInscription) - .filter_by(formsemestre_id=group.partition.formsemestre_id, etat=etat) - .join(group_membership) - .filter_by(group_id=group_id) + group = query.first_or_404() # just to ckeck that group exists in accessible dept + + query = Identite.query.join(FormSemestreInscription).filter_by( + formsemestre_id=group.partition.formsemestre_id ) + if etat is not None: + query = query.filter_by(etat=etat) + + query = query.join(group_membership).filter_by(group_id=group_id) return jsonify([etud.to_dict_short() for etud in query]) diff --git a/tests/api/test_api_partitions.py b/tests/api/test_api_partitions.py index f3999db85..d1138a576 100644 --- a/tests/api/test_api_partitions.py +++ b/tests/api/test_api_partitions.py @@ -134,10 +134,10 @@ def test_etud_in_group(api_headers): - /group//etudiants/query?etat= """ group_id = 1 - etudiants = GET(f"/group/{group_id}/etudiants", headers=api_headers) - assert isinstance(etudiants, list) + etuds = GET(f"/group/{group_id}/etudiants", headers=api_headers) + assert isinstance(etuds, list) - for etud in etudiants: + for etud in etuds: assert verify_fields(etud, PARTITION_GROUPS_ETUD_FIELDS) assert isinstance(etud["id"], int) assert isinstance(etud["dept_id"], int) @@ -148,12 +148,14 @@ def test_etud_in_group(api_headers): assert isinstance(etud["code_nip"], str) assert isinstance(etud["code_ine"], str) + # query sans filtre: + etuds_query = GET(f"/group/{group_id}/etudiants/query", headers=api_headers) + assert etuds_query == etuds + etat = "I" - etudiants = GET( - f"/group/{group_id}/etudiants/query?etat={etat}", headers=api_headers - ) - assert isinstance(etudiants, list) - for etud in etudiants: + etuds = GET(f"/group/{group_id}/etudiants/query?etat={etat}", headers=api_headers) + assert isinstance(etuds, list) + for etud in etuds: assert verify_fields(etud, PARTITION_GROUPS_ETUD_FIELDS) assert isinstance(etud["id"], int) assert isinstance(etud["dept_id"], int)