From aa38ac0664985ff654882942d6b99f8ec50de63a Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sat, 2 Sep 2023 22:19:35 +0200 Subject: [PATCH] Remplace partout sco_permissions_check.can_change_groups() par FormSemestre.can_change_groups() --- app/scodoc/sco_formsemestre_status.py | 19 ++---- app/scodoc/sco_groups.py | 85 +++++++++++++-------------- app/scodoc/sco_groups_edit.py | 9 ++- app/views/scolar.py | 8 +-- 4 files changed, 54 insertions(+), 67 deletions(-) diff --git a/app/scodoc/sco_formsemestre_status.py b/app/scodoc/sco_formsemestre_status.py index 61053f7476..286bcf113c 100755 --- a/app/scodoc/sco_formsemestre_status.py +++ b/app/scodoc/sco_formsemestre_status.py @@ -73,7 +73,6 @@ from app.scodoc import sco_formsemestre from app.scodoc import sco_formsemestre_inscriptions from app.scodoc import sco_groups from app.scodoc import sco_moduleimpl -from app.scodoc import sco_permissions_check from app.scodoc import sco_preferences from app.scodoc import sco_users from app.scodoc.gen_tables import GenTable @@ -340,6 +339,7 @@ def formsemestre_status_menubar(formsemestre: FormSemestre) -> str: }, ] + can_change_groups = formsemestre.can_change_groups() menu_groupes = [ { "title": "Listes, photos, feuilles...", @@ -352,27 +352,20 @@ def formsemestre_status_menubar(formsemestre: FormSemestre) -> str: "title": "Modifier groupes et partitions", "endpoint": "scolar.partition_editor", "args": {"formsemestre_id": formsemestre_id}, - "enabled": sco_groups.sco_permissions_check.can_change_groups( - formsemestre_id - ), + "enabled": can_change_groups, "helpmsg": "Editeur de partitions", }, { "title": "Ancienne page édition partitions", "endpoint": "scolar.edit_partition_form", "args": {"formsemestre_id": formsemestre_id}, - "enabled": sco_groups.sco_permissions_check.can_change_groups( - formsemestre_id - ), + "enabled": can_change_groups, }, ] # 1 item / partition: partitions = sco_groups.get_partitions_list(formsemestre_id, with_default=False) submenu = [] - enabled = ( - sco_groups.sco_permissions_check.can_change_groups(formsemestre_id) - and partitions - ) + enabled = can_change_groups and partitions for partition in partitions: submenu.append( { @@ -921,7 +914,7 @@ def _make_listes_sem(formsemestre: FormSemestre, with_absences=True): H.append("") if partition_is_empty: H.append('

Aucun groupe peuplé dans cette partition') - if sco_groups.sco_permissions_check.can_change_groups(formsemestre.id): + if formsemestre.can_change_groups(): H.append( f""" (créer)""" ) H.append("

") - if sco_groups.sco_permissions_check.can_change_groups(formsemestre.id): + if formsemestre.can_change_groups(): H.append( f"""

