2022-08-05 17:05:24 +02:00
|
|
|
##############################################################################
|
|
|
|
# ScoDoc
|
2023-01-02 13:16:27 +01:00
|
|
|
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
|
2022-08-05 17:05:24 +02:00
|
|
|
# See LICENSE
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""
|
|
|
|
ScoDoc 9 API : accès aux départements
|
|
|
|
|
2023-11-26 18:28:56 +01:00
|
|
|
Note: les routes /departement[s] sont publiées sur l'API (/ScoDoc/api/),
|
2022-08-05 17:05:24 +02:00
|
|
|
mais évidemment pas sur l'API web (/ScoDoc/<dept>/api).
|
|
|
|
"""
|
2022-10-31 10:12:04 +01:00
|
|
|
from datetime import datetime
|
2022-05-03 13:35:17 +02:00
|
|
|
|
2023-04-06 16:10:32 +02:00
|
|
|
from flask import request
|
|
|
|
from flask_json import as_json
|
2022-08-05 17:05:24 +02:00
|
|
|
from flask_login import login_required
|
2022-03-02 16:45:47 +01:00
|
|
|
|
2022-05-05 18:11:44 +02:00
|
|
|
import app
|
2022-12-03 11:36:01 +01:00
|
|
|
from app import db
|
2023-02-22 02:13:06 +01:00
|
|
|
from app.api import api_bp as bp, API_CLIENT_ERROR
|
2022-08-07 19:56:25 +02:00
|
|
|
from app.scodoc.sco_utils import json_error
|
2022-07-27 16:03:14 +02:00
|
|
|
from app.decorators import scodoc, permission_required
|
2022-05-05 18:50:59 +02:00
|
|
|
from app.models import Departement, FormSemestre
|
2022-08-05 06:55:05 +02:00
|
|
|
from app.models import departements
|
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
2022-03-04 17:16:08 +01:00
|
|
|
from app.scodoc.sco_permissions import Permission
|
2022-03-02 16:45:47 +01:00
|
|
|
|
|
|
|
|
2022-05-05 18:50:59 +02:00
|
|
|
def get_departement(dept_ident: str) -> Departement:
|
|
|
|
"Le departement, par id ou acronyme. Erreur 404 si pas trouvé."
|
|
|
|
try:
|
|
|
|
dept_id = int(dept_ident)
|
|
|
|
except ValueError:
|
|
|
|
dept_id = None
|
|
|
|
if dept_id is None:
|
|
|
|
return Departement.query.filter_by(acronym=dept_ident).first_or_404()
|
|
|
|
return Departement.query.get_or_404(dept_id)
|
|
|
|
|
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
@bp.route("/departements")
|
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-05 06:55:05 +02:00
|
|
|
def departements_list():
|
2022-07-21 09:59:54 +02:00
|
|
|
"""Liste les départements"""
|
2023-04-06 16:10:32 +02:00
|
|
|
return [dept.to_dict(with_dept_name=True) for dept in Departement.query]
|
2022-07-21 09:59:54 +02:00
|
|
|
|
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
@bp.route("/departements_ids")
|
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-05-05 18:11:44 +02:00
|
|
|
def departements_ids():
|
|
|
|
"""Liste des ids de départements"""
|
2023-04-06 16:10:32 +02:00
|
|
|
return [dept.id for dept in Departement.query]
|
2022-05-05 18:11:44 +02:00
|
|
|
|
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
@bp.route("/departement/<string:acronym>")
|
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-07-21 09:59:54 +02:00
|
|
|
def departement(acronym: str):
|
2022-03-02 16:45:47 +01:00
|
|
|
"""
|
2022-07-21 09:59:54 +02:00
|
|
|
Info sur un département. Accès par acronyme.
|
2022-03-02 16:45:47 +01:00
|
|
|
|
2022-04-28 09:06:20 +02:00
|
|
|
Exemple de résultat :
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"acronym": "TAPI",
|
2022-12-03 11:36:01 +01:00
|
|
|
"dept_name" : "TEST",
|
2022-04-28 09:06:20 +02:00
|
|
|
"description": null,
|
|
|
|
"visible": true,
|
|
|
|
"date_creation": "Fri, 15 Apr 2022 12:19:28 GMT"
|
2022-05-05 18:11:44 +02:00
|
|
|
}
|
2022-03-02 16:45:47 +01:00
|
|
|
"""
|
2022-07-21 09:59:54 +02:00
|
|
|
dept = Departement.query.filter_by(acronym=acronym).first_or_404()
|
2023-04-06 16:10:32 +02:00
|
|
|
return dept.to_dict(with_dept_name=True)
|
2022-03-02 16:45:47 +01:00
|
|
|
|
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
@bp.route("/departement/id/<int:dept_id>")
|
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-07-21 09:59:54 +02:00
|
|
|
def departement_by_id(dept_id: int):
|
|
|
|
"""
|
|
|
|
Info sur un département. Accès par id.
|
|
|
|
"""
|
|
|
|
dept = Departement.query.get_or_404(dept_id)
|
2023-04-06 16:10:32 +02:00
|
|
|
return dept.to_dict()
|
2022-03-02 16:45:47 +01:00
|
|
|
|
|
|
|
|
2022-08-05 06:55:05 +02:00
|
|
|
@bp.route("/departement/create", methods=["POST"])
|
2022-08-05 17:05:24 +02:00
|
|
|
@login_required
|
2022-08-05 06:55:05 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-05 06:55:05 +02:00
|
|
|
def departement_create():
|
|
|
|
"""
|
|
|
|
Création d'un département.
|
|
|
|
The request content type should be "application/json":
|
|
|
|
{
|
|
|
|
"acronym": str,
|
|
|
|
"visible":bool,
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
data = request.get_json(force=True) # may raise 400 Bad Request
|
|
|
|
acronym = str(data.get("acronym", ""))
|
|
|
|
if not acronym:
|
2023-02-22 02:13:06 +01:00
|
|
|
return json_error(API_CLIENT_ERROR, "missing acronym")
|
2022-08-05 06:55:05 +02:00
|
|
|
visible = bool(data.get("visible", True))
|
|
|
|
try:
|
|
|
|
dept = departements.create_dept(acronym, visible=visible)
|
|
|
|
except ScoValueError as exc:
|
2023-02-22 02:13:06 +01:00
|
|
|
return json_error(500, exc.args[0] if exc.args else "")
|
2023-04-06 16:10:32 +02:00
|
|
|
return dept.to_dict()
|
2022-08-05 06:55:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/departement/<string:acronym>/edit", methods=["POST"])
|
2022-08-05 17:05:24 +02:00
|
|
|
@login_required
|
2022-08-05 06:55:05 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-08-05 06:55:05 +02:00
|
|
|
def departement_edit(acronym):
|
|
|
|
"""
|
|
|
|
Edition d'un département: seul visible peut être modifié
|
|
|
|
The request content type should be "application/json":
|
|
|
|
{
|
|
|
|
"visible":bool,
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
dept = Departement.query.filter_by(acronym=acronym).first_or_404()
|
|
|
|
data = request.get_json(force=True) # may raise 400 Bad Request
|
|
|
|
visible = bool(data.get("visible", None))
|
|
|
|
if visible is None:
|
2023-02-22 02:13:06 +01:00
|
|
|
return json_error(API_CLIENT_ERROR, "missing argument: visible")
|
2022-08-05 06:55:05 +02:00
|
|
|
visible = bool(visible)
|
|
|
|
dept.visible = visible
|
|
|
|
db.session.add(dept)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return dept.to_dict()
|
2022-08-05 06:55:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/departement/<string:acronym>/delete", methods=["POST"])
|
2022-08-05 17:05:24 +02:00
|
|
|
@login_required
|
2022-08-05 06:55:05 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
|
|
|
def departement_delete(acronym):
|
|
|
|
"""
|
|
|
|
Suppression d'un département.
|
|
|
|
"""
|
|
|
|
dept = Departement.query.filter_by(acronym=acronym).first_or_404()
|
|
|
|
db.session.delete(dept)
|
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return {"OK": True}
|
2022-08-05 06:55:05 +02:00
|
|
|
|
|
|
|
|
2022-07-21 09:59:54 +02:00
|
|
|
@bp.route("/departement/<string:acronym>/etudiants", methods=["GET"])
|
2022-08-05 17:05:24 +02:00
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-07-21 09:59:54 +02:00
|
|
|
def dept_etudiants(acronym: str):
|
2022-03-02 16:45:47 +01:00
|
|
|
"""
|
|
|
|
Retourne la liste des étudiants d'un département
|
|
|
|
|
2022-07-21 09:59:54 +02:00
|
|
|
acronym: l'acronyme d'un département
|
2022-03-02 16:45:47 +01:00
|
|
|
|
|
|
|
Exemple de résultat :
|
2022-04-27 15:29:09 +02:00
|
|
|
[
|
|
|
|
{
|
2022-05-05 18:11:44 +02:00
|
|
|
"civilite": "M",
|
2022-07-29 16:19:40 +02:00
|
|
|
"code_ine": "7899X61616",
|
|
|
|
"code_nip": "F6777H88",
|
2022-03-02 16:45:47 +01:00
|
|
|
"date_naissance": null,
|
2022-05-05 18:11:44 +02:00
|
|
|
"email": "toto@toto.fr",
|
2022-03-02 16:45:47 +01:00
|
|
|
"emailperso": null,
|
|
|
|
"etudid": 18,
|
|
|
|
"nom": "MOREL",
|
|
|
|
"prenom": "JACQUES"
|
2022-04-27 15:29:09 +02:00
|
|
|
},
|
|
|
|
...
|
|
|
|
]
|
2022-03-02 16:45:47 +01:00
|
|
|
"""
|
2022-07-21 09:59:54 +02:00
|
|
|
dept = Departement.query.filter_by(acronym=acronym).first_or_404()
|
2023-04-06 16:10:32 +02:00
|
|
|
return [etud.to_dict_short() for etud in dept.etudiants]
|
2022-07-21 09:59:54 +02:00
|
|
|
|
2022-03-03 16:25:29 +01:00
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
@bp.route("/departement/id/<int:dept_id>/etudiants")
|
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-07-21 09:59:54 +02:00
|
|
|
def dept_etudiants_by_id(dept_id: int):
|
|
|
|
"""
|
|
|
|
Retourne la liste des étudiants d'un département d'id donné.
|
|
|
|
"""
|
|
|
|
dept = Departement.query.get_or_404(dept_id)
|
2023-04-06 16:10:32 +02:00
|
|
|
return [etud.to_dict_short() for etud in dept.etudiants]
|
2022-03-03 16:25:29 +01:00
|
|
|
|
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
@bp.route("/departement/<string:acronym>/formsemestres_ids")
|
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-07-21 09:59:54 +02:00
|
|
|
def dept_formsemestres_ids(acronym: str):
|
2022-05-05 19:49:43 +02:00
|
|
|
"""liste des ids formsemestre du département"""
|
2022-07-21 09:59:54 +02:00
|
|
|
dept = Departement.query.filter_by(acronym=acronym).first_or_404()
|
2023-04-06 16:10:32 +02:00
|
|
|
return [formsemestre.id for formsemestre in dept.formsemestres]
|
2022-05-05 19:49:43 +02:00
|
|
|
|
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
@bp.route("/departement/id/<int:dept_id>/formsemestres_ids")
|
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-07-21 09:59:54 +02:00
|
|
|
def dept_formsemestres_ids_by_id(dept_id: int):
|
|
|
|
"""liste des ids formsemestre du département"""
|
|
|
|
dept = Departement.query.get_or_404(dept_id)
|
2023-04-06 16:10:32 +02:00
|
|
|
return [formsemestre.id for formsemestre in dept.formsemestres]
|
2022-03-02 16:45:47 +01:00
|
|
|
|
2022-07-21 09:59:54 +02:00
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
@bp.route("/departement/<string:acronym>/formsemestres_courants")
|
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-07-21 09:59:54 +02:00
|
|
|
def dept_formsemestres_courants(acronym: str):
|
|
|
|
"""
|
|
|
|
Liste des semestres actifs d'un département d'acronyme donné
|
2022-03-02 16:45:47 +01:00
|
|
|
|
|
|
|
Exemple de résultat :
|
2022-04-27 15:29:09 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"titre": "master machine info",
|
|
|
|
"gestion_semestrielle": false,
|
|
|
|
"scodoc7_id": null,
|
|
|
|
"date_debut": "01/09/2021",
|
|
|
|
"bul_bgcolor": null,
|
|
|
|
"date_fin": "15/12/2022",
|
|
|
|
"resp_can_edit": false,
|
|
|
|
"dept_id": 1,
|
|
|
|
"etat": true,
|
|
|
|
"resp_can_change_ens": false,
|
|
|
|
"id": 1,
|
|
|
|
"modalite": "FI",
|
|
|
|
"ens_can_edit_eval": false,
|
|
|
|
"formation_id": 1,
|
|
|
|
"gestion_compensation": false,
|
|
|
|
"elt_sem_apo": null,
|
|
|
|
"semestre_id": 1,
|
|
|
|
"bul_hide_xml": false,
|
|
|
|
"elt_annee_apo": null,
|
|
|
|
"block_moyennes": false,
|
|
|
|
"formsemestre_id": 1,
|
|
|
|
"titre_num": "master machine info semestre 1",
|
|
|
|
"date_debut_iso": "2021-09-01",
|
|
|
|
"date_fin_iso": "2022-12-15",
|
|
|
|
"responsables": [
|
|
|
|
3,
|
|
|
|
2
|
|
|
|
]
|
|
|
|
},
|
|
|
|
...
|
|
|
|
]
|
2022-03-02 16:45:47 +01:00
|
|
|
"""
|
2022-07-21 09:59:54 +02:00
|
|
|
dept = Departement.query.filter_by(acronym=acronym).first_or_404()
|
2022-11-01 11:19:28 +01:00
|
|
|
date_courante = request.args.get("date_courante")
|
2023-11-26 18:28:56 +01:00
|
|
|
date_courante = datetime.fromisoformat(date_courante) if date_courante else None
|
2023-08-11 18:44:51 +02:00
|
|
|
return [
|
2023-11-26 18:28:56 +01:00
|
|
|
formsemestre.to_dict_api()
|
|
|
|
for formsemestre in FormSemestre.get_dept_formsemestres_courants(
|
|
|
|
dept, date_courante
|
2023-08-11 18:44:51 +02:00
|
|
|
)
|
|
|
|
]
|
2022-07-21 09:59:54 +02:00
|
|
|
|
|
|
|
|
2022-08-05 17:05:24 +02:00
|
|
|
@bp.route("/departement/id/<int:dept_id>/formsemestres_courants")
|
|
|
|
@login_required
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoView)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2022-07-21 09:59:54 +02:00
|
|
|
def dept_formsemestres_courants_by_id(dept_id: int):
|
|
|
|
"""
|
|
|
|
Liste des semestres actifs d'un département d'id donné
|
|
|
|
"""
|
|
|
|
# Le département, spécifié par un id ou un acronyme
|
|
|
|
dept = Departement.query.get_or_404(dept_id)
|
2022-11-01 11:19:28 +01:00
|
|
|
date_courante = request.args.get("date_courante")
|
|
|
|
if date_courante:
|
|
|
|
test_date = datetime.fromisoformat(date_courante)
|
2022-10-31 10:12:04 +01:00
|
|
|
else:
|
|
|
|
test_date = app.db.func.now()
|
2022-05-05 18:11:44 +02:00
|
|
|
# Les semestres en cours de ce département
|
2022-08-05 06:55:05 +02:00
|
|
|
formsemestres = FormSemestre.query.filter(
|
2022-05-05 18:50:59 +02:00
|
|
|
FormSemestre.dept_id == dept.id,
|
2022-10-31 10:12:04 +01:00
|
|
|
FormSemestre.date_debut <= test_date,
|
|
|
|
FormSemestre.date_fin >= test_date,
|
2022-05-05 18:11:44 +02:00
|
|
|
)
|
2022-03-02 16:45:47 +01:00
|
|
|
|
2023-04-06 16:10:32 +02:00
|
|
|
return [d.to_dict_api() for d in formsemestres]
|