From a1e689d1057109e276b44d6fccaaf8e2572923ee Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Fri, 29 Dec 2023 02:48:23 +0100 Subject: [PATCH] API: group_set_edt_id. +cosmetic --- app/api/partitions.py | 27 ++++++++++++++++++++++++ app/models/groups.py | 4 +++- app/static/css/partition_editor.css | 4 ++++ app/templates/scolar/partition_editor.j2 | 4 ++-- app/views/scodoc.py | 4 ++-- tests/api/test_api_partitions.py | 8 +++++++ 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/app/api/partitions.py b/app/api/partitions.py index 516fc417..268e64c9 100644 --- a/app/api/partitions.py +++ b/app/api/partitions.py @@ -394,6 +394,32 @@ def group_edit(group_id: int): return group.to_dict(with_partition=True) +@bp.route("/group//set_edt_id/", methods=["POST"]) +@api_web_bp.route("/group//set_edt_id/", methods=["POST"]) +@login_required +@scodoc +@permission_required(Permission.ScoView) +@as_json +def group_set_edt_id(group_id: int, edt_id: str): + """Set edt_id for this group. + Contrairement à /edit, peut-être changé pour toute partition + ou formsemestre non verrouillé. + """ + query = GroupDescr.query.filter_by(id=group_id) + if g.scodoc_dept: + query = ( + query.join(Partition).join(FormSemestre).filter_by(dept_id=g.scodoc_dept_id) + ) + group: GroupDescr = query.first_or_404() + if not group.partition.formsemestre.can_change_groups(): + return json_error(401, "opération non autorisée") + log(f"group_set_edt_id( {group_id}, '{edt_id}' )") + group.edt_id = edt_id + db.session.add(group) + db.session.commit() + return group.to_dict(with_partition=True) + + @bp.route("/formsemestre//partition/create", methods=["POST"]) @api_web_bp.route( "/formsemestre//partition/create", methods=["POST"] @@ -494,6 +520,7 @@ def formsemestre_order_partitions(formsemestre_id: int): db.session.commit() app.set_sco_dept(formsemestre.departement.acronym) sco_cache.invalidate_formsemestre(formsemestre_id) + log(f"formsemestre_order_partitions({partition_ids})") return [ partition.to_dict() for partition in formsemestre.partitions.order_by(Partition.numero) diff --git a/app/models/groups.py b/app/models/groups.py index 4df0b5d8..d4905cda 100644 --- a/app/models/groups.py +++ b/app/models/groups.py @@ -242,10 +242,12 @@ class GroupDescr(ScoDocModel): def to_dict(self, with_partition=True) -> dict: """as a dict, with or without partition""" + if with_partition: + partition_dict = self.partition.to_dict(with_groups=False) d = dict(self.__dict__) d.pop("_sa_instance_state", None) if with_partition: - d["partition"] = self.partition.to_dict(with_groups=False) + d["partition"] = partition_dict return d def get_edt_ids(self) -> list[str]: diff --git a/app/static/css/partition_editor.css b/app/static/css/partition_editor.css index 912f0d16..00bbb248 100644 --- a/app/static/css/partition_editor.css +++ b/app/static/css/partition_editor.css @@ -633,3 +633,7 @@ h3 { #zoneGroupes .groupe[data-idgroupe=aucun]>div:nth-child(1) { color: red; } + +#zonePartitions button span.editing:not(:first-child) { + margin-left: 8px; +} \ No newline at end of file diff --git a/app/templates/scolar/partition_editor.j2 b/app/templates/scolar/partition_editor.j2 index dbd58d17..0f6464b2 100644 --- a/app/templates/scolar/partition_editor.j2 +++ b/app/templates/scolar/partition_editor.j2 @@ -815,7 +815,7 @@ fetch(url, { method: "POST" }) .then(r => { return r.json() }) .then(r => { - if (r.OK != true) { + if (r.id != idGroupe) { document.querySelector("main").innerHTML = "

Une erreur s'est produite lors de la sauvegarde des données (5).

"; } }); @@ -870,7 +870,7 @@ .then(r => { return r.json() }) .then(r => { if (r.OK != true) { - document.querySelector("main").innerHTML = "

Une erreur s'est produite lors de la sauvegarde des données (5).

"; + document.querySelector("main").innerHTML = "

Une erreur s'est produite lors de la sauvegarde des données (6).

"; } listeGroupesAutoaffectation(); }) diff --git a/app/views/scodoc.py b/app/views/scodoc.py index d2d8acae..bd2a642e 100644 --- a/app/views/scodoc.py +++ b/app/views/scodoc.py @@ -391,11 +391,11 @@ def ics_raw_sample(edt_id: str): try: ics = raw_ics.decode(scu.SCO_ENCODING) except SyntaxError: - log(f"ics_raw_sample: raw_ics.decode failed") + log("ics_raw_sample: raw_ics.decode failed") return f"Erreur lors de la conversion vers {scu.SCO_ENCODING}" evs = ics.split("BEGIN:VEVENT") if len(evs) < 1: - log(f"ics_raw_sample: empty calendar") + log("ics_raw_sample: empty calendar") return "pas d'évènements VEVENT détectés dans ce fichier" return "BEGIN:VEVENT" + evs[len(evs) // 2] diff --git a/tests/api/test_api_partitions.py b/tests/api/test_api_partitions.py index b1a149f5..d4b4040e 100644 --- a/tests/api/test_api_partitions.py +++ b/tests/api/test_api_partitions.py @@ -120,6 +120,14 @@ def test_formsemestre_partition(api_headers): assert group["group_name"] == group_d["group_name"] assert group["edt_id"] == "GEDT2" + # Change edt_id via route dédiée: + group_t = POST_JSON( + f"/group/{group_r['id']}/set_edt_id/GEDT3", + headers=headers, + ) + assert group_t["id"] == group_r["id"] + assert group_t["edt_id"] == "GEDT3" + # Place un étudiant dans le groupe etud = GET(f"/formsemestre/{formsemestre_id}/etudiants", headers=headers)[0] repl = POST_JSON(f"/group/{group['id']}/set_etudiant/{etud['id']}", headers=headers)