From 6903c0dfde82a0618b62946d05dce1d8463eab6f Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 13 Dec 2017 18:56:16 +0100 Subject: [PATCH] Can_delete_set sur preferences et cotisations --- cotisations/forms.py | 30 +++++++++++++++++++++++++++--- cotisations/views.py | 27 +++++++++++++++++---------- preferences/forms.py | 10 +++++++++- preferences/views.py | 8 ++++---- 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/cotisations/forms.py b/cotisations/forms.py index 7725016c..d72241eb 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -180,11 +180,19 @@ class DelArticleForm(Form): """Suppression d'un ou plusieurs articles en vente. Choix parmis les modèles""" articles = forms.ModelMultipleChoiceField( - queryset=Article.objects.all(), + queryset=Article.objects.none(), label="Articles actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelArticleForm, self).__init__(*args, **kwargs) + if instances: + self.fields['articles'].queryset = instances + else: + self.fields['articles'].queryset = Article.objects.all() + class PaiementForm(ModelForm): """Creation d'un moyen de paiement, champ text moyen et type @@ -204,11 +212,19 @@ class DelPaiementForm(Form): """Suppression d'un ou plusieurs moyens de paiements, selection parmis les models""" paiements = forms.ModelMultipleChoiceField( - queryset=Paiement.objects.all(), + queryset=Paiement.objects.none(), label="Moyens de paiement actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelPaiementForm, self).__init__(*args, **kwargs) + if instances: + self.fields['paiements'].queryset = instances + else: + self.fields['paiements'].queryset = Paiement.objects.all() + class BanqueForm(ModelForm): """Creation d'une banque, field name""" @@ -225,7 +241,15 @@ class BanqueForm(ModelForm): class DelBanqueForm(Form): """Selection d'une ou plusieurs banques, pour suppression""" banques = forms.ModelMultipleChoiceField( - queryset=Banque.objects.all(), + queryset=Banque.objects.none(), label="Banques actuelles", widget=forms.CheckboxSelectMultiple ) + + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelBanqueForm, self).__init__(*args, **kwargs) + if instances: + self.fields['banques'].queryset = instances + else: + self.fields['banques'].queryset = Banque.objects.all() diff --git a/cotisations/views.py b/cotisations/views.py index e7405d0f..06d3dc9d 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -43,7 +43,14 @@ from users.models import User from re2o.settings import LOGO_PATH from re2o import settings from re2o.views import form -from re2o.utils import SortTable, can_create, can_edit, can_delete, can_view +from re2o.utils import ( + SortTable, + can_create, + can_edit, + can_delete, + can_view, + can_delete_set +) from preferences.models import OptionalUser, AssoOption, GeneralOption from .models import Facture, Article, Vente, Paiement, Banque from .forms import ( @@ -374,10 +381,10 @@ def edit_article(request, article_instance, articleid): @login_required -@permission_required('tresorier') -def del_article(request): +@can_delete_set(Article) +def del_article(request, instances): """Suppression d'un article en vente""" - article = DelArticleForm(request.POST or None) + article = DelArticleForm(request.POST or None, instances=instances) if article.is_valid(): article_del = article.cleaned_data['articles'] with transaction.atomic(), reversion.create_revision(): @@ -424,10 +431,10 @@ def edit_paiement(request, paiement_instance, paiementid): @login_required -@permission_required('tresorier') -def del_paiement(request): +@can_delete_set(Paiement) +def del_paiement(request, instances): """Suppression d'un moyen de paiement""" - paiement = DelPaiementForm(request.POST or None) + paiement = DelPaiementForm(request.POST or None, instances=instances) if paiement.is_valid(): paiement_dels = paiement.cleaned_data['paiements'] for paiement_del in paiement_dels: @@ -485,10 +492,10 @@ def edit_banque(request, banque_instance, banqueid): @login_required -@permission_required('tresorier') -def del_banque(request): +@can_delete_set(Banque) +def del_banque(request, instances): """Supprime une banque""" - banque = DelBanqueForm(request.POST or None) + banque = DelBanqueForm(request.POST or None, instances=instances) if banque.is_valid(): banque_dels = banque.cleaned_data['banques'] for banque_del in banque_dels: diff --git a/preferences/forms.py b/preferences/forms.py index 51cbb885..7dda8620 100644 --- a/preferences/forms.py +++ b/preferences/forms.py @@ -173,7 +173,15 @@ class ServiceForm(ModelForm): class DelServiceForm(Form): """Suppression de services sur la page d'accueil""" services = forms.ModelMultipleChoiceField( - queryset=Service.objects.all(), + queryset=Service.objects.none(), label="Enregistrements service actuels", widget=forms.CheckboxSelectMultiple ) + + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelServiceForm, self).__init__(*args, **kwargs) + if instances: + self.fields['services'].queryset = instances + else: + self.fields['services'].queryset = Service.objects.all() diff --git a/preferences/views.py b/preferences/views.py index c568a8e9..0c905762 100644 --- a/preferences/views.py +++ b/preferences/views.py @@ -42,7 +42,7 @@ from reversion.models import Version from reversion import revisions as reversion from re2o.views import form -from re2o.utils import can_create, can_edit +from re2o.utils import can_create, can_edit, can_delete_set from .forms import ServiceForm, DelServiceForm from .models import Service, OptionalUser, OptionalMachine, AssoOption from .models import MailMessageOption, GeneralOption, OptionalTopologie @@ -149,10 +149,10 @@ def edit_service(request, service_instance, serviceid): @login_required -@permission_required('admin') -def del_services(request): +@can_delete_set(Service) +def del_services(request, instances): """Suppression d'un service de la page d'accueil""" - services = DelServiceForm(request.POST or None) + services = DelServiceForm(request.POST or None, instances=instances) if services.is_valid(): services_dels = services.cleaned_data['services'] for services_del in services_dels: