From 8fa64476b6aa18f760cf2da25d28881767605338 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 10 Aug 2022 07:16:34 +0200 Subject: [PATCH] API: modif /formsemestre//etudiants[/query] --- app/api/formsemestres.py | 47 +++++++++++++----------------- tests/api/test_api_formsemestre.py | 12 ++++++-- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/app/api/formsemestres.py b/app/api/formsemestres.py index 7827d5839..ba09a2e4e 100644 --- a/app/api/formsemestres.py +++ b/app/api/formsemestres.py @@ -251,49 +251,44 @@ def formsemestre_programme(formsemestre_id: int): @bp.route( "/formsemestre//etudiants", - defaults={"etat": None}, + defaults={"with_query": False}, ) @bp.route( - "/formsemestre//etudiants/actifs", - defaults={"etat": scu.INSCRIT}, -) -@bp.route( - "/formsemestre//etudiants/demissionnaires", - defaults={"etat": scu.DEMISSION}, -) -@bp.route( - "/formsemestre//etudiants/defaillants", - defaults={"etat": scu.DEF}, + "/formsemestre//etudiants/query", + defaults={"with_query": True}, ) @api_web_bp.route( "/formsemestre//etudiants", - defaults={"etat": None}, + defaults={"with_query": False}, ) @api_web_bp.route( - "/formsemestre//etudiants/actifs", - defaults={"etat": scu.INSCRIT}, -) -@api_web_bp.route( - "/formsemestre//etudiants/demissionnaires", - defaults={"etat": scu.DEMISSION}, -) -@api_web_bp.route( - "/formsemestre//etudiants/defaillants", - defaults={"etat": scu.DEF}, + "/formsemestre//etudiants/query", + defaults={"with_query": True}, ) @login_required @scodoc @permission_required(Permission.ScoView) -def formsemestre_etudiants(formsemestre_id: int, etat: str = None): +def formsemestre_etudiants(formsemestre_id: int, with_query: bool = False): """Etudiants d'un formsemestre.""" query = FormSemestre.query.filter_by(id=formsemestre_id) if g.scodoc_dept: query = query.filter_by(dept_id=g.scodoc_dept_id) formsemestre: FormSemestre = query.first_or_404(formsemestre_id) - if etat is None: - inscriptions = formsemestre.inscriptions + if with_query: + etat = request.args.get("etat") + if etat is not None: + etat = { + "actifs": scu.INSCRIT, + "demissionnaires": scu.DEMISSION, + "defaillants": scu.DEF, + }.get(etat, etat) + inscriptions = [ + ins for ins in formsemestre.inscriptions if ins.etat == etat + ] + else: + inscriptions = formsemestre.inscriptions else: - inscriptions = [ins for ins in formsemestre.inscriptions if ins.etat == etat] + inscriptions = formsemestre.inscriptions etuds = [ins.etud.to_dict_short() for ins in inscriptions] # Ajout des groupes de chaque étudiants diff --git a/tests/api/test_api_formsemestre.py b/tests/api/test_api_formsemestre.py index 6ccad0289..f9d6e8629 100644 --- a/tests/api/test_api_formsemestre.py +++ b/tests/api/test_api_formsemestre.py @@ -515,22 +515,28 @@ def test_formsemestre_etudiants(api_headers): assert isinstance(group["group_id"], int) assert group["group_name"] is None or isinstance(group["group_name"], int) + ## Avec query: + etuds_query = GET( + f"/formsemestre/{formsemestre_id}/etudiants/query", headers=api_headers + ) + assert etuds_query == etuds + ### actifs etuds_actifs = GET( - f"/formsemestre/{formsemestre_id}/etudiants/actifs", headers=api_headers + f"/formsemestre/{formsemestre_id}/etudiants/query?etat=I", headers=api_headers ) assert isinstance(etuds_actifs, list) ### démissionnaires etuds_dem = GET( - f"/formsemestre/{formsemestre_id}/etudiants/demissionnaires", + f"/formsemestre/{formsemestre_id}/etudiants/query?etat=D", headers=api_headers, ) assert isinstance(etuds_dem, list) ### défaillants etuds_def = GET( - f"/formsemestre/{formsemestre_id}/etudiants/defaillants", headers=api_headers + f"/formsemestre/{formsemestre_id}/etudiants/query?etat=DEF", headers=api_headers ) assert isinstance(etuds_def, list)