forms.py 6.73 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
    def __init__(self, *args, **kwargs):
        infra = kwargs.pop('infra')
Dalahro's avatar
Dalahro committed
73 74 75 76 77 78
        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 
79 80 81 82
        super(AliasForm, self).__init__(*args, **kwargs)
        if not infra:
            self.fields['extension'].queryset = Extension.objects.filter(need_infra=False)
 
83
class DelAliasForm(ModelForm):
chirac's avatar
chirac committed
84
    alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels",  widget=forms.CheckboxSelectMultiple)
85 86 87 88

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

    class Meta:
chirac's avatar
chirac committed
92 93
        exclude = ['interface_parent', 'name', 'extension', 'cname']
        model = Domain
94

chirac's avatar
chirac committed
95 96 97
class MachineTypeForm(ModelForm):
    class Meta:
        model = MachineType
chirac's avatar
chirac committed
98
        fields = ['type','ip_type']
chirac's avatar
chirac committed
99 100 101 102

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

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
109
        exclude = ['type','ip_type']
chirac's avatar
chirac committed
110
        model = MachineType
111

chirac's avatar
chirac committed
112 113 114
class IpTypeForm(ModelForm):
    class Meta:
        model = IpType
guimoz's avatar
guimoz committed
115
        fields = ['type','extension','need_infra','domaine_ip','domaine_range']
chirac's avatar
chirac committed
116 117 118 119 120 121 122 123 124

    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
125
        exclude = ['type','extension','need_infra','domaine_ip','domaine_range']
chirac's avatar
chirac committed
126 127 128
        model = IpType


129 130 131
class ExtensionForm(ModelForm):
    class Meta:
        model = Extension
132
        fields = ['name', 'need_infra', 'origin']
133 134 135

    def __init__(self, *args, **kwargs):
        super(ExtensionForm, self).__init__(*args, **kwargs)
chirac's avatar
chirac committed
136
        self.fields['name'].label = 'Extension à ajouter'
137
        self.fields['origin'].label = 'Enregistrement A origin'
138 139 140 141 142

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

    class Meta:
143
        exclude = ['name', 'need_infra', 'origin']
144
        model = Extension
145 146 147 148 149

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

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

162 163 164
class NsForm(ModelForm):
    class Meta:
        model = Ns
Gabriel Detraz's avatar
Gabriel Detraz committed
165
        fields = ['zone', 'ns']
166

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

171 172 173 174
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
175
        exclude = ['zone', 'ns']
176
        model = Ns