MonScoDocEssai/app/api/absences.py

146 lines
4.2 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-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
# XXX TODO INACHEVEE
# @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( # XXX A REVOIR XXX
# group_id: int, date_debut, date_fin, with_boursier=True, format="html"
# ):
# """
# Liste des absences d'un ou plusieurs groupes entre deux dates
# """
# return error_response(501, message="Not implemented")
# # Fonction utilisée : app.scodoc.sco_groups.get_group_members() et app.scodoc.sco_abs.list_abs_date()
# try:
# # Utilisation de la fonction get_group_members
# members = get_group_members(group_id)
# except ValueError:
# return error_response(
# 404, message="La requête ne peut être traitée en létat actuel"
# )
# data = []
# # Filtre entre les deux dates renseignées
# for member in members:
# abs = sco_abs.list_abs_date(member.id, date_debut, date_fin)
# data.append(abs)
# # return jsonify(data) # XXX TODO faire en sorte de pouvoir renvoyer sa (ex to_dict() dans absences)
# return error_response(501, message="Not implemented")