forms.py 9.26 KB
Newer Older
lhark's avatar
lhark committed
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 24
from __future__ import unicode_literals

chirac's avatar
chirac committed
25
from django.forms import ModelForm, Form, ValidationError
chirac's avatar
chirac committed
26
from django import forms
27
from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Text, Ns, Service, Vlan, Nas, IpType, PortList
28
from django.db.models import Q
29
from django.core.validators import validate_email
chirac's avatar
chirac committed
30

31 32
from users.models import User

chirac's avatar
chirac committed
33 34 35 36 37 38 39 40 41 42 43
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):
chirac's avatar
chirac committed
44
        fields = ['name']
chirac's avatar
chirac committed
45

46 47
class BaseEditMachineForm(EditMachineForm):
    class Meta(EditMachineForm.Meta):
chirac's avatar
chirac committed
48
        fields = ['name','active']
49

chirac's avatar
chirac committed
50 51 52 53 54 55 56 57
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'
chirac's avatar
chirac committed
58 59
        self.fields['type'].label = 'Type de machine'
        self.fields['type'].empty_label = "Séléctionner un type de machine"
Gabriel Detraz's avatar
Gabriel Detraz committed
60 61
        if "machine" in self.fields:
            self.fields['machine'].queryset = Machine.objects.all().select_related('user')
chirac's avatar
chirac committed
62 63 64

class AddInterfaceForm(EditInterfaceForm):
    class Meta(EditInterfaceForm.Meta):
chirac's avatar
chirac committed
65
        fields = ['ipv4','mac_address','type','details']
chirac's avatar
chirac committed
66

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

chirac's avatar
chirac committed
77 78
class NewInterfaceForm(EditInterfaceForm):
    class Meta(EditInterfaceForm.Meta):
chirac's avatar
chirac committed
79
        fields = ['mac_address','type','details']
chirac's avatar
chirac committed
80

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

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

95
class AliasForm(ModelForm):
96
    class Meta:
chirac's avatar
chirac committed
97 98
        model = Domain
        fields = ['name','extension']
99

100
    def __init__(self, *args, **kwargs):
101 102
        if 'infra' in kwargs:
            infra = kwargs.pop('infra')
103 104 105 106 107 108 109
        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
110 111 112 113 114 115
        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 
116
        super(DomainForm, self).__init__(*args, **kwargs)
117
 
118
class DelAliasForm(Form):
chirac's avatar
chirac committed
119
    alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels",  widget=forms.CheckboxSelectMultiple)
120 121 122 123

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

chirac's avatar
chirac committed
126 127 128
class MachineTypeForm(ModelForm):
    class Meta:
        model = MachineType
chirac's avatar
chirac committed
129
        fields = ['type','ip_type']
chirac's avatar
chirac committed
130 131 132 133

    def __init__(self, *args, **kwargs):
        super(MachineTypeForm, self).__init__(*args, **kwargs)
        self.fields['type'].label = 'Type de machine à ajouter'
chirac's avatar
chirac committed
134
        self.fields['ip_type'].label = "Type d'ip relié"
chirac's avatar
chirac committed
135

136
class DelMachineTypeForm(Form):
chirac's avatar
chirac committed
137 138
    machinetypes = forms.ModelMultipleChoiceField(queryset=MachineType.objects.all(), label="Types de machines actuelles",  widget=forms.CheckboxSelectMultiple)

chirac's avatar
chirac committed
139 140 141
class IpTypeForm(ModelForm):
    class Meta:
        model = IpType
142 143
        fields = ['type','extension','need_infra','domaine_ip_start','domaine_ip_stop', 'vlan']
        
chirac's avatar
chirac committed
144 145 146 147 148

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

149 150
class EditIpTypeForm(IpTypeForm):
    class Meta(IpTypeForm.Meta):
151
        fields = ['extension','type','need_infra', 'vlan']
chirac's avatar
chirac committed
152

153
class DelIpTypeForm(Form):
154
    iptypes = forms.ModelMultipleChoiceField(queryset=IpType.objects.all(), label="Types d'ip actuelles",  widget=forms.CheckboxSelectMultiple)
chirac's avatar
chirac committed
155

156 157 158
class ExtensionForm(ModelForm):
    class Meta:
        model = Extension
159
        fields = ['name', 'need_infra', 'origin']
160 161 162

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

166
class DelExtensionForm(Form):
167 168
    extensions = forms.ModelMultipleChoiceField(queryset=Extension.objects.all(), label="Extensions actuelles",  widget=forms.CheckboxSelectMultiple)

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

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

Gabriel Detraz's avatar
Fix bug  
Gabriel Detraz committed
186 187 188 189
    def __init__(self, *args, **kwargs):
        super(NsForm, self).__init__(*args, **kwargs)
        self.fields['ns'].queryset = Domain.objects.exclude(interface_parent=None)

190
class DelNsForm(Form):
191 192
    ns = forms.ModelMultipleChoiceField(queryset=Ns.objects.all(), label="Enregistrements NS actuels",  widget=forms.CheckboxSelectMultiple)

Gabriel Detraz's avatar
Gabriel Detraz committed
193 194 195 196 197 198 199 200
class TextForm(ModelForm):
    class Meta:
        model = Text
        fields = '__all__'

class DelTextForm(Form):
    text = forms.ModelMultipleChoiceField(queryset=Text.objects.all(), label="Enregistrements Text actuels",  widget=forms.CheckboxSelectMultiple)

201 202 203 204 205 206 207 208
class NasForm(ModelForm):
    class Meta:
        model = Nas
        fields = '__all__'

class DelNasForm(Form):
    nas = forms.ModelMultipleChoiceField(queryset=Nas.objects.all(), label="Enregistrements Nas actuels",  widget=forms.CheckboxSelectMultiple)

209
class ServiceForm(ModelForm):
210
    class Meta:
211 212 213 214 215 216 217 218 219 220 221 222 223
        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)

224 225 226 227 228 229 230 231
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)

232 233 234 235 236 237 238
class EditPortListForm(ModelForm):
    tcp_ports = forms.CharField(required=False, label="Ports TCP")
    udp_ports = forms.CharField(required=False, label="Ports UDP")
    # interfaces = forms.ModelMultipleChoiceField(queryset=Interface.objects.filter(Q(has_public_ip=True)), label="Interface", widget=forms.CheckboxSelectMultiple)
    class Meta:
        model = PortList
        fields = ['name']
239 240