2022-08-01 21:42:19 +02:00
|
|
|
##############################################################################
|
|
|
|
# ScoDoc
|
2023-01-02 13:16:27 +01:00
|
|
|
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
|
2022-08-01 21:42:19 +02:00
|
|
|
# See LICENSE
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""
|
|
|
|
API : billets d'absences
|
|
|
|
"""
|
|
|
|
|
|
|
|
from flask import g, jsonify, request
|
|
|
|
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)
|
|
|
|
def billets_absence_etudiant(etudid: int):
|
|
|
|
"""Liste des billets d'absence pour cet étudiant"""
|
|
|
|
billets = sco_abs_billets.query_billets_etud(etudid)
|
|
|
|
return jsonify([billet.to_dict() for billet in billets])
|
|
|
|
|
|
|
|
|
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
|
|
|
|
@permission_required(Permission.ScoAbsAddBillet)
|
2022-08-02 08:48:08 +02:00
|
|
|
def billets_absence_create():
|
2022-08-01 21:42:19 +02:00
|
|
|
"""Ajout d'un billet d'absence"""
|
|
|
|
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
|
|
|
)
|
|
|
|
query = Identite.query.filter_by(etudid=etudid)
|
|
|
|
if g.scodoc_dept:
|
|
|
|
query = query.filter_by(dept_id=g.scodoc_dept_id)
|
|
|
|
etud = query.first_or_404()
|
|
|
|
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()
|
|
|
|
return jsonify(billet.to_dict())
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
@permission_required(Permission.ScoAbsAddBillet)
|
|
|
|
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()
|
|
|
|
return jsonify({"OK": True})
|