GroupDescr: Obsolete: utiliser Partition.create_group """ partition = Partition.query.get_or_404(partition_id) - if not sco_permissions_check.can_change_groups(partition.formsemestre_id): + if not partition.formsemestre.can_change_groups(): raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") # if group_name: @@ -847,7 +841,7 @@ def delete_group(group_id, partition_id=None): if partition_id: if partition_id != group.partition_id: raise ValueError("inconsistent partition/group") - if not sco_permissions_check.can_change_groups(group.partition.formsemestre_id): + if not group.partition.formsemestre.can_change_groups(): raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") log(f"delete_group: group={group} partition={group.partition}") formsemestre = group.partition.formsemestre @@ -863,7 +857,8 @@ def partition_create( redirect=True, ): """Create a new partition""" - if not sco_permissions_check.can_change_groups(formsemestre_id): + formsemestre = FormSemestre.get_formsemestre(formsemestre_id) + if not formsemestre.can_change_groups(): raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") if partition_name: partition_name = str(partition_name).strip() @@ -924,9 +919,10 @@ def get_arrow_icons_tags(): def edit_partition_form(formsemestre_id=None): """Form to create/suppress partitions""" # ad-hoc form - if not sco_permissions_check.can_change_groups(formsemestre_id): - raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") formsemestre = FormSemestre.get_formsemestre(formsemestre_id) + if not formsemestre.can_change_groups(): + raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") + partitions = get_partitions_list(formsemestre_id) arrow_up, arrow_down, arrow_none = get_arrow_icons_tags() suppricon = scu.icontag( @@ -1093,21 +1089,22 @@ def edit_partition_form(formsemestre_id=None): def partition_set_attr(partition_id, attr, value): """Set partition attribute: bul_show_rank or show_in_lists""" if attr not in {"bul_show_rank", "show_in_lists"}: - raise ValueError("invalid partition attribute: %s" % attr) + raise ValueError(f"invalid partition attribute: {attr}") - partition = get_partition(partition_id) - formsemestre_id = partition["formsemestre_id"] - if not sco_permissions_check.can_change_groups(formsemestre_id): + partition = Partition.query.get_or_404(partition_id) + if not partition.formsemestre.can_change_groups(): raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") - log("partition_set_attr(%s, %s, %s)" % (partition_id, attr, value)) + log(f"partition_set_attr({partition_id}, {attr}, {value})") value = int(value) - - cnx = ndb.GetDBConnexion() - partition[attr] = value - partitionEditor.edit(cnx, partition) - # invalid bulletin cache - sco_cache.invalidate_formsemestre(formsemestre_id=partition["formsemestre_id"]) + if getattr(partition, attr, None) != value: + setattr(partition, attr, value) + db.session.add(partition) + db.session.commit() + # invalid bulletin cache + sco_cache.invalidate_formsemestre( + formsemestre_id=partition.formsemestre.id["formsemestre_id"] + ) return "enregistré" @@ -1118,9 +1115,9 @@ def partition_delete(partition_id, force=False, redirect=1, dialog_confirmed=Fal """ partition = get_partition(partition_id) formsemestre_id = partition["formsemestre_id"] - if not sco_permissions_check.can_change_groups(formsemestre_id): - raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") formsemestre = FormSemestre.get_formsemestre(formsemestre_id) + if not formsemestre.can_change_groups(): + raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") if not partition["partition_name"] and not force: raise ValueError("cannot suppress this partition") @@ -1163,7 +1160,8 @@ def partition_move(partition_id, after=0, redirect=1): """Move before/after previous one (decrement/increment numero)""" partition = get_partition(partition_id) formsemestre_id = partition["formsemestre_id"] - if not sco_permissions_check.can_change_groups(formsemestre_id): + formsemestre = FormSemestre.get_formsemestre(formsemestre_id) + if not formsemestre.can_change_groups(): raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") # redirect = int(redirect) @@ -1221,7 +1219,8 @@ def partition_rename(partition_id): """Form to rename a partition""" partition = get_partition(partition_id) formsemestre_id = partition["formsemestre_id"] - if not sco_permissions_check.can_change_groups(formsemestre_id): + formsemestre = FormSemestre.get_formsemestre(formsemestre_id) + if not formsemestre.can_change_groups(): raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") H = ["

Renommer une partition

"] tf = TrivialFormulator( @@ -1272,7 +1271,9 @@ def partition_set_name(partition_id, partition_name, redirect=1): if partition_name == scu.PARTITION_PARCOURS: raise ScoValueError(f"nom de partition {scu.PARTITION_PARCOURS} réservé.") formsemestre_id = partition["formsemestre_id"] - + formsemestre = FormSemestre.get_formsemestre(formsemestre_id) + if not formsemestre.can_change_groups(): + raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") # check unicity r = ndb.SimpleDictFetch( """SELECT p.* FROM partition p @@ -1285,9 +1286,6 @@ def partition_set_name(partition_id, partition_name, redirect=1): raise ScoValueError( f"Partition {partition_name} déjà existante dans ce semestre !" ) - - if not sco_permissions_check.can_change_groups(formsemestre_id): - raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") redirect = int(redirect) cnx = ndb.GetDBConnexion() partitionEditor.edit( @@ -1304,7 +1302,7 @@ def partition_set_name(partition_id, partition_name, redirect=1): def group_set_name(group: GroupDescr, group_name: str, redirect=True): """Set group name""" - if not sco_permissions_check.can_change_groups(group.partition.formsemestre.id): + if not group.partition.formsemestre.can_change_groups(): raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") if group.group_name is None: raise ValueError("can't set a name to default group") @@ -1337,7 +1335,8 @@ def group_rename(group_id): """Form to rename a group""" group = GroupDescr.query.get_or_404(group_id) formsemestre_id = group.partition.formsemestre_id - if not sco_permissions_check.can_change_groups(formsemestre_id): + formsemestre = FormSemestre.get_formsemestre(formsemestre_id) + if not formsemestre.can_change_groups(): raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") H = [f"

Renommer un groupe de {group.partition.partition_name or '-'}

"] tf = TrivialFormulator( @@ -1395,7 +1394,7 @@ def groups_auto_repartition(partition_id=None): scodoc_dept=g.scodoc_dept, formsemestre_id=formsemestre_id, ) - if not sco_permissions_check.can_change_groups(formsemestre_id): + if not formsemestre.can_change_groups(): raise AccessDenied("Vous n'avez pas le droit d'effectuer cette opération !") descr = [ diff --git a/app/scodoc/sco_groups_edit.py b/app/scodoc/sco_groups_edit.py index 5caba3beb3..4fd1e103cb 100644 --- a/app/scodoc/sco_groups_edit.py +++ b/app/scodoc/sco_groups_edit.py @@ -31,7 +31,6 @@ from flask import render_template from app.models import Partition from app.scodoc import html_sco_header -from app.scodoc import sco_groups from app.scodoc.sco_exceptions import AccessDenied @@ -41,10 +40,10 @@ def affect_groups(partition_id): """ # réécrit pour 9.0.47 avec un template partition = Partition.query.get_or_404(partition_id) - formsemestre_id = partition.formsemestre_id - if not sco_groups.sco_permissions_check.can_change_groups(formsemestre_id): + formsemestre = partition.formsemestre + if not formsemestre.can_change_groups(): raise AccessDenied("vous n'avez pas la permission de modifier les groupes") - partition.formsemestre.setup_parcours_groups() + formsemestre.setup_parcours_groups() return render_template( "scolar/affect_groups.j2", sco_header=html_sco_header.sco_header( @@ -58,5 +57,5 @@ def affect_groups(partition_id): partitions_list=partition.formsemestre.partitions.filter( Partition.partition_name != None ), - formsemestre_id=formsemestre_id, + formsemestre_id=formsemestre.id, ) diff --git a/app/views/scolar.py b/app/views/scolar.py index e63522bc2b..b8a4466e71 100644 --- a/app/views/scolar.py +++ b/app/views/scolar.py @@ -923,15 +923,11 @@ def partition_editor(formsemestre_id: int, edit_partition=False): page_title=f"Partitions de {formsemestre.titre_annee()}", init_datatables=True, ), - f"""

-

- """, + """

""", render_template( "scolar/partition_editor.j2", formsemestre=formsemestre, - read_only=not sco_groups.sco_permissions_check.can_change_groups( - formsemestre_id - ), + read_only=not formsemestre.can_change_groups(), edit_partition=edit_partition, ), html_sco_header.sco_footer(),