1
0
forked from ScoDoc/ScoDoc

Assiduites : Mise à jour suivi master (flask_json)

This commit is contained in:
iziram 2023-05-17 21:16:23 +02:00
parent 4d5c1a84c3
commit fe80051573
5 changed files with 53 additions and 36 deletions

View File

@ -1,8 +1,8 @@
"""api.__init__ """api.__init__
""" """
from flask_json import as_json
from flask import Blueprint from flask import Blueprint
from flask import request, g, jsonify from flask import request, g
from app import db from app import db
from app.scodoc import sco_utils as scu from app.scodoc import sco_utils as scu
from app.scodoc.sco_exceptions import ScoException from app.scodoc.sco_exceptions import ScoException
@ -35,6 +35,7 @@ def requested_format(default_format="json", allowed_formats=None):
return None return None
@as_json
def get_model_api_object(model_cls: db.Model, model_id: int, join_cls: db.Model = None): 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]" 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) query = query.join(join_cls).filter_by(dept_id=g.scodoc_dept_id)
unique: model_cls = query.first_or_404() 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 from app.api import tokens

View File

@ -6,7 +6,8 @@
"""ScoDoc 9 API : Assiduités """ScoDoc 9 API : Assiduités
""" """
from datetime import datetime 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 from flask_login import login_required, current_user
import app.scodoc.sco_assiduites as scass import app.scodoc.sco_assiduites as scass
@ -52,6 +53,7 @@ def assiduite(assiduite_id: int = None):
@api_web_bp.route("/assiduites/<int:etudid>/count/query", defaults={"with_query": True}) @api_web_bp.route("/assiduites/<int:etudid>/count/query", defaults={"with_query": True})
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
def count_assiduites(etudid: int = None, with_query: bool = False): 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: if with_query:
metric, filtered = _count_manager(request) metric, filtered = _count_manager(request)
return jsonify( return scass.get_assiduites_stats(
scass.get_assiduites_stats( assiduites=etud.assiduites, metric=metric, filtered=filtered
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/<int:etudid>/query", defaults={"with_query": True}) @api_web_bp.route("/assiduites/<int:etudid>/query", defaults={"with_query": True})
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
def assiduites(etudid: int = None, with_query: bool = False): 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 = ass.to_dict(format_api=True)
data_set.append(data) data_set.append(data)
return jsonify(data_set) return data_set
@bp.route("/assiduites/group/query", defaults={"with_query": True}) @bp.route("/assiduites/group/query", defaults={"with_query": True})
@api_web_bp.route("/assiduites/group/query", defaults={"with_query": True}) @api_web_bp.route("/assiduites/group/query", defaults={"with_query": True})
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
def assiduites_group(with_query: bool = False): 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 = ass.to_dict(format_api=True)
data_set.get(data["etudid"]).append(data) data_set.get(data["etudid"]).append(data)
return jsonify(data_set) return data_set
@bp.route( @bp.route(
@ -271,6 +273,7 @@ def assiduites_group(with_query: bool = False):
) )
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
def assiduites_formsemestre(formsemestre_id: int, with_query: bool = False): def assiduites_formsemestre(formsemestre_id: int, with_query: bool = False):
"""Retourne toutes les assiduités du formsemestre""" """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 = ass.to_dict(format_api=True)
data_set.append(data) data_set.append(data)
return jsonify(data_set) return data_set
@bp.route( @bp.route(
@ -312,6 +315,7 @@ def assiduites_formsemestre(formsemestre_id: int, with_query: bool = False):
) )
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
def count_assiduites_formsemestre( def count_assiduites_formsemestre(
formsemestre_id: int = None, with_query: bool = False formsemestre_id: int = None, with_query: bool = False
@ -334,12 +338,13 @@ def count_assiduites_formsemestre(
if with_query: if with_query:
metric, filtered = _count_manager(request) 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/<int:etudid>/create", methods=["POST"]) @bp.route("/assiduite/<int:etudid>/create", methods=["POST"])
@api_web_bp.route("/assiduite/<int:etudid>/create", methods=["POST"]) @api_web_bp.route("/assiduite/<int:etudid>/create", methods=["POST"])
@scodoc @scodoc
@as_json
@login_required @login_required
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
@ -382,12 +387,13 @@ def assiduite_create(etudid: int = None):
db.session.commit() db.session.commit()
return jsonify({"errors": errors, "success": success}) return {"errors": errors, "success": success}
@bp.route("/assiduites/create", methods=["POST"]) @bp.route("/assiduites/create", methods=["POST"])
@api_web_bp.route("/assiduites/create", methods=["POST"]) @api_web_bp.route("/assiduites/create", methods=["POST"])
@scodoc @scodoc
@as_json
@login_required @login_required
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
@ -435,7 +441,7 @@ def assiduites_create():
else: else:
success[i] = obj success[i] = obj
return jsonify({"errors": errors, "success": success}) return {"errors": errors, "success": success}
def _create_singular( def _create_singular(
@ -515,6 +521,7 @@ def _create_singular(
@api_web_bp.route("/assiduite/delete", methods=["POST"]) @api_web_bp.route("/assiduite/delete", methods=["POST"])
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def assiduite_delete(): def assiduite_delete():
@ -543,7 +550,7 @@ def assiduite_delete():
else: else:
output["success"][f"{i}"] = {"OK": True} output["success"][f"{i}"] = {"OK": True}
db.session.commit() db.session.commit()
return jsonify(output) return output
def _delete_singular(assiduite_id: int, database): def _delete_singular(assiduite_id: int, database):
@ -558,6 +565,7 @@ def _delete_singular(assiduite_id: int, database):
@api_web_bp.route("/assiduite/<int:assiduite_id>/edit", methods=["POST"]) @api_web_bp.route("/assiduite/<int:assiduite_id>/edit", methods=["POST"])
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def assiduite_edit(assiduite_id: int): def assiduite_edit(assiduite_id: int):
@ -625,13 +633,14 @@ def assiduite_edit(assiduite_id: int):
db.session.add(assiduite_unique) db.session.add(assiduite_unique)
db.session.commit() db.session.commit()
return jsonify({"OK": True}) return {"OK": True}
@bp.route("/assiduites/edit", methods=["POST"]) @bp.route("/assiduites/edit", methods=["POST"])
@api_web_bp.route("/assiduites/edit", methods=["POST"]) @api_web_bp.route("/assiduites/edit", methods=["POST"])
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def assiduites_edit(): def assiduites_edit():
@ -666,7 +675,7 @@ def assiduites_edit():
db.session.commit() db.session.commit()
return jsonify({"errors": errors, "success": success}) return {"errors": errors, "success": success}
def _edit_singular(assiduite_unique, data): def _edit_singular(assiduite_unique, data):

View File

@ -7,6 +7,7 @@
""" """
from datetime import datetime from datetime import datetime
from flask_json import as_json
from flask import g, jsonify, request from flask import g, jsonify, request
from flask_login import login_required, current_user from flask_login import login_required, current_user
@ -57,6 +58,7 @@ def justificatif(justif_id: int = None):
@api_web_bp.route("/justificatifs/<int:etudid>/query", defaults={"with_query": True}) @api_web_bp.route("/justificatifs/<int:etudid>/query", defaults={"with_query": True})
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
def justificatifs(etudid: int = None, with_query: bool = False): 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 = just.to_dict(format_api=True)
data_set.append(data) data_set.append(data)
return jsonify(data_set) return data_set
@bp.route("/justificatif/<int:etudid>/create", methods=["POST"]) @bp.route("/justificatif/<int:etudid>/create", methods=["POST"])
@api_web_bp.route("/justificatif/<int:etudid>/create", methods=["POST"]) @api_web_bp.route("/justificatif/<int:etudid>/create", methods=["POST"])
@scodoc @scodoc
@login_required @login_required
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def justif_create(etudid: int = None): def justif_create(etudid: int = None):
@ -145,7 +148,7 @@ def justif_create(etudid: int = None):
else: else:
success[i] = obj success[i] = obj
compute_assiduites_justified(Justificatif.query.filter_by(etudid=etudid), True) compute_assiduites_justified(Justificatif.query.filter_by(etudid=etudid), True)
return jsonify({"errors": errors, "success": success}) return {"errors": errors, "success": success}
def _create_singular( def _create_singular(
@ -221,6 +224,7 @@ def _create_singular(
@api_web_bp.route("/justificatif/<int:justif_id>/edit", methods=["POST"]) @api_web_bp.route("/justificatif/<int:justif_id>/edit", methods=["POST"])
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def justif_edit(justif_id: int): def justif_edit(justif_id: int):
@ -296,23 +300,22 @@ def justif_edit(justif_id: int):
db.session.add(justificatif_unique) db.session.add(justificatif_unique)
db.session.commit() db.session.commit()
return jsonify( return {
{ "couverture": {
"couverture": { "avant": avant_ids,
"avant": avant_ids, "après": compute_assiduites_justified(
"après": compute_assiduites_justified( Justificatif.query.filter_by(etudid=justificatif_unique.etudid),
Justificatif.query.filter_by(etudid=justificatif_unique.etudid), True,
True, ),
),
}
} }
) }
@bp.route("/justificatif/delete", methods=["POST"]) @bp.route("/justificatif/delete", methods=["POST"])
@api_web_bp.route("/justificatif/delete", methods=["POST"]) @api_web_bp.route("/justificatif/delete", methods=["POST"])
@login_required @login_required
@scodoc @scodoc
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def justif_delete(): def justif_delete():
@ -342,7 +345,7 @@ def justif_delete():
output["success"][f"{i}"] = {"OK": True} output["success"][f"{i}"] = {"OK": True}
db.session.commit() db.session.commit()
return jsonify(output) return output
def _delete_singular(justif_id: int, database): def _delete_singular(justif_id: int, database):
@ -371,6 +374,7 @@ def _delete_singular(justif_id: int, database):
@api_web_bp.route("/justificatif/<int:justif_id>/import", methods=["POST"]) @api_web_bp.route("/justificatif/<int:justif_id>/import", methods=["POST"])
@scodoc @scodoc
@login_required @login_required
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def justif_import(justif_id: int = None): 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.add(justificatif_unique)
db.session.commit() db.session.commit()
return jsonify({"filename": fname}) return {"filename": fname}
except ScoValueError as err: except ScoValueError as err:
return json_error(404, err.args[0]) 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/<int:justif_id>/remove", methods=["POST"]) @api_web_bp.route("/justificatif/<int:justif_id>/remove", methods=["POST"])
@scodoc @scodoc
@login_required @login_required
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def justif_remove(justif_id: int = None): def justif_remove(justif_id: int = None):
@ -504,13 +509,14 @@ def justif_remove(justif_id: int = None):
except ScoValueError as err: except ScoValueError as err:
return json_error(404, err.args[0]) return json_error(404, err.args[0])
return jsonify({"response": "removed"}) return {"response": "removed"}
@bp.route("/justificatif/<int:justif_id>/list", methods=["GET"]) @bp.route("/justificatif/<int:justif_id>/list", methods=["GET"])
@api_web_bp.route("/justificatif/<int:justif_id>/list", methods=["GET"]) @api_web_bp.route("/justificatif/<int:justif_id>/list", methods=["GET"])
@scodoc @scodoc
@login_required @login_required
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def justif_list(justif_id: int = None): def justif_list(justif_id: int = None):
@ -534,7 +540,7 @@ def justif_list(justif_id: int = None):
archive_name, justificatif_unique.etudid archive_name, justificatif_unique.etudid
) )
return jsonify(filenames) return filenames
# Partie justification # Partie justification
@ -542,6 +548,7 @@ def justif_list(justif_id: int = None):
@api_web_bp.route("/justificatif/<int:justif_id>/justifies", methods=["GET"]) @api_web_bp.route("/justificatif/<int:justif_id>/justifies", methods=["GET"])
@scodoc @scodoc
@login_required @login_required
@as_json
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
# @permission_required(Permission.ScoAssiduiteChange) # @permission_required(Permission.ScoAssiduiteChange)
def justif_justifies(justif_id: int = None): 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) assiduites_list: list[int] = scass.justifies(justificatif_unique)
return jsonify(assiduites_list) return assiduites_list
# -- Utils -- # -- Utils --

View File

@ -11,7 +11,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = "dbcf2175e87f" revision = "dbcf2175e87f"
down_revision = "6520faf67508" down_revision = "b8df1b913c79"
branch_labels = None branch_labels = None
depends_on = None depends_on = None

View File

@ -395,7 +395,7 @@ def verifier_filtrage_justificatifs(etud: Identite, justificatifs: list[Justific
# Justifications des assiduites # 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" assert len(scass.justifies(justificatifs[0])) == 0, "Justifications mauvais"