Assiduites : Fonctionnement BackEnd + API

This commit is contained in:
iziram 2023-04-17 15:35:42 +02:00
parent e49448f809
commit 2a63533fc0
4 changed files with 44 additions and 63 deletions

View File

@ -2,7 +2,7 @@
"""
from flask_json import as_json
from flask import Blueprint
from flask import request, g
from flask import request, g, jsonify
from app import db
from app.scodoc import sco_utils as scu
from app.scodoc.sco_exceptions import ScoException
@ -35,7 +35,6 @@ def requested_format(default_format="json", allowed_formats=None):
return None
@as_json
def get_model_api_object(model_cls: db.Model, model_id: int, join_cls: db.Model = None):
"""
Retourne une réponse contenant la représentation api de l'objet "Model[model_id]"
@ -49,7 +48,7 @@ def get_model_api_object(model_cls: db.Model, model_id: int, join_cls: db.Model
query = query.join(join_cls).filter_by(dept_id=g.scodoc_dept_id)
unique: model_cls = query.first_or_404()
return unique.to_dict(format_api=True)
return jsonify(unique.to_dict(format_api=True))
from app.api import tokens

View File

@ -6,8 +6,7 @@
"""ScoDoc 9 API : Assiduités
"""
from datetime import datetime
from flask_json import as_json
from flask import g, request
from flask import g, jsonify, request
from flask_login import login_required, current_user
import app.scodoc.sco_assiduites as scass
@ -53,7 +52,6 @@ def assiduite(assiduite_id: int = None):
@api_web_bp.route("/assiduites/<int:etudid>/count/query", defaults={"with_query": True})
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
def count_assiduites(etudid: int = None, with_query: bool = False):
"""
@ -111,9 +109,11 @@ def count_assiduites(etudid: int = None, with_query: bool = False):
if with_query:
metric, filtered = _count_manager(request)
return scass.get_assiduites_stats(
return jsonify(
scass.get_assiduites_stats(
assiduites=etud.assiduites, metric=metric, filtered=filtered
)
)
@bp.route("/assiduites/<int:etudid>", defaults={"with_query": False})
@ -122,7 +122,6 @@ def count_assiduites(etudid: int = None, with_query: bool = False):
@api_web_bp.route("/assiduites/<int:etudid>/query", defaults={"with_query": True})
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
def assiduites(etudid: int = None, with_query: bool = False):
"""
@ -179,14 +178,13 @@ def assiduites(etudid: int = None, with_query: bool = False):
data = ass.to_dict(format_api=True)
data_set.append(data)
return data_set
return jsonify(data_set)
@bp.route("/assiduites/group/query", defaults={"with_query": True})
@api_web_bp.route("/assiduites/group/query", defaults={"with_query": True})
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
def assiduites_group(with_query: bool = False):
"""
@ -249,11 +247,12 @@ def assiduites_group(with_query: bool = False):
if with_query:
assiduites_query = _filter_manager(request, assiduites_query)
data_set: dict[list[dict]] = {str(key): [] for key in etuds}
data_set: dict[list[dict]] = {key: [] for key in etuds}
for ass in assiduites_query.all():
data = ass.to_dict(format_api=True)
data_set.get(str(data["etudid"])).append(data)
return data_set
data_set.get(data["etudid"]).append(data)
return jsonify(data_set)
@bp.route(
@ -272,7 +271,6 @@ def assiduites_group(with_query: bool = False):
)
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
def assiduites_formsemestre(formsemestre_id: int, with_query: bool = False):
"""Retourne toutes les assiduités du formsemestre"""
@ -293,7 +291,7 @@ def assiduites_formsemestre(formsemestre_id: int, with_query: bool = False):
data = ass.to_dict(format_api=True)
data_set.append(data)
return data_set
return jsonify(data_set)
@bp.route(
@ -314,7 +312,6 @@ def assiduites_formsemestre(formsemestre_id: int, with_query: bool = False):
)
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
def count_assiduites_formsemestre(
formsemestre_id: int = None, with_query: bool = False
@ -337,13 +334,12 @@ def count_assiduites_formsemestre(
if with_query:
metric, filtered = _count_manager(request)
return scass.get_assiduites_stats(assiduites_query, metric, filtered)
return jsonify(scass.get_assiduites_stats(assiduites_query, metric, filtered))
@bp.route("/assiduite/<int:etudid>/create", methods=["POST"])
@api_web_bp.route("/assiduite/<int:etudid>/create", methods=["POST"])
@scodoc
@as_json
@login_required
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
@ -386,13 +382,12 @@ def assiduite_create(etudid: int = None):
db.session.commit()
return {"errors": errors, "success": success}
return jsonify({"errors": errors, "success": success})
@bp.route("/assiduites/create", methods=["POST"])
@api_web_bp.route("/assiduites/create", methods=["POST"])
@scodoc
@as_json
@login_required
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
@ -440,7 +435,7 @@ def assiduites_create():
else:
success[i] = obj
return {"errors": errors, "success": success}
return jsonify({"errors": errors, "success": success})
def _create_singular(
@ -520,7 +515,6 @@ def _create_singular(
@api_web_bp.route("/assiduite/delete", methods=["POST"])
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def assiduite_delete():
@ -549,7 +543,7 @@ def assiduite_delete():
else:
output["success"][f"{i}"] = {"OK": True}
db.session.commit()
return output
return jsonify(output)
def _delete_singular(assiduite_id: int, database):
@ -564,7 +558,6 @@ def _delete_singular(assiduite_id: int, database):
@api_web_bp.route("/assiduite/<int:assiduite_id>/edit", methods=["POST"])
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def assiduite_edit(assiduite_id: int):
@ -632,14 +625,13 @@ def assiduite_edit(assiduite_id: int):
db.session.add(assiduite_unique)
db.session.commit()
return {"OK": True}
return jsonify({"OK": True})
@bp.route("/assiduites/edit", methods=["POST"])
@api_web_bp.route("/assiduites/edit", methods=["POST"])
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def assiduites_edit():
@ -674,7 +666,7 @@ def assiduites_edit():
db.session.commit()
return {"errors": errors, "success": success}
return jsonify({"errors": errors, "success": success})
def _edit_singular(assiduite_unique, data):

View File

@ -7,7 +7,6 @@
"""
from datetime import datetime
from flask_json import as_json
from flask import g, jsonify, request
from flask_login import login_required, current_user
@ -58,7 +57,6 @@ def justificatif(justif_id: int = None):
@api_web_bp.route("/justificatifs/<int:etudid>/query", defaults={"with_query": True})
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
def justificatifs(etudid: int = None, with_query: bool = False):
"""
@ -102,14 +100,13 @@ def justificatifs(etudid: int = None, with_query: bool = False):
data = just.to_dict(format_api=True)
data_set.append(data)
return data_set
return jsonify(data_set)
@bp.route("/justificatif/<int:etudid>/create", methods=["POST"])
@api_web_bp.route("/justificatif/<int:etudid>/create", methods=["POST"])
@scodoc
@login_required
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def justif_create(etudid: int = None):
@ -148,7 +145,7 @@ def justif_create(etudid: int = None):
else:
success[i] = obj
compute_assiduites_justified(Justificatif.query.filter_by(etudid=etudid), True)
return {"errors": errors, "success": success}
return jsonify({"errors": errors, "success": success})
def _create_singular(
@ -224,7 +221,6 @@ def _create_singular(
@api_web_bp.route("/justificatif/<int:justif_id>/edit", methods=["POST"])
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def justif_edit(justif_id: int):
@ -300,7 +296,8 @@ def justif_edit(justif_id: int):
db.session.add(justificatif_unique)
db.session.commit()
return {
return jsonify(
{
"couverture": {
"avant": avant_ids,
"après": compute_assiduites_justified(
@ -309,13 +306,13 @@ def justif_edit(justif_id: int):
),
}
}
)
@bp.route("/justificatif/delete", methods=["POST"])
@api_web_bp.route("/justificatif/delete", methods=["POST"])
@login_required
@scodoc
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def justif_delete():
@ -344,8 +341,7 @@ def justif_delete():
else:
output["success"][f"{i}"] = {"OK": True}
db.session.commit()
return output
return jsonify(output)
def _delete_singular(justif_id: int, database):
@ -360,12 +356,8 @@ def _delete_singular(justif_id: int, database):
if archive_name is not None:
archiver: JustificatifArchiver = JustificatifArchiver()
archiver.delete_justificatif(justificatif_unique.etudid, archive_name)
database.session.delete(justificatif_unique)
compute_assiduites_justified(
Justificatif.query.filter_by(etudid=justificatif_unique.etudid), True
)
return (200, "OK")
@ -374,7 +366,6 @@ def _delete_singular(justif_id: int, database):
@api_web_bp.route("/justificatif/<int:justif_id>/import", methods=["POST"])
@scodoc
@login_required
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def justif_import(justif_id: int = None):
@ -411,7 +402,7 @@ def justif_import(justif_id: int = None):
db.session.add(justificatif_unique)
db.session.commit()
return {"filename": fname}
return jsonify({"filename": fname})
except ScoValueError as err:
return json_error(404, err.args[0])
@ -451,7 +442,6 @@ def justif_export(justif_id: int = None, filename: str = None):
@api_web_bp.route("/justificatif/<int:justif_id>/remove", methods=["POST"])
@scodoc
@login_required
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def justif_remove(justif_id: int = None):
@ -509,14 +499,13 @@ def justif_remove(justif_id: int = None):
except ScoValueError as err:
return json_error(404, err.args[0])
return {"response": "removed"}
return jsonify({"response": "removed"})
@bp.route("/justificatif/<int:justif_id>/list", methods=["GET"])
@api_web_bp.route("/justificatif/<int:justif_id>/list", methods=["GET"])
@scodoc
@login_required
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def justif_list(justif_id: int = None):
@ -540,7 +529,7 @@ def justif_list(justif_id: int = None):
archive_name, justificatif_unique.etudid
)
return filenames
return jsonify(filenames)
# Partie justification
@ -548,7 +537,6 @@ def justif_list(justif_id: int = None):
@api_web_bp.route("/justificatif/<int:justif_id>/justifies", methods=["GET"])
@scodoc
@login_required
@as_json
@permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange)
def justif_justifies(justif_id: int = None):
@ -564,7 +552,7 @@ def justif_justifies(justif_id: int = None):
assiduites_list: list[int] = scass.justifies(justificatif_unique)
return assiduites_list
return jsonify(assiduites_list)
# -- Utils --

View File

@ -317,12 +317,14 @@ def justifies(justi: Justificatif, obj: bool = False) -> list[int]:
if justi.etat != scu.EtatJustificatif.VALIDE:
return []
assiduites_query: Assiduite = Assiduite.query.join(
Justificatif, Assiduite.etudid == Justificatif.etudid
).filter(
assiduites_query: Assiduite = (
Assiduite.query.join(Justificatif, Assiduite.etudid == Justificatif.etudid)
.filter(Assiduite.etat != scu.EtatAssiduite.PRESENT)
.filter(
Assiduite.date_debut <= justi.date_fin,
Assiduite.date_fin >= justi.date_debut,
)
)
if not obj:
return [assi.id for assi in assiduites_query.all()]