From 2936252a324c31de53f7b4f4962b931ddbb51df2 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 13 Dec 2017 18:31:13 +0100 Subject: [PATCH] Can_delete_set sur machines --- machines/forms.py | 113 ++++++++++++++++++++++++++++++++++++++++------ machines/views.py | 69 ++++++++++++++-------------- 2 files changed, 135 insertions(+), 47 deletions(-) diff --git a/machines/forms.py b/machines/forms.py index d6aa5e3e..fefdc9c3 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -233,11 +233,19 @@ class MachineTypeForm(ModelForm): class DelMachineTypeForm(Form): """Suppression d'un ou plusieurs machinetype""" machinetypes = forms.ModelMultipleChoiceField( - queryset=MachineType.objects.all(), + queryset=MachineType.objects.none(), label="Types de machines actuelles", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelMachineTypeForm, self).__init__(*args, **kwargs) + if instances: + self.fields['machinetypes'].queryset = instances + else: + self.fields['machinetypes'].queryset = MachineType.objects.all() + class IpTypeForm(ModelForm): """Formulaire d'ajout d'un iptype. Pas d'edition de l'ip de start et de @@ -264,11 +272,19 @@ class EditIpTypeForm(IpTypeForm): class DelIpTypeForm(Form): """Suppression d'un ou plusieurs iptype""" iptypes = forms.ModelMultipleChoiceField( - queryset=IpType.objects.all(), + queryset=IpType.objects.none(), label="Types d'ip actuelles", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelIpTypeForm, self).__init__(*args, **kwargs) + if instances: + self.fields['iptypes'].queryset = instances + else: + self.fields['iptypes'].queryset = IpType.objects.all() + class ExtensionForm(ModelForm): """Formulaire d'ajout et edition d'une extension""" @@ -288,11 +304,19 @@ class ExtensionForm(ModelForm): class DelExtensionForm(Form): """Suppression d'une ou plusieurs extensions""" extensions = forms.ModelMultipleChoiceField( - queryset=Extension.objects.all(), + queryset=Extension.objects.none(), label="Extensions actuelles", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelExtensionForm, self).__init__(*args, **kwargs) + if instances: + self.fields['extensions'].queryset = instances + else: + self.fields['extensions'].queryset = Extension.objects.all() + class SOAForm(ModelForm): """Ajout et edition d'un SOA""" @@ -308,11 +332,19 @@ class SOAForm(ModelForm): class DelSOAForm(Form): """Suppression d'un ou plusieurs SOA""" soa = forms.ModelMultipleChoiceField( - queryset=SOA.objects.all(), + queryset=SOA.objects.none(), label="SOA actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelSOAForm, self).__init__(*args, **kwargs) + if instances: + self.fields['soa'].queryset = instances + else: + self.fields['soa'].queryset = SOA.objects.all() + class MxForm(ModelForm): """Ajout et edition d'un MX""" @@ -327,15 +359,22 @@ class MxForm(ModelForm): interface_parent=None ).select_related('extension') - class DelMxForm(Form): """Suppression d'un ou plusieurs MX""" mx = forms.ModelMultipleChoiceField( - queryset=Mx.objects.all(), + queryset=Mx.objects.none(), label="MX actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelMxForm, self).__init__(*args, **kwargs) + if instances: + self.fields['mx'].queryset = instances + else: + self.fields['mx'].queryset = Mx.objects.all() + class NsForm(ModelForm): """Ajout d'un NS pour une zone @@ -356,11 +395,19 @@ class NsForm(ModelForm): class DelNsForm(Form): """Suppresion d'un ou plusieurs NS""" ns = forms.ModelMultipleChoiceField( - queryset=Ns.objects.all(), + queryset=Ns.objects.none(), label="Enregistrements NS actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelNsForm, self).__init__(*args, **kwargs) + if instances: + self.fields['ns'].queryset = instances + else: + self.fields['ns'].queryset = Ns.objects.all() + class TxtForm(ModelForm): """Ajout d'un txt pour une zone""" @@ -376,12 +423,20 @@ class TxtForm(ModelForm): class DelTxtForm(Form): """Suppression d'un ou plusieurs TXT""" txt = forms.ModelMultipleChoiceField( - queryset=Txt.objects.all(), + queryset=Txt.objects.none(), label="Enregistrements Txt actuels", widget=forms.CheckboxSelectMultiple ) - + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelTxtForm, self).__init__(*args, **kwargs) + if instances: + self.fields['txt'].queryset = instances + else: + self.fields['txt'].queryset = Txt.objects.all() + + class SrvForm(ModelForm): """Ajout d'un srv pour une zone""" class Meta: @@ -396,11 +451,19 @@ class SrvForm(ModelForm): class DelSrvForm(Form): """Suppression d'un ou plusieurs Srv""" srv = forms.ModelMultipleChoiceField( - queryset=Srv.objects.all(), + queryset=Srv.objects.none(), label="Enregistrements Srv actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelSrvForm, self).__init__(*args, **kwargs) + if instances: + self.fields['srv'].queryset = instances + else: + self.fields['srv'].queryset = Srv.objects.all() + class NasForm(ModelForm): """Ajout d'un type de nas (machine d'authentification, @@ -417,11 +480,19 @@ class NasForm(ModelForm): class DelNasForm(Form): """Suppression d'un ou plusieurs nas""" nas = forms.ModelMultipleChoiceField( - queryset=Nas.objects.all(), + queryset=Nas.objects.none(), label="Enregistrements Nas actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelNasForm, self).__init__(*args, **kwargs) + if instances: + self.fields['nas'].queryset = instances + else: + self.fields['nas'].queryset = Nas.objects.all() + class ServiceForm(ModelForm): """Ajout et edition d'une classe de service : dns, dhcp, etc""" @@ -446,11 +517,19 @@ class ServiceForm(ModelForm): class DelServiceForm(Form): """Suppression d'un ou plusieurs service""" service = forms.ModelMultipleChoiceField( - queryset=Service.objects.all(), + queryset=Service.objects.none(), label="Services actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelServiceForm, self).__init__(*args, **kwargs) + if instances: + self.fields['service'].queryset = instances + else: + self.fields['service'].queryset = Service.objects.all() + class VlanForm(ModelForm): """Ajout d'un vlan : id, nom""" @@ -466,11 +545,19 @@ class VlanForm(ModelForm): class DelVlanForm(Form): """Suppression d'un ou plusieurs vlans""" vlan = forms.ModelMultipleChoiceField( - queryset=Vlan.objects.all(), + queryset=Vlan.objects.none(), label="Vlan actuels", widget=forms.CheckboxSelectMultiple ) + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelVlanForm, self).__init__(*args, **kwargs) + if instances: + self.fields['vlan'].queryset = instances + else: + self.fields['vlan'].queryset = Vlan.objects.all() + class EditOuverturePortConfigForm(ModelForm): """Edition de la liste des profils d'ouverture de ports diff --git a/machines/views.py b/machines/views.py index ddc56400..1a47ebce 100644 --- a/machines/views.py +++ b/machines/views.py @@ -127,7 +127,8 @@ from re2o.utils import ( can_create, can_edit, can_delete, - can_view + can_view, + can_delete_set, ) from re2o.views import form @@ -408,10 +409,10 @@ def edit_iptype(request, iptype_instance, iptypeid): return form({'iptypeform': iptype}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_iptype(request): +@can_delete_set(IpType) +def del_iptype(request, instances): """ Suppression d'un range ip. Supprime les objets ip associƩs""" - iptype = DelIpTypeForm(request.POST or None) + iptype = DelIpTypeForm(request.POST or None, instances=instances) if iptype.is_valid(): iptype_dels = iptype.cleaned_data['iptypes'] for iptype_del in iptype_dels: @@ -454,9 +455,9 @@ def edit_machinetype(request, machinetype_instance, machinetypeid): return form({'machinetypeform': machinetype}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_machinetype(request): - machinetype = DelMachineTypeForm(request.POST or None) +@can_delete_set(MachineType) +def del_machinetype(request, instances): + machinetype = DelMachineTypeForm(request.POST or None, instances=instances) if machinetype.is_valid(): machinetype_dels = machinetype.cleaned_data['machinetypes'] for machinetype_del in machinetype_dels: @@ -499,9 +500,9 @@ def edit_extension(request, extension_instance, extensionid): return form({'extensionform': extension}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_extension(request): - extension = DelExtensionForm(request.POST or None) +@can_delete_set(Extension) +def del_extension(request, instances): + extension = DelExtensionForm(request.POST or None, instances=instances) if extension.is_valid(): extension_dels = extension.cleaned_data['extensions'] for extension_del in extension_dels: @@ -544,9 +545,9 @@ def edit_soa(request, soa_instance, soaid): return form({'soaform': soa}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_soa(request): - soa = DelSOAForm(request.POST or None) +@can_delete_set(SOA) +def del_soa(request, instances): + soa = DelSOAForm(request.POST or None, instances=instances) if soa.is_valid(): soa_dels = soa.cleaned_data['soa'] for soa_del in soa_dels: @@ -589,9 +590,9 @@ def edit_mx(request, mx_instance, mxid): return form({'mxform': mx}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_mx(request): - mx = DelMxForm(request.POST or None) +@can_delete_set(Mx) +def del_mx(request, instances): + mx = DelMxForm(request.POST or None, instances=instances) if mx.is_valid(): mx_dels = mx.cleaned_data['mx'] for mx_del in mx_dels: @@ -634,9 +635,9 @@ def edit_ns(request, ns_instance, nsid): return form({'nsform': ns}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_ns(request): - ns = DelNsForm(request.POST or None) +@can_delete_set(Ns) +def del_ns(request, instances): + ns = DelNsForm(request.POST or None, instances=instances) if ns.is_valid(): ns_dels = ns.cleaned_data['ns'] for ns_del in ns_dels: @@ -679,9 +680,9 @@ def edit_txt(request, txt_instance, txtid): return form({'txtform': txt}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_txt(request): - txt = DelTxtForm(request.POST or None) +@can_delete_set(Txt) +def del_txt(request, instances): + txt = DelTxtForm(request.POST or None, instances=instances) if txt.is_valid(): txt_dels = txt.cleaned_data['txt'] for txt_del in txt_dels: @@ -724,9 +725,9 @@ def edit_srv(request, srv_instance, srvid): return form({'srvform': srv}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_srv(request): - srv = DelSrvForm(request.POST or None) +@can_delete_set(Srv) +def del_srv(request, instances): + srv = DelSrvForm(request.POST or None, instances=instances) if srv.is_valid(): srv_dels = srv.cleaned_data['srv'] for srv_del in srv_dels: @@ -827,9 +828,9 @@ def edit_service(request, service_instance, serviceid): return form({'serviceform': service}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_service(request): - service = DelServiceForm(request.POST or None) +@can_delete_set(Service) +def del_service(request, instances): + service = DelServiceForm(request.POST or None, instances=instances) if service.is_valid(): service_dels = service.cleaned_data['service'] for service_del in service_dels: @@ -872,9 +873,9 @@ def edit_vlan(request, vlan_instance, vlanid): return form({'vlanform': vlan}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_vlan(request): - vlan = DelVlanForm(request.POST or None) +@can_delete_set(Vlan) +def del_vlan(request, instances): + vlan = DelVlanForm(request.POST or None, instances=instances) if vlan.is_valid(): vlan_dels = vlan.cleaned_data['vlan'] for vlan_del in vlan_dels: @@ -917,9 +918,9 @@ def edit_nas(request, nas_instance, nasid): return form({'nasform': nas}, 'machines/machine.html', request) @login_required -@permission_required('infra') -def del_nas(request): - nas = DelNasForm(request.POST or None) +@can_delete_set(Nas) +def del_nas(request, instances): + nas = DelNasForm(request.POST or None, instances=instances) if nas.is_valid(): nas_dels = nas.cleaned_data['nas'] for nas_del in nas_dels: