API: ajout /group/<int:group_id>/remove_etudiant/<int:etudid> + cache invals

This commit is contained in:
Emmanuel Viennet 2022-07-25 08:46:24 +02:00
parent 692c5b0369
commit 59d6205777
2 changed files with 43 additions and 10 deletions

View File

@ -148,6 +148,19 @@ def set_etud_group(etudid: int, group_id: int):
group.etuds.append(etud) group.etuds.append(etud)
log(f"set_etud_group({etud}, {group})") log(f"set_etud_group({etud}, {group})")
db.session.commit() db.session.commit()
sco_cache.invalidate_formsemestre(group.partition.formsemestre_id)
return jsonify({"group_id": group_id, "etudid": etudid})
@bp.route("/group/<int:group_id>/remove_etudiant/<int:etudid>", 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}) 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) @permission_required_api(Permission.ScoEtudChangeGroups, Permission.APIEditGroups)
def partition_remove_etud(partition_id: int, etudid: int): 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) etud = Identite.query.get_or_404(etudid)
partition = Partition.query.get_or_404(partition_id)
groups = ( groups = (
GroupDescr.query.filter_by(partition_id=partition_id) GroupDescr.query.filter_by(partition_id=partition_id)
.join(group_membership) .join(group_membership)
@ -166,6 +182,7 @@ def partition_remove_etud(partition_id: int, etudid: int):
for g in groups: for g in groups:
g.etuds.remove(etud) g.etuds.remove(etud)
db.session.commit() db.session.commit()
sco_cache.invalidate_formsemestre(partition.formsemestre_id)
return jsonify({"partition_id": partition_id, "etudid": etudid}) return jsonify({"partition_id": partition_id, "etudid": etudid})
@ -302,6 +319,7 @@ def formsemestre_order_partitions(formsemestre_id: int):
p.numero = numero p.numero = numero
db.session.add(p) db.session.add(p)
db.session.commit() db.session.commit()
sco_cache.invalidate_formsemestre(formsemestre_id)
return jsonify(formsemestre.to_dict()) return jsonify(formsemestre.to_dict())
@ -325,6 +343,7 @@ def partition_order_groups(partition_id: int):
group.numero = numero group.numero = numero
db.session.add(group) db.session.add(group)
db.session.commit() db.session.commit()
sco_cache.invalidate_formsemestre(partition.formsemestre_id)
return jsonify(partition.to_dict(with_groups=True)) return jsonify(partition.to_dict(with_groups=True))

View File

@ -59,7 +59,7 @@ def GET(path: str, headers={}, errmsg=None):
"""Get and returns as JSON""" """Get and returns as JSON"""
r = requests.get(API_URL + path, headers=headers or HEADERS, verify=CHK_CERT) r = requests.get(API_URL + path, headers=headers or HEADERS, verify=CHK_CERT)
if r.status_code != 200: 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 return r.json() # decode la reponse JSON
@ -72,7 +72,7 @@ def POST(path: str, data: dict = {}, headers={}, errmsg=None):
verify=CHK_CERT, verify=CHK_CERT,
) )
if r.status_code != 200: 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 return r.json() # decode la reponse JSON
@ -85,7 +85,7 @@ def POST_JSON(path: str, data: dict = {}, headers={}, errmsg=None):
verify=CHK_CERT, verify=CHK_CERT,
) )
if r.status_code != 200: 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 return r.json() # decode la reponse JSON
@ -169,17 +169,31 @@ pp(partitions)
POST_JSON(f"/group/5559/delete") POST_JSON(f"/group/5559/delete")
POST_JSON(f"/group/5327/edit", data={"group_name": "TDXXX"}) 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", f"/formsemestre/{formsemestre_id}/partition/create",
data={"partition_name": "PXXXXYY"}, data={"partition_name": "PART"},
) )
partition_id = js["id"]
POST_JSON( POST_JSON(
f"/partition/{2379}/edit", f"/partition/{partition_id}/edit",
data={"partition_name": "---PPPP", "show_in_lists": True}, 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( POST_JSON(