DocScoDoc/app/api/absences.py

175 lines
4.9 KiB
Python
Raw Normal View History

#################################################### Absences #########################################################
from flask import jsonify
from app.api import bp
from app.api.errors import error_response
2022-05-03 13:35:17 +02:00
from app.api.auth import token_auth, token_permission_required
2022-05-11 00:59:51 +02:00
from app.models import Identite
2022-04-27 05:28:08 +02:00
2022-05-11 00:59:51 +02:00
from app.scodoc import notesdb as ndb
2022-04-27 05:19:13 +02:00
from app.scodoc import sco_abs
2022-05-16 15:15:33 +02:00
from app.scodoc.sco_groups import get_group_members
2022-03-04 17:16:08 +01:00
from app.scodoc.sco_permissions import Permission
@bp.route("/absences/etudid/<int:etudid>", methods=["GET"])
2022-05-03 13:35:17 +02:00
@token_auth.login_required
2022-04-14 14:56:36 +02:00
@token_permission_required(Permission.APIView)
2022-05-11 00:59:51 +02:00
def absences(etudid: int = None):
"""
Retourne la liste des absences d'un étudiant donné
etudid : l'etudid d'un étudiant
Exemple de résultat:
2022-05-11 00:59:51 +02:00
[
{
"jour": "2022-04-15",
"matin": true,
"estabs": true,
"estjust": true,
"description": "",
"begin": "2022-04-15 08:00:00",
"end": "2022-04-15 11:59:59"
},
{
"jour": "2022-04-15",
"matin": false,
"estabs": true,
"estjust": false,
"description": "",
"begin": "2022-04-15 12:00:00",
"end": "2022-04-15 17:59:59"
}
]
"""
2022-05-11 00:59:51 +02:00
etud = Identite.query.get(etudid)
if etud is None:
return error_response(
404,
message="id de l'étudiant (etudid, nip, ine) inconnu",
)
# Absences de l'étudiant
2022-04-27 05:19:13 +02:00
ndb.open_db_connection()
2022-05-11 00:59:51 +02:00
absences = sco_abs.list_abs_date(etud.id)
2022-04-27 05:19:13 +02:00
for absence in absences:
absence["jour"] = absence["jour"].isoformat()
return jsonify(absences)
2022-04-27 05:19:13 +02:00
@bp.route("/absences/etudid/<int:etudid>/just", methods=["GET"])
2022-05-03 13:35:17 +02:00
@token_auth.login_required
2022-04-14 14:56:36 +02:00
@token_permission_required(Permission.APIView)
2022-05-11 00:59:51 +02:00
def absences_just(etudid: int = None):
"""
Retourne la liste des absences justifiées d'un étudiant donné
etudid : l'etudid d'un étudiant
nip: le code nip d'un étudiant
ine : le code ine d'un étudiant
Exemple de résultat :
2022-05-11 00:59:51 +02:00
[
{
"jour": "2022-04-15",
"matin": true,
"estabs": true,
"estjust": true,
"description": "",
"begin": "2022-04-15 08:00:00",
"end": "2022-04-15 11:59:59"
},
{
"jour": "Fri, 15 Apr 2022 00:00:00 GMT",
"matin": false,
"estabs": true,
"estjust": true,
"description": "",
"begin": "2022-04-15 12:00:00",
"end": "2022-04-15 17:59:59"
}
]
"""
2022-05-11 00:59:51 +02:00
etud = Identite.query.get(etudid)
if etud is None:
return error_response(
404,
message="id de l'étudiant (etudid, nip, ine) inconnu",
)
2022-05-11 00:59:51 +02:00
# Absences justifiées de l'étudiant
2022-05-03 13:35:17 +02:00
abs_just = [
2022-05-11 00:59:51 +02:00
absence for absence in sco_abs.list_abs_date(etud.id) if absence["estjust"]
2022-05-03 13:35:17 +02:00
]
for absence in abs_just:
2022-04-27 05:19:13 +02:00
absence["jour"] = absence["jour"].isoformat()
2022-05-03 13:35:17 +02:00
return jsonify(abs_just)
2022-04-27 05:19:13 +02:00
2022-05-16 15:15:33 +02:00
@bp.route(
"/absences/abs_group_etat/<int:group_id>",
methods=["GET"],
)
@bp.route(
"/absences/abs_group_etat/group_id/<int:group_id>/date_debut/<string:date_debut>/date_fin/<string:date_fin>",
methods=["GET"],
)
@token_auth.login_required
@token_permission_required(Permission.APIView)
def abs_groupe_etat(group_id: int, date_debut=None, date_fin=None):
"""
Liste des absences d'un groupe (possibilité de choisir entre deux dates)
group_id = l'id du groupe
date_debut = None par défaut, sinon la date ISO du début de notre filtre
date_fin = None par défaut, sinon la date ISO de la fin de notre filtre
Exemple de résultat :
[
{
"etudid": 1,
"list_abs": []
},
{
"etudid": 2,
"list_abs": [
{
"jour": "Fri, 15 Apr 2022 00:00:00 GMT",
"matin": true,
"estabs": true,
"estjust": true,
"description": "",
"begin": "2022-04-15 08:00:00",
"end": "2022-04-15 11:59:59"
},
{
"jour": "Fri, 15 Apr 2022 00:00:00 GMT",
"matin": false,
"estabs": true,
"estjust": false,
"description": "",
"begin": "2022-04-15 12:00:00",
"end": "2022-04-15 17:59:59"
},
]
},
...
]
"""
# Fonction utilisée : app.scodoc.sco_groups.get_group_members() et app.scodoc.sco_abs.list_abs_date()
# Utilisation de la fonction get_group_members
members = get_group_members(group_id)
data = []
# Filtre entre les deux dates renseignées
for member in members:
abs = {
"etudid": member["etudid"],
"list_abs": sco_abs.list_abs_date(member["etudid"], date_debut, date_fin),
}
data.append(abs)
return jsonify(data)