forms.py 5.52 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
3
from .models import Alias, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, IpType
chirac's avatar
chirac committed
4
5
6
7
8
9
10
11
12
13
14
15

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

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

chirac's avatar
chirac committed
22
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['dns'].label = 'Nom dns de la machine'
        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','dns','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:
chirac's avatar
chirac committed
43
44
            self.fields['type'].queryset = MachineType.objects.filter(ip_type=IpType.objects.filter(need_infra=False))
            self.fields['ipv4'].queryset = IpList.objects.filter(ip_type=IpType.objects.filter(need_infra=False))
45

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

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

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

62
class AliasForm(ModelForm):
63
64
    class Meta:
        model = Alias
65
        fields = ['alias','extension']
66

67
class DelAliasForm(ModelForm):
68
69
70
71
72
73
    alias = forms.ModelMultipleChoiceField(queryset=Alias.objects.all(), label="Alias actuels",  widget=forms.CheckboxSelectMultiple)

    def __init__(self, *args, **kwargs):
        interface = kwargs.pop('interface')
        super(DelAliasForm, self).__init__(*args, **kwargs)
        self.fields['alias'].queryset = Alias.objects.filter(interface_parent=interface)
74
75
76
77

    class Meta:
        exclude = ['interface_parent', 'alias', 'extension']
        model = Alias
78

chirac's avatar
chirac committed
79
80
81
class MachineTypeForm(ModelForm):
    class Meta:
        model = MachineType
chirac's avatar
chirac committed
82
        fields = ['type','ip_type']
chirac's avatar
chirac committed
83
84
85
86

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

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
93
        exclude = ['type','ip_type']
chirac's avatar
chirac committed
94
        model = MachineType
95

chirac's avatar
chirac committed
96
97
98
class IpTypeForm(ModelForm):
    class Meta:
        model = IpType
guimoz's avatar
guimoz committed
99
        fields = ['type','extension','need_infra','domaine_ip','domaine_range']
chirac's avatar
chirac committed
100
101
102
103
104
105
106
107
108

    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
109
        exclude = ['type','extension','need_infra','domaine_ip','domaine_range']
chirac's avatar
chirac committed
110
111
112
        model = IpType


113
114
115
class ExtensionForm(ModelForm):
    class Meta:
        model = Extension
116
        fields = ['name', 'origin']
117
118
119

    def __init__(self, *args, **kwargs):
        super(ExtensionForm, self).__init__(*args, **kwargs)
chirac's avatar
chirac committed
120
        self.fields['name'].label = 'Extension à ajouter'
121
        self.fields['origin'].label = 'Enregistrement A origin'
122
123
124
125
126

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

    class Meta:
127
        exclude = ['name', 'origin']
128
        model = Extension
129
130
131
132
133
134
135
136
137
138
139
140
141

class MxForm(ModelForm):
    class Meta:
        model = Mx
        fields = ['zone', 'priority', 'name']
        
class DelMxForm(ModelForm):
    mx = forms.ModelMultipleChoiceField(queryset=Mx.objects.all(), label="MX actuels",  widget=forms.CheckboxSelectMultiple)

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

142
143
144
145
146
147
148
149
150
151
152
class NsForm(ModelForm):
    class Meta:
        model = Ns
        fields = ['zone', 'interface']

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

    class Meta:
        exclude = ['zone', 'interface']
        model = Ns