Nouvelle API: Groupes/Partitions: accès en lecture

This commit is contained in:
Emmanuel Viennet 2022-07-20 08:19:24 +02:00
parent b6de8f8631
commit 866da5b83d
2 changed files with 54 additions and 39 deletions

View File

@ -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/<int:formsemestre_id>", methods=["GET"])
@bp.route("/partition/<int:partition_id>", 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/<int:formsemestre_id>/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/<int:group_id>", methods=["GET"])
@bp.route("/partition/group/<int:group_id>/etat/<string:etat>", methods=["GET"])
@bp.route("/group/<int:group_id>/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,11 +85,17 @@ 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:
return jsonify([etud.to_dict_short() for etud in group.etuds])
@bp.route("/group/<int:group_id>/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)

View File

@ -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: