From fe80051573feec78ef74b7dbdbe224d13d9e59a3 Mon Sep 17 00:00:00 2001 From: iziram Date: Wed, 17 May 2023 21:16:23 +0200 Subject: [PATCH] =?UTF-8?q?Assiduites=20:=20Mise=20=C3=A0=20jour=20suivi?= =?UTF-8?q?=20master=20(flask=5Fjson)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/__init__.py | 7 ++-- app/api/assiduites.py | 37 ++++++++++------- app/api/justificatifs.py | 41 +++++++++++-------- ...75e87f_modèles_assiduites_justificatifs.py | 2 +- tests/unit/test_assiduites.py | 2 +- 5 files changed, 53 insertions(+), 36 deletions(-) diff --git a/app/api/__init__.py b/app/api/__init__.py index a3c77eb97..bb8f6cc5c 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -1,8 +1,8 @@ """api.__init__ """ - +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 5853e3ff0..17ccf816c 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): """ @@ -252,7 +254,7 @@ def assiduites_group(with_query: bool = False): data = ass.to_dict(format_api=True) data_set.get(data["etudid"]).append(data) - return jsonify(data_set) + return data_set @bp.route( @@ -271,6 +273,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 +294,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 +315,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 +338,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 +387,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 +441,7 @@ def assiduites_create(): else: success[i] = obj - return jsonify({"errors": errors, "success": success}) + return {"errors": errors, "success": success} def _create_singular( @@ -515,6 +521,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 +550,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 +565,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 +633,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 +675,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 6228bcf86..e2cfe61eb 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(): @@ -342,7 +345,7 @@ def justif_delete(): output["success"][f"{i}"] = {"OK": True} db.session.commit() - return jsonify(output) + return output def _delete_singular(justif_id: int, database): @@ -371,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): @@ -407,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]) @@ -447,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): @@ -504,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): @@ -534,7 +540,7 @@ def justif_list(justif_id: int = None): archive_name, justificatif_unique.etudid ) - return jsonify(filenames) + return filenames # Partie justification @@ -542,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): @@ -557,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/migrations/versions/dbcf2175e87f_modèles_assiduites_justificatifs.py b/migrations/versions/dbcf2175e87f_modèles_assiduites_justificatifs.py index b74fa27d7..8b4b39d43 100755 --- a/migrations/versions/dbcf2175e87f_modèles_assiduites_justificatifs.py +++ b/migrations/versions/dbcf2175e87f_modèles_assiduites_justificatifs.py @@ -11,7 +11,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. revision = "dbcf2175e87f" -down_revision = "6520faf67508" +down_revision = "b8df1b913c79" branch_labels = None depends_on = None diff --git a/tests/unit/test_assiduites.py b/tests/unit/test_assiduites.py index 567c49ad5..3ba691a53 100644 --- a/tests/unit/test_assiduites.py +++ b/tests/unit/test_assiduites.py @@ -395,7 +395,7 @@ def verifier_filtrage_justificatifs(etud: Identite, justificatifs: list[Justific # Justifications des assiduites - assert len(scass.justifies(justificatifs[2])) == 1, "Justifications mauvais" + assert len(scass.justifies(justificatifs[2])) == 2, "Justifications mauvais" assert len(scass.justifies(justificatifs[0])) == 0, "Justifications mauvais"