forms.py 8.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

23
from django.forms import ModelForm, Form, ValidationError
24
from django import forms
25
from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Service, Vlan, IpType
26
from django.db.models import Q
27
from django.core.validators import validate_email
28

29 30
from users.models import User

31 32 33 34 35 36 37 38 39 40 41
class EditMachineForm(ModelForm):
    class Meta:
        model = Machine
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super(EditMachineForm, self).__init__(*args, **kwargs)
        self.fields['name'].label = 'Nom de la machine'

class NewMachineForm(EditMachineForm):
    class Meta(EditMachineForm.Meta):
42
        fields = ['name']
43

44 45
class BaseEditMachineForm(EditMachineForm):
    class Meta(EditMachineForm.Meta):
46
        fields = ['name','active']
47

48 49 50 51 52 53 54 55
class EditInterfaceForm(ModelForm):
    class Meta:
        model = Interface
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super(EditInterfaceForm, self).__init__(*args, **kwargs)
        self.fields['mac_address'].label = 'Adresse mac'
56 57
        self.fields['type'].label = 'Type de machine'
        self.fields['type'].empty_label = "Séléctionner un type de machine"
58 59
        if "machine" in self.fields:
            self.fields['machine'].queryset = Machine.objects.all().select_related('user')
60

61 62 63 64 65 66
    def clean(self):
        data = super(EditInterfaceForm, self).clean()
        mac = str(self.data['mac_address'])
        if len(''.join(mac.replace("-",":").split(":"))) != 12:
            self.add_error('mac_address', "Format de la mac incorrect")

67 68
class AddInterfaceForm(EditInterfaceForm):
    class Meta(EditInterfaceForm.Meta):
chirac's avatar
chirac committed
69
        fields = ['ipv4','mac_address','type','details']
70

71
    def __init__(self, *args, **kwargs):
72
        infra = kwargs.pop('infra')
73 74
        super(AddInterfaceForm, self).__init__(*args, **kwargs)
        self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
75
        if not infra:
76
            self.fields['type'].queryset = MachineType.objects.filter(ip_type__in=IpType.objects.filter(need_infra=False))
77
            self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False))
78 79
        else:
            self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True)
80

81 82
class NewInterfaceForm(EditInterfaceForm):
    class Meta(EditInterfaceForm.Meta):
chirac's avatar
chirac committed
83
        fields = ['mac_address','type','details']
84

85 86
class BaseEditInterfaceForm(EditInterfaceForm):
    class Meta(EditInterfaceForm.Meta):
chirac's avatar
chirac committed
87
        fields = ['ipv4','mac_address','type','details']
88

chirac's avatar
chirac committed
89
    def __init__(self, *args, **kwargs):
90
        infra = kwargs.pop('infra')
91 92
        super(BaseEditInterfaceForm, self).__init__(*args, **kwargs)
        self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
93
        if not infra:
94
            self.fields['type'].queryset = MachineType.objects.filter(ip_type__in=IpType.objects.filter(need_infra=False))
95
            self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False))
96 97
        else:
            self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True)
98

99
class AliasForm(ModelForm):
100
    class Meta:
chirac's avatar
chirac committed
101 102
        model = Domain
        fields = ['name','extension']
103

104
    def __init__(self, *args, **kwargs):
105 106
        if 'infra' in kwargs:
            infra = kwargs.pop('infra')
107 108 109 110 111 112 113
        super(AliasForm, self).__init__(*args, **kwargs)

class DomainForm(AliasForm):
    class Meta(AliasForm.Meta):
        fields = ['name']

    def __init__(self, *args, **kwargs):
Dalahro's avatar
Dalahro committed
114 115 116 117 118 119
        if 'name_user' in kwargs:
            name_user = kwargs.pop('name_user')
            nb_machine = kwargs.pop('nb_machine')
            initial = kwargs.get('initial', {})
            initial['name'] = name_user.lower()+str(nb_machine)
            kwargs['initial'] = initial 
120
        super(DomainForm, self).__init__(*args, **kwargs)
121
 
122
class DelAliasForm(Form):
chirac's avatar
chirac committed
123
    alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels",  widget=forms.CheckboxSelectMultiple)
124 125 126 127

    def __init__(self, *args, **kwargs):
        interface = kwargs.pop('interface')
        super(DelAliasForm, self).__init__(*args, **kwargs)
