1
0
forked from ScoDoc/ScoDoc

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 ScoDoc 9 API : partitions
""" """
from flask import jsonify from flask import abort, jsonify, request
from app.api import bp 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 import GroupDescr, Partition
from app.models.groups import group_membership from app.models.groups import group_membership
from app.scodoc.sco_permissions import Permission 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_auth.login_required
@token_permission_required(Permission.APIView) @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 Retourne la liste de toutes les partitions d'un formsemestre
formsemestre_id : l'id 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) 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("/group/<int:group_id>/etudiants", methods=["GET"])
@bp.route("/partition/group/<int:group_id>/etat/<string:etat>", methods=["GET"])
@token_auth.login_required @token_auth.login_required
@token_permission_required(Permission.APIView) @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 Retourne la liste des étudiants dans un groupe
group_id : l'id d'un groupe group_id : l'id d'un groupe
etat : état de l'inscription
Exemple de résultat : 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) group = GroupDescr.query.get_or_404(group_id)
if etat is None: return jsonify([etud.to_dict_short() for etud in group.etuds])
query = group.etuds
else:
query = ( @bp.route("/group/<int:group_id>/etudiants/query", methods=["GET"])
Identite.query.join(FormSemestreInscription) def etud_in_group_query(group_id: int):
.filter_by(formsemestre_id=group.partition.formsemestre_id, etat=etat) """Etudiants du groupe, filtrés par état"""
.join(group_membership) etat = request.get("etat", "")
.filter_by(group_id=group_id) 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]) return jsonify([etud.to_dict_short() for etud in query])

View File

@ -170,6 +170,7 @@ class FormSemestre(db.Model):
""" """
d = dict(self.__dict__) d = dict(self.__dict__)
d.pop("_sa_instance_state", None) d.pop("_sa_instance_state", None)
d["annee_scolaire"] = self.annee_scolaire_str()
d["formsemestre_id"] = self.id d["formsemestre_id"] = self.id
d["titre_num"] = self.titre_num() d["titre_num"] = self.titre_num()
if self.date_debut: if self.date_debut:
@ -184,6 +185,8 @@ class FormSemestre(db.Model):
d["date_fin"] = d["date_fin_iso"] = "" d["date_fin"] = d["date_fin_iso"] = ""
d["responsables"] = [u.id for u in self.responsables] d["responsables"] = [u.id for u in self.responsables]
d["titre_court"] = self.formation.acronyme d["titre_court"] = self.formation.acronyme
d["parcours"] = [p.to_dict() for p in self.parcours]
d["session_id"] = self.session_id()
return d return d
def get_infos_dict(self) -> dict: def get_infos_dict(self) -> dict: