forms.py 6.46 KB
Newer Older
chirac's avatar
chirac committed
1
from django.forms import ModelForm, Form, ValidationError
chirac's avatar
chirac committed
2
from django import forms
chirac's avatar
chirac committed
3
from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, IpType
4
from django.db.models import Q
chirac's avatar
chirac committed
5
6
7
8
9
10
11
12
13
14
15
16

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
17
        fields = ['name']
chirac's avatar
chirac committed
18

19
20
class BaseEditMachineForm(EditMachineForm):
    class Meta(EditMachineForm.Meta):
chirac's avatar
chirac committed
21
        fields = ['name','active']
22

chirac's avatar
chirac committed
23
24
25
26
27
28
29
30
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
31
32
        self.fields['type'].label = 'Type de machine'
        self.fields['type'].empty_label = "Séléctionner un type de machine"
chirac's avatar
chirac committed
33
34
35

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

38
    def __init__(self, *args, **kwargs):
39
        infra = kwargs.pop('infra')
40
41
        super(AddInterfaceForm, self).__init__(*args, **kwargs)
        self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
42
        if not infra:
43
44
            self.fields['type'].queryset = MachineType.objects.filter(ip_type__in=IpType.objects.filter(need_infra=False))
            self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False)).filter(need_infra=False)
45
46
        else:
            self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True)
47

chirac's avatar
chirac committed
48
49
class NewInterfaceForm(EditInterfaceForm):
    class Meta(EditInterfaceForm.Meta):
chirac's avatar
chirac committed
50
        fields = ['mac_address','type','details']
chirac's avatar
chirac committed
51

52
53
class BaseEditInterfaceForm(EditInterfaceForm):
    class Meta(EditInterfaceForm.Meta):
chirac's avatar
chirac committed
54
        fields = ['ipv4','mac_address','type','details']
55

chirac's avatar
chirac committed
56
    def __init__(self, *args, **kwargs):
57
        infra = kwargs.pop('infra')
58
59
        super(BaseEditInterfaceForm, self).__init__(*args, **kwargs)
        self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
60
        if not infra:
61
62
            self.fields['type'].queryset = MachineType.objects.filter(ip_type__in=IpType.objects.filter(need_infra=False))
            self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True).filter(ip_type__in=IpType.objects.filter(need_infra=False)).filter(need_infra=False)
63
64
        else:
            self.fields['ipv4'].queryset = IpList.objects.filter(interface__isnull=True)
65

66
class AliasForm(ModelForm):
67
    class Meta:
chirac's avatar
chirac committed
68
69
        model = Domain
        fields = ['name','extension']
70

71
72
73
74
75
76
    def __init__(self, *args, **kwargs):
        infra = kwargs.pop('infra')
        super(AliasForm, self).__init__(*args, **kwargs)
        if not infra:
            self.fields['extension'].queryset = Extension.objects.filter(need_infra=False)
 
77
class DelAliasForm(ModelForm):
chirac's avatar
chirac committed
78
    alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels",  widget=forms.CheckboxSelectMultiple)
79
80
81
82

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

    class Meta:
chirac's avatar
chirac committed
86
87
        exclude = ['interface_parent', 'name', 'extension', 'cname']
        model = Domain
88

chirac's avatar
chirac committed
89
90
91
class MachineTypeForm(ModelForm):
    class Meta:
        model = MachineType
chirac's avatar
chirac committed
92
        fields = ['type','ip_type']
chirac's avatar
chirac committed
93
94
95
96

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

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

    class Meta:
chirac's avatar
chirac committed
103
        exclude = ['type','ip_type']
chirac's avatar
chirac committed
104
        model = MachineType
105

chirac's avatar
chirac committed
106
107
108
class IpTypeForm(ModelForm):
    class Meta:
        model = IpType
guimoz's avatar
guimoz committed
109
        fields = ['type','extension','need_infra','domaine_ip','domaine_range']
chirac's avatar
chirac committed
110
111
112
113
114
115
116
117
118

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

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

    class Meta:
guimoz's avatar
guimoz committed
119
        exclude = ['type','extension','need_infra','domaine_ip','domaine_range']
chirac's avatar
chirac committed
120
121
122
        model = IpType


123
124
125
class ExtensionForm(ModelForm):
    class Meta:
        model = Extension
126
        fields = ['name', 'need_infra', 'origin']
127
128
129

    def __init__(self, *args, **kwargs):
        super(ExtensionForm, self).__init__(*args, **kwargs)
chirac's avatar
chirac committed
130
        self.fields['name'].label = 'Extension à ajouter'
131
        self.fields['origin'].label = 'Enregistrement A origin'
132
133
134
135
136

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

    class Meta:
137
        exclude = ['name', 'need_infra', 'origin']
138
        model = Extension
139
140
141
142
143

class MxForm(ModelForm):
    class Meta:
        model = Mx
        fields = ['zone', 'priority', 'name']
Gabriel Detraz's avatar
Gabriel Detraz committed
144
145
146
147
148
      
    def __init__(self, *args, **kwargs):
        super(MxForm, self).__init__(*args, **kwargs)
        self.fields['name'].queryset = Domain.objects.exclude(interface_parent=None)
  
149
150
151
152
153
154
155
class DelMxForm(ModelForm):
    mx = forms.ModelMultipleChoiceField(queryset=Mx.objects.all(), label="MX actuels",  widget=forms.CheckboxSelectMultiple)

    class Meta:
        exclude = ['zone', 'priority', 'name']
        model = Mx

156
157
158
class NsForm(ModelForm):
    class Meta:
        model = Ns
Gabriel Detraz's avatar
Gabriel Detraz committed
159
        fields = ['zone', 'ns']
160

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

165
166
167
168
class DelNsForm(ModelForm):
    ns = forms.ModelMultipleChoiceField(queryset=Ns.objects.all(), label="Enregistrements NS actuels",  widget=forms.CheckboxSelectMultiple)

    class Meta:
Gabriel Detraz's avatar
Gabriel Detraz committed
169
        exclude = ['zone', 'ns']
170
        model = Ns