From f3ceaff30762896f3ed6245194ebb5620de1c0ca Mon Sep 17 00:00:00 2001 From: iziram Date: Thu, 27 Jul 2023 18:00:40 +0200 Subject: [PATCH] Assiduites : API - changement retour batch --- app/api/assiduites.py | 43 +++++++++++++++++------------ app/api/justificatifs.py | 16 +++++------ tests/api/test_api_assiduites.py | 28 +++++++++---------- tests/api/test_api_justificatifs.py | 24 ++++++++-------- 4 files changed, 60 insertions(+), 51 deletions(-) diff --git a/app/api/assiduites.py b/app/api/assiduites.py index ccd35a30..d8d45462 100644 --- a/app/api/assiduites.py +++ b/app/api/assiduites.py @@ -442,14 +442,14 @@ def assiduite_create(etudid: int = None, nip=None, ine=None): if not isinstance(create_list, list): return json_error(404, "Le contenu envoyé n'est pas une liste") - errors: dict[int, str] = {} - success: dict[int, object] = {} + errors: list = [] + success: list = [] for i, data in enumerate(create_list): code, obj = _create_singular(data, etud) if code == 404: - errors[i] = obj + errors.append({"indice": i, "message": obj}) else: - success[i] = obj + success.append({"indice": i, "message": obj}) scass.simple_invalidate_cache(data, etud.id) db.session.commit() @@ -493,19 +493,19 @@ def assiduites_create(): if not isinstance(create_list, list): return json_error(404, "Le contenu envoyé n'est pas une liste") - errors: dict[int, str] = {} - success: dict[int, object] = {} + errors: list = [] + success: list = [] for i, data in enumerate(create_list): etud: Identite = Identite.query.filter_by(id=data["etudid"]).first() if etud is None: - errors[i] = "Cet étudiant n'existe pas." + errors.append({"indice": i, "message": "Cet étudiant n'existe pas."}) continue code, obj = _create_singular(data, etud) if code == 404: - errors[i] = obj + errors.append({"indice": i, "message": obj}) else: - success[i] = obj + success.append({"indice": i, "message": obj}) scass.simple_invalidate_cache(data) return {"errors": errors, "success": success} @@ -607,14 +607,14 @@ def assiduite_delete(): if not isinstance(assiduites_list, list): return json_error(404, "Le contenu envoyé n'est pas une liste") - output = {"errors": {}, "success": {}} + output = {"errors": [], "success": []} for i, ass in enumerate(assiduites_list): code, msg = _delete_singular(ass, db) if code == 404: - output["errors"][f"{i}"] = msg + output["errors"].append({"indice": i, "message": msg}) else: - output["success"][f"{i}"] = {"OK": True} + output["success"].append({"indice": i, "message": "OK"}) db.session.commit() return output @@ -743,19 +743,28 @@ def assiduites_edit(): if not isinstance(edit_list, list): return json_error(404, "Le contenu envoyé n'est pas une liste") - errors: dict[int, str] = {} - success: dict[int, object] = {} + errors: list[dict] = [] + success: list[dict] = [] for i, data in enumerate(edit_list): assi: Identite = Assiduite.query.filter_by(id=data["assiduite_id"]).first() if assi is None: - errors[i] = f"assiduité {data['assiduite_id']} n'existe pas." + errors.append( + { + "indice": i, + "message": f"assiduité {data['assiduite_id']} n'existe pas.", + } + ) continue code, obj = _edit_singular(assi, data) + obj_retour = { + "indice": i, + "message": obj, + } if code == 404: - errors[i] = obj + errors.append(obj_retour) else: - success[i] = obj + success.append(obj_retour) db.session.commit() diff --git a/app/api/justificatifs.py b/app/api/justificatifs.py index c09a2e68..78c751b4 100644 --- a/app/api/justificatifs.py +++ b/app/api/justificatifs.py @@ -154,7 +154,7 @@ def justificatifs_dept(dept_id: int = None, with_query: bool = False): @login_required @as_json @permission_required(Permission.ScoAbsChange) -def justif_create(etudid: int = None, nip = None, ine = None): +def justif_create(etudid: int = None, nip=None, ine=None): """ Création d'un justificatif pour l'étudiant (etudid) La requête doit avoir un content type "application/json": @@ -187,14 +187,14 @@ def justif_create(etudid: int = None, nip = None, ine = None): if not isinstance(create_list, list): return json_error(404, "Le contenu envoyé n'est pas une liste") - errors: dict[int, str] = {} - success: dict[int, object] = {} + errors: list = [] + success: list = [] for i, data in enumerate(create_list): code, obj = _create_singular(data, etud) if code == 404: - errors[i] = obj + errors.append({"indice": i, "message": obj}) else: - success[i] = obj + success.append({"indice": i, "message": obj}) scass.simple_invalidate_cache(data, etud.id) compute_assiduites_justified(Justificatif.query.filter_by(etudid=etudid), True) return {"errors": errors, "success": success} @@ -383,14 +383,14 @@ def justif_delete(): if not isinstance(justificatifs_list, list): return json_error(404, "Le contenu envoyé n'est pas une liste") - output = {"errors": {}, "success": {}} + output = {"errors": [], "success": []} for i, ass in enumerate(justificatifs_list): code, msg = _delete_singular(ass, db) if code == 404: - output["errors"][f"{i}"] = msg + output["errors"].append({"indice": i, "message": msg}) else: - output["success"][f"{i}"] = {"OK": True} + output["success"].append({"indice": i, "message": "OK"}) db.session.commit() diff --git a/tests/api/test_api_assiduites.py b/tests/api/test_api_assiduites.py index 37ca9bd8..33a151ce 100644 --- a/tests/api/test_api_assiduites.py +++ b/tests/api/test_api_assiduites.py @@ -35,7 +35,7 @@ ASSIDUITES_FIELDS = { } CREATE_FIELD = {"assiduite_id": int} -BATCH_FIELD = {"errors": dict, "success": dict} +BATCH_FIELD = {"errors": list, "success": list} COUNT_FIELDS = {"compte": int, "journee": int, "demi": int, "heure": float} @@ -262,14 +262,14 @@ def test_route_create(api_admin_headers): check_fields(res, BATCH_FIELD) assert len(res["success"]) == 1 - TO_REMOVE.append(res["success"]["0"]["assiduite_id"]) + TO_REMOVE.append(res["success"][0]["message"]["assiduite_id"]) data2 = create_data("absent", "02", MODULE, "desc") res = POST_JSON(f"/assiduite/{ETUDID}/create", [data2], api_admin_headers) check_fields(res, BATCH_FIELD) assert len(res["success"]) == 1 - TO_REMOVE.append(res["success"]["0"]["assiduite_id"]) + TO_REMOVE.append(res["success"][0]["message"]["assiduite_id"]) # Mauvais fonctionnement check_failure_post(f"/assiduite/{FAUX}/create", api_admin_headers, [data]) @@ -278,7 +278,7 @@ def test_route_create(api_admin_headers): check_fields(res, BATCH_FIELD) assert len(res["errors"]) == 1 assert ( - res["errors"]["0"] + res["errors"][0]["message"] == "Duplication des assiduités (la période rentrée rentre en conflit avec une assiduité enregistrée)" ) @@ -289,7 +289,7 @@ def test_route_create(api_admin_headers): ) check_fields(res, BATCH_FIELD) assert len(res["errors"]) == 1 - assert res["errors"]["0"] == "param 'moduleimpl_id': invalide" + assert res["errors"][0]["message"] == "param 'moduleimpl_id': invalide" # -== Multiple ==- @@ -304,8 +304,8 @@ def test_route_create(api_admin_headers): res = POST_JSON(f"/assiduite/{ETUDID}/create", data, api_admin_headers) check_fields(res, BATCH_FIELD) for dat in res["success"]: - check_fields(res["success"][dat], CREATE_FIELD) - TO_REMOVE.append(res["success"][dat]["assiduite_id"]) + check_fields(dat["message"], CREATE_FIELD) + TO_REMOVE.append(dat["message"]["assiduite_id"]) # Mauvais Fonctionnement @@ -321,13 +321,13 @@ def test_route_create(api_admin_headers): assert len(res["errors"]) == 4 assert ( - res["errors"]["0"] + res["errors"][0]["message"] == "Duplication des assiduités (la période rentrée rentre en conflit avec une assiduité enregistrée)" ) - assert res["errors"]["1"] == "param 'moduleimpl_id': invalide" - assert res["errors"]["2"] == "param 'etat': invalide" + assert res["errors"][1]["message"] == "param 'moduleimpl_id': invalide" + assert res["errors"][2]["message"] == "param 'etat': invalide" assert ( - res["errors"]["3"] + res["errors"][3]["message"] == "param 'date_debut': format invalide, param 'date_fin': format invalide" ) @@ -367,7 +367,7 @@ def test_route_delete(api_admin_headers): res = POST_JSON("/assiduite/delete", [data], api_admin_headers) check_fields(res, BATCH_FIELD) for dat in res["success"]: - assert res["success"][dat] == {"OK": True} + assert dat["message"] == "OK" # Mauvais fonctionnement res = POST_JSON("/assiduite/delete", [data], api_admin_headers) @@ -383,7 +383,7 @@ def test_route_delete(api_admin_headers): res = POST_JSON("/assiduite/delete", data, api_admin_headers) check_fields(res, BATCH_FIELD) for dat in res["success"]: - assert res["success"][dat] == {"OK": True} + assert dat["message"] == "OK" # Mauvais Fonctionnement @@ -397,4 +397,4 @@ def test_route_delete(api_admin_headers): check_fields(res, BATCH_FIELD) assert len(res["errors"]) == 3 - assert all([res["errors"][i] == "Assiduite non existante" for i in res["errors"]]) + assert all(i["message"] == "Assiduite non existante" for i in res["errors"]) diff --git a/tests/api/test_api_justificatifs.py b/tests/api/test_api_justificatifs.py index 68f65792..33c37101 100644 --- a/tests/api/test_api_justificatifs.py +++ b/tests/api/test_api_justificatifs.py @@ -35,7 +35,7 @@ JUSTIFICATIFS_FIELDS = { } CREATE_FIELD = {"justif_id": int, "couverture": list} -BATCH_FIELD = {"errors": dict, "success": dict} +BATCH_FIELD = {"errors": list, "success": list} TO_REMOVE = [] @@ -172,14 +172,14 @@ def test_route_create(api_admin_headers): check_fields(res, BATCH_FIELD) assert len(res["success"]) == 1 - TO_REMOVE.append(res["success"]["0"]["justif_id"]) + TO_REMOVE.append(res["success"][0]["message"]["justif_id"]) data2 = create_data("modifie", "02", "raison") res = POST_JSON(f"/justificatif/{ETUDID}/create", [data2], api_admin_headers) check_fields(res, BATCH_FIELD) assert len(res["success"]) == 1 - TO_REMOVE.append(res["success"]["0"]["justif_id"]) + TO_REMOVE.append(res["success"][0]["message"]["justif_id"]) # Mauvais fonctionnement check_failure_post(f"/justificatif/{FAUX}/create", api_admin_headers, [data]) @@ -191,7 +191,7 @@ def test_route_create(api_admin_headers): ) check_fields(res, BATCH_FIELD) assert len(res["errors"]) == 1 - assert res["errors"]["0"] == "param 'etat': invalide" + assert res["errors"][0]["message"] == "param 'etat': invalide" # -== Multiple ==- @@ -206,8 +206,8 @@ def test_route_create(api_admin_headers): res = POST_JSON(f"/justificatif/{ETUDID}/create", data, api_admin_headers) check_fields(res, BATCH_FIELD) for dat in res["success"]: - check_fields(res["success"][dat], CREATE_FIELD) - TO_REMOVE.append(res["success"][dat]["justif_id"]) + check_fields(dat["message"], CREATE_FIELD) + TO_REMOVE.append(dat["message"]["justif_id"]) # Mauvais Fonctionnement @@ -221,10 +221,10 @@ def test_route_create(api_admin_headers): check_fields(res, BATCH_FIELD) assert len(res["errors"]) == 3 - assert res["errors"]["0"] == "param 'etat': manquant" - assert res["errors"]["1"] == "param 'etat': invalide" + assert res["errors"][0]["message"] == "param 'etat': manquant" + assert res["errors"][1]["message"] == "param 'etat': invalide" assert ( - res["errors"]["2"] + res["errors"][2]["message"] == "param 'date_debut': format invalide, param 'date_fin': format invalide" ) @@ -263,7 +263,7 @@ def test_route_delete(api_admin_headers): res = POST_JSON("/justificatif/delete", [data], api_admin_headers) check_fields(res, BATCH_FIELD) for dat in res["success"]: - assert res["success"][dat] == {"OK": True} + assert dat["message"] == "OK" # Mauvais fonctionnement res = POST_JSON("/justificatif/delete", [data], api_admin_headers) @@ -279,7 +279,7 @@ def test_route_delete(api_admin_headers): res = POST_JSON("/justificatif/delete", data, api_admin_headers) check_fields(res, BATCH_FIELD) for dat in res["success"]: - assert res["success"][dat] == {"OK": True} + assert dat["message"] == "OK" # Mauvais Fonctionnement @@ -293,7 +293,7 @@ def test_route_delete(api_admin_headers): check_fields(res, BATCH_FIELD) assert len(res["errors"]) == 3 - assert all([res["errors"][i] == "Justificatif non existant" for i in res["errors"]]) + assert all(i["message"] == "Justificatif non existant" for i in res["errors"]) # Gestion de l'archivage