2022-08-01 21:42:19 +02:00
|
|
|
##############################################################################
|
|
|
|
# ScoDoc
|
2023-12-31 23:04:06 +01:00
|
|
|
# Copyright (c) 1999 - 2024 Emmanuel Viennet. All rights reserved.
|
2022-08-01 21:42:19 +02:00
|
|
|
# See LICENSE
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""
|
|
|
|
API : billets d'absences
|
2024-07-24 17:34:30 +02:00
|
|
|
|
|
|
|
CATEGORY
|
|
|
|
--------
|
|
|
|
Billets d'absence
|
|
|
|
|
2022-08-01 21:42:19 +02:00
|
|
|
"""
|
|
|
|
|
2023-04-06 16:10:32 +02:00
|
|
|
from flask import g, request
|
|
|
|
from flask_json import as_json
|
2022-08-01 21:42:19 +02:00
|
|
|
from flask_login import login_required
|
|
|
|
|
|
|
|
from app import db
|
|
|
|
from app.api import api_bp as bp, api_web_bp
|
|
|
|
from app.decorators import scodoc, permission_required
|
2022-08-07 19:56:25 +02:00
|
|
|
from app.scodoc.sco_utils import json_error
|
2022-08-01 21:42:19 +02:00
|
|
|
from app.models import BilletAbsence
|
|
|
|
from app.models.etudiants import Identite
|
|
|
|
from app.scodoc import sco_abs_billets
|
|
|
|
from app.scodoc.sco_permissions import Permission
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/billets_absence/etudiant/<int:etudid>")
|
|
|
|
@api_web_bp.route("/billets_absence/etudiant/<int:etudid>")
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-01 21:42:19 +02:00
|
|
|
def billets_absence_etudiant(etudid: int):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Liste des billets d'absence pour cet étudiant."""
|
2022-08-01 21:42:19 +02:00
|
|
|
billets = sco_abs_billets.query_billets_etud(etudid)
|
2023-04-06 16:10:32 +02:00
|
|
|
return [billet.to_dict() for billet in billets]
|
2022-08-01 21:42:19 +02:00
|
|
|
|
|
|
|
|
2022-08-02 08:48:08 +02:00
|
|
|
@bp.route("/billets_absence/create", methods=["POST"])
|
|
|
|
@api_web_bp.route("/billets_absence/create", methods=["POST"])
|
2022-08-01 21:42:19 +02:00
|
|
|
@login_required
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.AbsAddBillet)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-02 08:48:08 +02:00
|
|
|
def billets_absence_create():
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Ajout d'un billet d'absence. Renvoie le billet créé en json.
|
|
|
|
|
|
|
|
DATA
|
|
|
|
----
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"etudid" : int,
|
|
|
|
"abs_begin" : date_iso,
|
|
|
|
"abs_end" : date_iso,
|
|
|
|
"description" : string,
|
|
|
|
"justified" : bool
|
|
|
|
}
|
|
|
|
```
|
|
|
|
"""
|
2022-08-01 21:42:19 +02:00
|
|
|
data = request.get_json(force=True) # may raise 400 Bad Request
|
|
|
|
etudid = data.get("etudid")
|
|
|
|
abs_begin = data.get("abs_begin")
|
|
|
|
abs_end = data.get("abs_end")
|
|
|
|
description = data.get("description", "")
|
|
|
|
justified = data.get("justified", False)
|
|
|
|
if None in (etudid, abs_begin, abs_end):
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(
|
2023-02-22 02:13:06 +01:00
|
|
|
404, message="Paramètre manquant: etudid, abs_begin, abs_end requis"
|
2022-08-01 21:42:19 +02:00
|
|
|
)
|
2023-03-20 11:17:38 +01:00
|
|
|
etud = Identite.get_etud(etudid)
|
2022-08-01 21:42:19 +02:00
|
|
|
billet = BilletAbsence(
|
|
|
|
etudid=etud.id,
|
|
|
|
abs_begin=abs_begin,
|
|
|
|
abs_end=abs_end,
|
|
|
|
description=description,
|
|
|
|
etat=False,
|
|
|
|
justified=justified,
|
|
|
|
)
|
|
|
|
db.session.add(billet)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return billet.to_dict()
|
2022-08-01 21:42:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/billets_absence/<int:billet_id>/delete", methods=["POST"])
|
|
|
|
@api_web_bp.route("/billets_absence/<int:billet_id>/delete", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.AbsAddBillet)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-01 21:42:19 +02:00
|
|
|
def billets_absence_delete(billet_id: int):
|
|
|
|
"""Suppression d'un billet d'absence"""
|
|
|
|
query = BilletAbsence.query.filter_by(id=billet_id)
|
|
|
|
if g.scodoc_dept is not None:
|
|
|
|
# jointure avec departement de l'étudiant
|
|
|
|
query = query.join(BilletAbsence.etudiant).filter_by(dept_id=g.scodoc_dept_id)
|
|
|
|
billet = query.first_or_404()
|
|
|
|
db.session.delete(billet)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return {"OK": True}
|