From 866da5b83d111625691c46d7b7ef4c30dedd1e60 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 20 Jul 2022 08:19:24 +0200 Subject: [PATCH] =?UTF-8?q?Nouvelle=20API:=20Groupes/Partitions:=20acc?= =?UTF-8?q?=C3=A8s=20en=20lecture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/partitions.py | 90 +++++++++++++++++++++----------------- app/models/formsemestre.py | 3 ++ 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/app/api/partitions.py b/app/api/partitions.py index 78570199..bc534cf7 100644 --- a/app/api/partitions.py +++ b/app/api/partitions.py @@ -7,7 +7,7 @@ """ ScoDoc 9 API : partitions """ -from flask import jsonify +from flask import abort, jsonify, request from app.api import bp @@ -17,53 +17,59 @@ from app.models import FormSemestre, FormSemestreInscription, Identite from app.models import GroupDescr, Partition from app.models.groups import group_membership from app.scodoc.sco_permissions import Permission +from app.scodoc import sco_utils as scu -@bp.route("/partitions/", methods=["GET"]) +@bp.route("/partition/", methods=["GET"]) @token_auth.login_required @token_permission_required(Permission.APIView) -def partition(formsemestre_id: int): +def partition_info(partition_id: int): + """ + Exemple de résultat : + ``` + { + 'bul_show_rank': False, + 'formsemestre_id': 39, + 'groups': [ + {'id': 268, 'name': 'A', 'partition_id': 100}, + {'id': 269, 'name': 'B', 'partition_id': 100} + ], + 'groups_editable': True, + 'id': 100, + 'numero': 100, + 'partition_name': 'TD', + 'show_in_lists': True + } + ``` + """ + partition = Partition.query.get_or_404(partition_id) + return jsonify(partition.to_dict(with_groups=True)) + + +@bp.route("/formsemestre//partitions", methods=["GET"]) +@token_auth.login_required +@token_permission_required(Permission.APIView) +def formsemestre_partitions(formsemestre_id: int): """ Retourne la liste de toutes les partitions d'un formsemestre formsemestre_id : l'id d'un formsemestre - Exemple de résultat : - [ - { - "partition_id": 2, - "id": 2, - "formsemestre_id": 1, - "partition_name": "TD", - "numero": 1, - "bul_show_rank": false, - "show_in_lists": true - }, - { - "partition_id": 1, - "id": 1, - "formsemestre_id": 1, - "partition_name": null, - "numero": 0, - "bul_show_rank": false, - "show_in_lists": true - } - ] """ formsemestre: FormSemestre = FormSemestre.query.get_or_404(formsemestre_id) - return jsonify([partition.to_dict() for partition in formsemestre.partitions]) + return jsonify( + [partition.to_dict(with_groups=True) for partition in formsemestre.partitions] + ) -@bp.route("/partition/group/", methods=["GET"]) -@bp.route("/partition/group//etat/", methods=["GET"]) +@bp.route("/group//etudiants", methods=["GET"]) @token_auth.login_required @token_permission_required(Permission.APIView) -def etud_in_group(group_id: int, etat=None): +def etud_in_group(group_id: int): """ Retourne la liste des étudiants dans un groupe group_id : l'id d'un groupe - etat : état de l'inscription Exemple de résultat : [ @@ -79,16 +85,22 @@ def etud_in_group(group_id: int, etat=None): ... ] """ - # Fonction utilisée : app.scodoc.sco_groups.get_group_members() group = GroupDescr.query.get_or_404(group_id) - if etat is None: - query = group.etuds - else: - query = ( - Identite.query.join(FormSemestreInscription) - .filter_by(formsemestre_id=group.partition.formsemestre_id, etat=etat) - .join(group_membership) - .filter_by(group_id=group_id) - ) + return jsonify([etud.to_dict_short() for etud in group.etuds]) + + +@bp.route("/group//etudiants/query", methods=["GET"]) +def etud_in_group_query(group_id: int): + """Etudiants du groupe, filtrés par état""" + etat = request.get("etat", "") + if etat not in {scu.INSCRIT, scu.DEMISSION, scu.DEF}: + abort(404, "etat invalid") + group = GroupDescr.query.get_or_404(group_id) + query = ( + Identite.query.join(FormSemestreInscription) + .filter_by(formsemestre_id=group.partition.formsemestre_id, etat=etat) + .join(group_membership) + .filter_by(group_id=group_id) + ) return jsonify([etud.to_dict_short() for etud in query]) diff --git a/app/models/formsemestre.py b/app/models/formsemestre.py index 4887c7b1..b3ad6672 100644 --- a/app/models/formsemestre.py +++ b/app/models/formsemestre.py @@ -170,6 +170,7 @@ class FormSemestre(db.Model): """ d = dict(self.__dict__) d.pop("_sa_instance_state", None) + d["annee_scolaire"] = self.annee_scolaire_str() d["formsemestre_id"] = self.id d["titre_num"] = self.titre_num() if self.date_debut: @@ -184,6 +185,8 @@ class FormSemestre(db.Model): d["date_fin"] = d["date_fin_iso"] = "" d["responsables"] = [u.id for u in self.responsables] d["titre_court"] = self.formation.acronyme + d["parcours"] = [p.to_dict() for p in self.parcours] + d["session_id"] = self.session_id() return d def get_infos_dict(self) -> dict: