From 25c6d99c0ac2442676617c1b15f8f8e557e9ed2d Mon Sep 17 00:00:00 2001 From: iziram Date: Wed, 31 May 2023 22:00:20 +0200 Subject: [PATCH] Revert "Assiduites : Fonctionnement BackEnd + API" This reverts commit 2a63533fc0388f5c7021ddbf6326de765371a0fd. Mauvaise remise en forme de la branche --- app/api/__init__.py | 5 ++-- app/api/assiduites.py | 42 ++++++++++++++++++------------- app/api/justificatifs.py | 48 ++++++++++++++++++++++-------------- app/scodoc/sco_assiduites.py | 12 ++++----- 4 files changed, 63 insertions(+), 44 deletions(-) diff --git a/app/api/__init__.py b/app/api/__init__.py index f84510a4..bb8f6cc5 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -2,7 +2,7 @@ """ from flask_json import as_json from flask import Blueprint -from flask import request, g, jsonify +from flask import request, g from app import db from app.scodoc import sco_utils as scu from app.scodoc.sco_exceptions import ScoException @@ -35,6 +35,7 @@ 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]" @@ -48,7 +49,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 jsonify(unique.to_dict(format_api=True)) + return unique.to_dict(format_api=True) from app.api import tokens diff --git a/app/api/assiduites.py b/app/api/assiduites.py index 5853e3ff..044931b6 100644 --- a/app/api/assiduites.py +++ b/app/api/assiduites.py @@ -6,7 +6,8 @@ """ScoDoc 9 API : Assiduités """ from datetime import datetime -from flask import g, jsonify, request +from flask_json import as_json +from flask import g, request from flask_login import login_required, current_user import app.scodoc.sco_assiduites as scass @@ -52,6 +53,7 @@ def assiduite(assiduite_id: int = None): @api_web_bp.route("/assiduites//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): """ @@ -109,10 +111,8 @@ def count_assiduites(etudid: int = None, with_query: bool = False): if with_query: metric, filtered = _count_manager(request) - return jsonify( - scass.get_assiduites_stats( - assiduites=etud.assiduites, metric=metric, filtered=filtered - ) + return scass.get_assiduites_stats( + assiduites=etud.assiduites, metric=metric, filtered=filtered ) @@ -122,6 +122,7 @@ def count_assiduites(etudid: int = None, with_query: bool = False): @api_web_bp.route("/assiduites//query", defaults={"with_query": True}) @login_required @scodoc +@as_json @permission_required(Permission.ScoView) def assiduites(etudid: int = None, with_query: bool = False): """ @@ -178,13 +179,14 @@ def assiduites(etudid: int = None, with_query: bool = False): data = ass.to_dict(format_api=True) data_set.append(data) - return jsonify(data_set) + return 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): """ @@ -247,12 +249,11 @@ def assiduites_group(with_query: bool = False): if with_query: assiduites_query = _filter_manager(request, assiduites_query) - data_set: dict[list[dict]] = {key: [] for key in etuds} + data_set: dict[list[dict]] = {str(key): [] for key in etuds} for ass in assiduites_query.all(): data = ass.to_dict(format_api=True) - data_set.get(data["etudid"]).append(data) - - return jsonify(data_set) + data_set.get(str(data["etudid"])).append(data) + return data_set @bp.route( @@ -271,6 +272,7 @@ 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""" @@ -291,7 +293,7 @@ def assiduites_formsemestre(formsemestre_id: int, with_query: bool = False): data = ass.to_dict(format_api=True) data_set.append(data) - return jsonify(data_set) + return data_set @bp.route( @@ -312,6 +314,7 @@ 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 @@ -334,12 +337,13 @@ def count_assiduites_formsemestre( if with_query: metric, filtered = _count_manager(request) - return jsonify(scass.get_assiduites_stats(assiduites_query, metric, filtered)) + return scass.get_assiduites_stats(assiduites_query, metric, filtered) @bp.route("/assiduite//create", methods=["POST"]) @api_web_bp.route("/assiduite//create", methods=["POST"]) @scodoc +@as_json @login_required @permission_required(Permission.ScoView) # @permission_required(Permission.ScoAssiduiteChange) @@ -382,12 +386,13 @@ def assiduite_create(etudid: int = None): db.session.commit() - return jsonify({"errors": errors, "success": success}) + return {"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) @@ -435,7 +440,7 @@ def assiduites_create(): else: success[i] = obj - return jsonify({"errors": errors, "success": success}) + return {"errors": errors, "success": success} def _create_singular( @@ -515,6 +520,7 @@ 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(): @@ -543,7 +549,7 @@ def assiduite_delete(): else: output["success"][f"{i}"] = {"OK": True} db.session.commit() - return jsonify(output) + return output def _delete_singular(assiduite_id: int, database): @@ -558,6 +564,7 @@ def _delete_singular(assiduite_id: int, database): @api_web_bp.route("/assiduite//edit", methods=["POST"]) @login_required @scodoc +@as_json @permission_required(Permission.ScoView) # @permission_required(Permission.ScoAssiduiteChange) def assiduite_edit(assiduite_id: int): @@ -625,13 +632,14 @@ def assiduite_edit(assiduite_id: int): db.session.add(assiduite_unique) db.session.commit() - return jsonify({"OK": True}) + return {"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(): @@ -666,7 +674,7 @@ def assiduites_edit(): db.session.commit() - return jsonify({"errors": errors, "success": success}) + return {"errors": errors, "success": success} def _edit_singular(assiduite_unique, data): diff --git a/app/api/justificatifs.py b/app/api/justificatifs.py index 7017ca1b..e2cfe61e 100644 --- a/app/api/justificatifs.py +++ b/app/api/justificatifs.py @@ -7,6 +7,7 @@ """ from datetime import datetime +from flask_json import as_json from flask import g, jsonify, request from flask_login import login_required, current_user @@ -57,6 +58,7 @@ def justificatif(justif_id: int = None): @api_web_bp.route("/justificatifs//query", defaults={"with_query": True}) @login_required @scodoc +@as_json @permission_required(Permission.ScoView) def justificatifs(etudid: int = None, with_query: bool = False): """ @@ -100,13 +102,14 @@ def justificatifs(etudid: int = None, with_query: bool = False): data = just.to_dict(format_api=True) data_set.append(data) - return jsonify(data_set) + return data_set @bp.route("/justificatif//create", methods=["POST"]) @api_web_bp.route("/justificatif//create", methods=["POST"]) @scodoc @login_required +@as_json @permission_required(Permission.ScoView) # @permission_required(Permission.ScoAssiduiteChange) def justif_create(etudid: int = None): @@ -145,7 +148,7 @@ def justif_create(etudid: int = None): else: success[i] = obj compute_assiduites_justified(Justificatif.query.filter_by(etudid=etudid), True) - return jsonify({"errors": errors, "success": success}) + return {"errors": errors, "success": success} def _create_singular( @@ -221,6 +224,7 @@ def _create_singular( @api_web_bp.route("/justificatif//edit", methods=["POST"]) @login_required @scodoc +@as_json @permission_required(Permission.ScoView) # @permission_required(Permission.ScoAssiduiteChange) def justif_edit(justif_id: int): @@ -296,23 +300,22 @@ def justif_edit(justif_id: int): db.session.add(justificatif_unique) db.session.commit() - return jsonify( - { - "couverture": { - "avant": avant_ids, - "après": compute_assiduites_justified( - Justificatif.query.filter_by(etudid=justificatif_unique.etudid), - True, - ), - } + return { + "couverture": { + "avant": avant_ids, + "après": compute_assiduites_justified( + Justificatif.query.filter_by(etudid=justificatif_unique.etudid), + True, + ), } - ) + } @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(): @@ -341,7 +344,8 @@ def justif_delete(): else: output["success"][f"{i}"] = {"OK": True} db.session.commit() - return jsonify(output) + + return output def _delete_singular(justif_id: int, database): @@ -356,8 +360,12 @@ 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") @@ -366,6 +374,7 @@ def _delete_singular(justif_id: int, database): @api_web_bp.route("/justificatif//import", methods=["POST"]) @scodoc @login_required +@as_json @permission_required(Permission.ScoView) # @permission_required(Permission.ScoAssiduiteChange) def justif_import(justif_id: int = None): @@ -402,7 +411,7 @@ def justif_import(justif_id: int = None): db.session.add(justificatif_unique) db.session.commit() - return jsonify({"filename": fname}) + return {"filename": fname} except ScoValueError as err: return json_error(404, err.args[0]) @@ -442,6 +451,7 @@ def justif_export(justif_id: int = None, filename: str = None): @api_web_bp.route("/justificatif//remove", methods=["POST"]) @scodoc @login_required +@as_json @permission_required(Permission.ScoView) # @permission_required(Permission.ScoAssiduiteChange) def justif_remove(justif_id: int = None): @@ -499,13 +509,14 @@ def justif_remove(justif_id: int = None): except ScoValueError as err: return json_error(404, err.args[0]) - return jsonify({"response": "removed"}) + return {"response": "removed"} @bp.route("/justificatif//list", methods=["GET"]) @api_web_bp.route("/justificatif//list", methods=["GET"]) @scodoc @login_required +@as_json @permission_required(Permission.ScoView) # @permission_required(Permission.ScoAssiduiteChange) def justif_list(justif_id: int = None): @@ -529,7 +540,7 @@ def justif_list(justif_id: int = None): archive_name, justificatif_unique.etudid ) - return jsonify(filenames) + return filenames # Partie justification @@ -537,6 +548,7 @@ def justif_list(justif_id: int = None): @api_web_bp.route("/justificatif//justifies", methods=["GET"]) @scodoc @login_required +@as_json @permission_required(Permission.ScoView) # @permission_required(Permission.ScoAssiduiteChange) def justif_justifies(justif_id: int = None): @@ -552,7 +564,7 @@ def justif_justifies(justif_id: int = None): assiduites_list: list[int] = scass.justifies(justificatif_unique) - return jsonify(assiduites_list) + return assiduites_list # -- Utils -- diff --git a/app/scodoc/sco_assiduites.py b/app/scodoc/sco_assiduites.py index 1fb1c125..4af613a5 100644 --- a/app/scodoc/sco_assiduites.py +++ b/app/scodoc/sco_assiduites.py @@ -317,13 +317,11 @@ 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(Assiduite.etat != scu.EtatAssiduite.PRESENT) - .filter( - Assiduite.date_debut <= justi.date_fin, - Assiduite.date_fin >= justi.date_debut, - ) + assiduites_query: Assiduite = Assiduite.query.join( + Justificatif, Assiduite.etudid == Justificatif.etudid + ).filter( + Assiduite.date_debut <= justi.date_fin, + Assiduite.date_fin >= justi.date_debut, ) if not obj: