From 59d6205777b8ccf2c04ae4811d3cafca9664a0b5 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 25 Jul 2022 08:46:24 +0200 Subject: [PATCH] API: ajout /group//remove_etudiant/ + cache invals --- app/api/partitions.py | 21 ++++++++++++++++++++- tests/api/exemple-api-basic.py | 32 +++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/app/api/partitions.py b/app/api/partitions.py index 886a0d82b..20b92ce30 100644 --- a/app/api/partitions.py +++ b/app/api/partitions.py @@ -148,6 +148,19 @@ def set_etud_group(etudid: int, group_id: int): group.etuds.append(etud) log(f"set_etud_group({etud}, {group})") db.session.commit() + sco_cache.invalidate_formsemestre(group.partition.formsemestre_id) + return jsonify({"group_id": group_id, "etudid": etudid}) + + +@bp.route("/group//remove_etudiant/", methods=["POST"]) +@permission_required_api(Permission.ScoEtudChangeGroups, Permission.APIEditGroups) +def group_remove_etud(group_id: int, etudid: int): + """Retire l'étudiant de ce groupe. S'il n'y est pas, ne fait rien.""" + etud = Identite.query.get_or_404(etudid) + group = GroupDescr.query.get_or_404(group_id) + group.etuds.remove(etud) + db.session.commit() + sco_cache.invalidate_formsemestre(group.partition.formsemestre_id) return jsonify({"group_id": group_id, "etudid": etudid}) @@ -156,8 +169,11 @@ def set_etud_group(etudid: int, group_id: int): ) @permission_required_api(Permission.ScoEtudChangeGroups, Permission.APIEditGroups) def partition_remove_etud(partition_id: int, etudid: int): - """ """ + """Enlève l'étudiant de tous les groupes de cette partition + (NB: en principe, un étudiant ne doit être que dans 0 ou 1 groupe d'une partition) + """ etud = Identite.query.get_or_404(etudid) + partition = Partition.query.get_or_404(partition_id) groups = ( GroupDescr.query.filter_by(partition_id=partition_id) .join(group_membership) @@ -166,6 +182,7 @@ def partition_remove_etud(partition_id: int, etudid: int): for g in groups: g.etuds.remove(etud) db.session.commit() + sco_cache.invalidate_formsemestre(partition.formsemestre_id) return jsonify({"partition_id": partition_id, "etudid": etudid}) @@ -302,6 +319,7 @@ def formsemestre_order_partitions(formsemestre_id: int): p.numero = numero db.session.add(p) db.session.commit() + sco_cache.invalidate_formsemestre(formsemestre_id) return jsonify(formsemestre.to_dict()) @@ -325,6 +343,7 @@ def partition_order_groups(partition_id: int): group.numero = numero db.session.add(group) db.session.commit() + sco_cache.invalidate_formsemestre(partition.formsemestre_id) return jsonify(partition.to_dict(with_groups=True)) diff --git a/tests/api/exemple-api-basic.py b/tests/api/exemple-api-basic.py index 9d03946b2..b03b9ff64 100644 --- a/tests/api/exemple-api-basic.py +++ b/tests/api/exemple-api-basic.py @@ -59,7 +59,7 @@ def GET(path: str, headers={}, errmsg=None): """Get and returns as JSON""" r = requests.get(API_URL + path, headers=headers or HEADERS, verify=CHK_CERT) if r.status_code != 200: - raise ScoError(errmsg or f"""erreur status={r.status_code} !\n{r.json()}""") + raise ScoError(errmsg or f"""erreur status={r.status_code} !\n{r.text}""") return r.json() # decode la reponse JSON @@ -72,7 +72,7 @@ def POST(path: str, data: dict = {}, headers={}, errmsg=None): verify=CHK_CERT, ) if r.status_code != 200: - raise ScoError(errmsg or f"erreur status={r.status_code} !\n{r.json()}") + raise ScoError(errmsg or f"erreur status={r.status_code} !\n{r.text}") return r.json() # decode la reponse JSON @@ -85,7 +85,7 @@ def POST_JSON(path: str, data: dict = {}, headers={}, errmsg=None): verify=CHK_CERT, ) if r.status_code != 200: - raise ScoError(errmsg or f"erreur status={r.status_code} !\n{r.json()}") + raise ScoError(errmsg or f"erreur status={r.status_code} !\n{r.text}") return r.json() # decode la reponse JSON @@ -169,17 +169,31 @@ pp(partitions) POST_JSON(f"/group/5559/delete") POST_JSON(f"/group/5327/edit", data={"group_name": "TDXXX"}) -POST_JSON( +# 1- Crée une partition, puis la change de nom +js = POST_JSON( f"/formsemestre/{formsemestre_id}/partition/create", - data={"partition_name": "PXXXXYY"}, + data={"partition_name": "PART"}, ) - +partition_id = js["id"] POST_JSON( - f"/partition/{2379}/edit", - data={"partition_name": "---PPPP", "show_in_lists": True}, + f"/partition/{partition_id}/edit", + data={"partition_name": "PART1", "show_in_lists": True}, ) -POST_JSON(f"/partition/{2379}/delete") +# 2- Crée un groupe +js = POST_JSON(f"/partition/{partition_id}/group/create", data={"group_name": "GG"}) +group_id = js["id"] +# Prend un étudiant au hasard dans le semestre +etud = GET(f"/formsemestre/{formsemestre_id}/etudiants")[10] +etudid = etud["id"] +# 3- Affecte étudiant au groupe +POST_JSON(f"/group/{group_id}/set_etudiant/{etudid}") + +# 4- retire du groupe +POST_JSON(f"/group/{group_id}/remove_etudiant/{etudid}") + +# Suppression +POST_JSON(f"/partition/{partition_id}/delete") # POST_JSON(