chirac's avatar
chirac committed
128
        self.fields['alias'].queryset = Domain.objects.filter(cname__in=Domain.objects.filter(interface_parent=interface))
129

130 131 132
class MachineTypeForm(ModelForm):
    class Meta:
        model = MachineType
133
        fields = ['type','ip_type']
134 135 136 137

    def __init__(self, *args, **kwargs):
        super(MachineTypeForm, self).__init__(*args, **kwargs)
        self.fields['type'].label = 'Type de machine à ajouter'
138
        self.fields['ip_type'].label = "Type d'ip relié"
139

140
class DelMachineTypeForm(Form):
141 142
    machinetypes = forms.ModelMultipleChoiceField(queryset=MachineType.objects.all(), label="Types de machines actuelles",  widget=forms.CheckboxSelectMultiple)

143 144 145
class IpTypeForm(ModelForm):
    class Meta:
        model = IpType
146
        fields = ['type','extension','need_infra','domaine_ip','domaine_range', 'vlan']
147 148 149 150 151

    def __init__(self, *args, **kwargs):
        super(IpTypeForm, self).__init__(*args, **kwargs)
        self.fields['type'].label = 'Type ip à ajouter'

152 153
class EditIpTypeForm(IpTypeForm):
    class Meta(IpTypeForm.Meta):
154
        fields = ['extension','type','need_infra', 'vlan']
155

156
class DelIpTypeForm(Form):
157
    iptypes = forms.ModelMultipleChoiceField(queryset=IpType.objects.all(), label="Types d'ip actuelles",  widget=forms.CheckboxSelectMultiple)
158

159 160 161
class ExtensionForm(ModelForm):
    class Meta:
        model = Extension
162
        fields = ['name', 'need_infra', 'origin']
163 164 165

    def __init__(self, *args, **kwargs):
        super(ExtensionForm, self).__init__(*args, **kwargs)
chirac's avatar
chirac committed
166
        self.fields['name'].label = 'Extension à ajouter'
167
        self.fields['origin'].label = 'Enregistrement A origin'
168

169
class DelExtensionForm(Form):
170 171
    extensions = forms.ModelMultipleChoiceField(queryset=Extension.objects.all(), label="Extensions actuelles",  widget=forms.CheckboxSelectMultiple)

172 173 174 175
class MxForm(ModelForm):
    class Meta:
        model = Mx
        fields = ['zone', 'priority', 'name']
Gabriel Detraz's avatar
Gabriel Detraz committed
176 177 178 179 180
      
    def __init__(self, *args, **kwargs):
        super(MxForm, self).__init__(*args, **kwargs)
        self.fields['name'].queryset = Domain.objects.exclude(interface_parent=None)
  
181
class DelMxForm(Form):
182 183
    mx = forms.ModelMultipleChoiceField(queryset=Mx.objects.all(), label="MX actuels",  widget=forms.CheckboxSelectMultiple)

184 185 186
class NsForm(ModelForm):
    class Meta:
        model = Ns
Gabriel Detraz's avatar
Gabriel Detraz committed
187
        fields = ['zone', 'ns']
188

Gabriel Detraz's avatar
Gabriel Detraz committed
189 190 191 192
    def __init__(self, *args, **kwargs):
        super(NsForm, self).__init__(*args, **kwargs)
        self.fields['ns'].queryset = Domain.objects.exclude(interface_parent=None)

193
class DelNsForm(Form):
194 195
    ns = forms.ModelMultipleChoiceField(queryset=Ns.objects.all(), label="Enregistrements NS actuels",  widget=forms.CheckboxSelectMultiple)

196
class ServiceForm(ModelForm):
197
    class Meta:
198 199 200 201 202 203 204 205 206 207 208 209 210
        model = Service
        fields = '__all__'

    def save(self, commit=True):
        instance = super(ServiceForm, self).save(commit=False)
        if commit:
            instance.save()
        instance.process_link(self.cleaned_data.get('servers'))
        return instance

class DelServiceForm(Form):
    service = forms.ModelMultipleChoiceField(queryset=Service.objects.all(), label="Services actuels",  widget=forms.CheckboxSelectMultiple)

211 212 213 214 215 216 217 218 219 220
class VlanForm(ModelForm):
    class Meta:
        model = Vlan
        fields = '__all__'

class DelVlanForm(Form):
    vlan = forms.ModelMultipleChoiceField(queryset=Vlan.objects.all(), label="Vlan actuels",  widget=forms.CheckboxSelectMultiple)