From c870f6871cf4f897844c0bbd83a7b4710e0ea621 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 13 Dec 2017 18:35:16 +0100 Subject: [PATCH] Renomme la fonction can_delete_set --- re2o/utils.py | 2 +- users/forms.py | 19 +++++++++++++++---- users/views.py | 8 ++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/re2o/utils.py b/re2o/utils.py index 0deacc63..9c76d16e 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -123,7 +123,7 @@ def can_delete(model): return decorator -def all_can_delete(model): +def can_delete_set(model): """Decorator which returns a list of detable models by request user. If none of them, return an error""" def decorator(view): diff --git a/users/forms.py b/users/forms.py index a65255d5..92f63068 100644 --- a/users/forms.py +++ b/users/forms.py @@ -479,24 +479,35 @@ class NewListRightForm(ListRightForm): class DelListRightForm(Form): """Suppression d'un ou plusieurs groupes""" listrights = forms.ModelMultipleChoiceField( - queryset=ListRight.objects.all(), + queryset=ListRight.objects.none(), label="Droits actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelListRightForm, self).__init__(*args, **kwargs) + if instances: + self.fields['listrights'].queryset = instances + else: + self.fields['listrights'].queryset = ListRight.objects.all() + class DelSchoolForm(Form): """Suppression d'une ou plusieurs écoles""" schools = forms.ModelMultipleChoiceField( - queryset=School.objects.all(), + queryset=School.objects.none(), label="Etablissements actuels", widget=forms.CheckboxSelectMultiple ) def __init__(self, *args, **kwargs): - instances = kwargs.pop('instances') + instances = kwargs.pop('instances', None) super(DelSchoolForm, self).__init__(*args, **kwargs) - self.fields['schools'].queryset = instances + if instances: + self.fields['schools'].queryset = instances + else: + self.fields['schools'].queryset = School.objects.all() class RightForm(ModelForm): diff --git a/users/views.py b/users/views.py index 1a662ae0..3462c97c 100644 --- a/users/views.py +++ b/users/views.py @@ -93,7 +93,7 @@ from preferences.models import OptionalUser, GeneralOption from re2o.views import form from re2o.utils import ( - all_has_access, SortTable, can_create, can_edit, all_can_delete, can_delete, can_view + all_has_access, SortTable, can_create, can_edit, can_delete_set, can_delete, can_view ) def password_change_action(u_form, user, request, req=False): @@ -500,7 +500,7 @@ def edit_school(request, school_instance, schoolid): @login_required -@all_can_delete(School) +@can_delete_set(School) def del_school(request, instances): """ Supprimer un établissement d'enseignement à la base de donnée, need cableur @@ -562,11 +562,11 @@ def edit_listright(request, listright_instance, listrightid): @login_required -@permission_required('bureau') +@can_delete_set(ListRight) def del_listright(request): """ Supprimer un ou plusieurs groupe, possible si il est vide, need droit bureau """ - listright = DelListRightForm(request.POST or None) + listright = DelListRightForm(request.POST or None, instances=instances) if listright.is_valid(): listright_dels = listright.cleaned_data['listrights'] for listright_del in listright_dels: