forms.py 18.8 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
lhark's avatar
lhark committed
2 3 4 5 6 7 8
# 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
9
# Copyright © 2017  Maël Kervella
lhark's avatar
lhark committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# 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.
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
24 25 26 27 28 29 30 31 32 33 34
"""
Formulaires d'ajout, edition et suppressions de :
    - machines
    - interfaces
    - domain (noms de machine)
    - alias (cname)
    - service (dhcp, dns..)
    - ns (serveur dns)
    - mx (serveur mail)
    - ports ouverts et profils d'ouverture par interface
"""
lhark's avatar
lhark committed
35

36 37
from __future__ import unicode_literals

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
38
from django.forms import ModelForm, Form
chirac's avatar
chirac committed
39
from django import forms
chirac's avatar
chirac committed
40

41
from re2o.field_permissions import FieldPermissionFormMixin
42
from re2o.mixins import FormRevMixin
43

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
44 45 46 47 48 49 50
from .models import (
    Domain,
    Machine,
    Interface,
    IpList,
    MachineType,
    Extension,
51
    SOA,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
52
    Mx,
Gabriel Detraz's avatar
Gabriel Detraz committed
53
    Txt,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
54 55 56
    Ns,
    Service,
    Vlan,
Gabriel Detraz's avatar
Fix #52  
Gabriel Detraz committed
57
    Srv,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
58 59 60
    Nas,
    IpType,
    OuverturePortList,
61
    Ipv6List,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
62 63
)

64

65
class EditMachineForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
66
    """Formulaire d'édition d'une machine"""
chirac's avatar
chirac committed
67 68 69 70 71
    class Meta:
        model = Machine
        fields = '__all__'

    def __init__(self, *args, **kwargs):
72
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
73
        super(EditMachineForm, self).__init__(*args, prefix=prefix, **kwargs)
chirac's avatar
chirac committed
74 75
        self.fields['name'].label = 'Nom de la machine'

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
76

chirac's avatar
chirac committed
77
class NewMachineForm(EditMachineForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
78
    """Creation d'une machine, ne renseigne que le nom"""
chirac's avatar
chirac committed
79
    class Meta(EditMachineForm.Meta):
chirac's avatar
chirac committed
80
        fields = ['name']
chirac's avatar
chirac committed
81

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
82

83
class EditInterfaceForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
84
    """Edition d'une interface. Edition complète"""
chirac's avatar
chirac committed
85 86
    class Meta:
        model = Interface
87
        fields = ['machine', 'type', 'ipv4', 'mac_address', 'details']
chirac's avatar
chirac committed
88 89

    def __init__(self, *args, **kwargs):
90
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
91
        user = kwargs.get('user')
92
        super(EditInterfaceForm, self).__init__(*args, prefix=prefix, **kwargs)
chirac's avatar
chirac committed
93
        self.fields['mac_address'].label = 'Adresse mac'
chirac's avatar
chirac committed
94 95
        self.fields['type'].label = 'Type de machine'
        self.fields['type'].empty_label = "Séléctionner un type de machine"
96
        if "ipv4" in self.fields:
Maël Kervella's avatar
Maël Kervella committed
97 98
            self.fields['ipv4'].empty_label = ("Assignation automatique de "
                                               "l'ipv4")
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
99 100 101
            self.fields['ipv4'].queryset = IpList.objects.filter(
                interface__isnull=True
            )
102 103 104 105 106 107 108 109
            if not IpType.can_use_all(user):
                self.fields['ipv4'].queryset = IpList.objects.filter(
                    interface__isnull=True
                ).filter(ip_type__in=IpType.objects.filter(need_infra=False))
            else:
                self.fields['ipv4'].queryset = IpList.objects.filter(
                    interface__isnull=True
                )
110
            # Add it's own address
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
111 112 113
            self.fields['ipv4'].queryset |= IpList.objects.filter(
                interface=self.instance
            )
Gabriel Detraz's avatar
Gabriel Detraz committed
114
        if "machine" in self.fields:
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
115 116
            self.fields['machine'].queryset = Machine.objects.all()\
                .select_related('user')
117
        if not MachineType.can_use_all(user):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
118 119 120 121
            self.fields['type'].queryset = MachineType.objects.filter(
                ip_type__in=IpType.objects.filter(need_infra=False)
            )

chirac's avatar
chirac committed
122

123 124 125
class AddInterfaceForm(EditInterfaceForm):
    """Ajout d'une interface à une machine. En fonction des droits,
    affiche ou non l'ensemble des ip disponibles"""
126
    class Meta(EditInterfaceForm.Meta):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
127
        fields = ['type', 'ipv4', 'mac_address', 'details']
128 129


130
class AliasForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
131
    """Ajout d'un alias (et edition), CNAME, contenant nom et extension"""
132
    class Meta:
chirac's avatar
chirac committed
133
        model = Domain
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
134
        fields = ['name', 'extension']
135

136
    def __init__(self, *args, **kwargs):
137
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Gabriel Detraz committed
138
        user = kwargs.pop('user')
139
        super(AliasForm, self).__init__(*args, prefix=prefix, **kwargs)
Maël Kervella's avatar
Maël Kervella committed
140
        can_use_all, _reason = Extension.can_use_all(user)
Gabriel Detraz's avatar
Gabriel Detraz committed
141
        if not can_use_all:
chirac's avatar
chirac committed
142
            self.fields['extension'].queryset = Extension.objects.filter(
Maël Kervella's avatar
Maël Kervella committed
143
                need_infra=False
chirac's avatar
chirac committed
144
            )
145

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
146

147
class DomainForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
148
    """Ajout et edition d'un enregistrement de nom, relié à interface"""
chirac's avatar
chirac committed
149 150
    class Meta:
        model = Domain
151 152 153
        fields = ['name']

    def __init__(self, *args, **kwargs):
154 155
        if 'user' in kwargs:
            user = kwargs.pop('user')
Dalahro's avatar
Dalahro committed
156
            initial = kwargs.get('initial', {})
157
            initial['name'] = user.get_next_domain_name()
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
158
            kwargs['initial'] = initial
159
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
160
        super(DomainForm, self).__init__(*args, prefix=prefix, **kwargs)
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
161 162


163
class DelAliasForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
164 165 166 167 168 169
    """Suppression d'un ou plusieurs objets alias"""
    alias = forms.ModelMultipleChoiceField(
        queryset=Domain.objects.all(),
        label="Alias actuels",
        widget=forms.CheckboxSelectMultiple
    )
170 171 172 173

    def __init__(self, *args, **kwargs):
        interface = kwargs.pop('interface')
        super(DelAliasForm, self).__init__(*args, **kwargs)
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
174 175 176 177
        self.fields['alias'].queryset = Domain.objects.filter(
            cname__in=Domain.objects.filter(interface_parent=interface)
        )

178

179
class MachineTypeForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
180
    """Ajout et edition d'un machinetype, relié à un iptype"""
chirac's avatar
chirac committed
181 182
    class Meta:
        model = MachineType
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
183
        fields = ['type', 'ip_type']
chirac's avatar
chirac committed
184 185

    def __init__(self, *args, **kwargs):
186
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
187
        super(MachineTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
chirac's avatar
chirac committed
188
        self.fields['type'].label = 'Type de machine à ajouter'
chirac's avatar
chirac committed
189
        self.fields['ip_type'].label = "Type d'ip relié"
chirac's avatar
chirac committed
190

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
191

192
class DelMachineTypeForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
193 194
    """Suppression d'un ou plusieurs machinetype"""
    machinetypes = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
195
        queryset=MachineType.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
196 197 198 199
        label="Types de machines actuelles",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
200 201 202 203 204 205 206 207
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelMachineTypeForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['machinetypes'].queryset = instances
        else:
            self.fields['machinetypes'].queryset = MachineType.objects.all()

chirac's avatar
chirac committed
208

209
class IpTypeForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
210 211
    """Formulaire d'ajout d'un iptype. Pas d'edition de l'ip de start et de
    stop après creation"""
chirac's avatar
chirac committed
212 213
    class Meta:
        model = IpType
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
214 215 216
        fields = ['type', 'extension', 'need_infra', 'domaine_ip_start',
                  'domaine_ip_stop', 'prefix_v6', 'vlan', 'ouverture_ports']

chirac's avatar
chirac committed
217
    def __init__(self, *args, **kwargs):
218
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
219
        super(IpTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
chirac's avatar
chirac committed
220 221
        self.fields['type'].label = 'Type ip à ajouter'

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
222

223
class EditIpTypeForm(IpTypeForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
224 225
    """Edition d'un iptype. Pas d'edition du rangev4 possible, car il faudrait
    synchroniser les objets iplist"""
226
    class Meta(IpTypeForm.Meta):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
227 228 229
        fields = ['extension', 'type', 'need_infra', 'prefix_v6', 'vlan',
                  'ouverture_ports']

chirac's avatar
chirac committed
230

231
class DelIpTypeForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
232 233
    """Suppression d'un ou plusieurs iptype"""
    iptypes = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
234
        queryset=IpType.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
235 236 237 238
        label="Types d'ip actuelles",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
239 240 241 242 243 244 245 246
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelIpTypeForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['iptypes'].queryset = instances
        else:
            self.fields['iptypes'].queryset = IpType.objects.all()

chirac's avatar
chirac committed
247

248
class ExtensionForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
249
    """Formulaire d'ajout et edition d'une extension"""
250 251
    class Meta:
        model = Extension
252
        fields = '__all__'
253 254

    def __init__(self, *args, **kwargs):
255
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
256
        super(ExtensionForm, self).__init__(*args, prefix=prefix, **kwargs)
chirac's avatar
chirac committed
257
        self.fields['name'].label = 'Extension à ajouter'
258
        self.fields['origin'].label = 'Enregistrement A origin'
259
        self.fields['origin_v6'].label = 'Enregistrement AAAA origin'
260
        self.fields['soa'].label = 'En-tête SOA à utiliser'
261

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
262

263
class DelExtensionForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
264 265
    """Suppression d'une ou plusieurs extensions"""
    extensions = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
266
        queryset=Extension.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
267 268 269 270
        label="Extensions actuelles",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
271 272 273 274 275 276 277 278
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelExtensionForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['extensions'].queryset = instances
        else:
            self.fields['extensions'].queryset = Extension.objects.all()

279

280
class Ipv6ListForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
281 282 283 284 285 286 287 288 289 290
    """Gestion des ipv6 d'une machine"""
    class Meta:
        model = Ipv6List
        fields = ['ipv6', 'slaac_ip']

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
        super(Ipv6ListForm, self).__init__(*args, prefix=prefix, **kwargs)


291
class SOAForm(FormRevMixin, ModelForm):
292 293 294 295 296 297 298 299 300 301
    """Ajout et edition d'un SOA"""
    class Meta:
        model = SOA
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
        super(SOAForm, self).__init__(*args, prefix=prefix, **kwargs)


302
class DelSOAForm(FormRevMixin, Form):
303 304
    """Suppression d'un ou plusieurs SOA"""
    soa = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
305
        queryset=SOA.objects.none(),
306 307 308 309
        label="SOA actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
310 311 312 313 314 315 316 317
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelSOAForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['soa'].queryset = instances
        else:
            self.fields['soa'].queryset = SOA.objects.all()

318

319
class MxForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
320
    """Ajout et edition d'un MX"""
321 322 323
    class Meta:
        model = Mx
        fields = ['zone', 'priority', 'name']
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
324

Gabriel Detraz's avatar
Gabriel Detraz committed
325
    def __init__(self, *args, **kwargs):
326
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
327
        super(MxForm, self).__init__(*args, prefix=prefix, **kwargs)
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
328 329
        self.fields['name'].queryset = Domain.objects.exclude(
            interface_parent=None
chirac's avatar
chirac committed
330
        ).select_related('extension')
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
331

Maël Kervella's avatar
Maël Kervella committed
332

333
class DelMxForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
334 335
    """Suppression d'un ou plusieurs MX"""
    mx = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
336
        queryset=Mx.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
337 338 339 340
        label="MX actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
341 342 343 344 345 346 347 348
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelMxForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['mx'].queryset = instances
        else:
            self.fields['mx'].queryset = Mx.objects.all()

349

350
class NsForm(FormRevMixin, ModelForm):
chirac's avatar
chirac committed
351 352 353
    """Ajout d'un NS pour une zone
    On exclue les CNAME dans les objets domain (interdit par la rfc)
    donc on prend uniquemet """
354 355
    class Meta:
        model = Ns
Gabriel Detraz's avatar
Gabriel Detraz committed
356
        fields = ['zone', 'ns']
357

Gabriel Detraz's avatar
Fix bug  
Gabriel Detraz committed
358
    def __init__(self, *args, **kwargs):
359
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
360
        super(NsForm, self).__init__(*args, prefix=prefix, **kwargs)
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
361 362
        self.fields['ns'].queryset = Domain.objects.exclude(
            interface_parent=None
chirac's avatar
chirac committed
363
        ).select_related('extension')
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
364

Gabriel Detraz's avatar
Fix bug  
Gabriel Detraz committed
365

366
class DelNsForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
367 368
    """Suppresion d'un ou plusieurs NS"""
    ns = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
369
        queryset=Ns.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
370 371 372 373
        label="Enregistrements NS actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
374 375 376 377 378 379 380 381
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelNsForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['ns'].queryset = instances
        else:
            self.fields['ns'].queryset = Ns.objects.all()

382

383
class TxtForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
384
    """Ajout d'un txt pour une zone"""
Gabriel Detraz's avatar
Gabriel Detraz committed
385
    class Meta:
Gabriel Detraz's avatar
Gabriel Detraz committed
386
        model = Txt
Gabriel Detraz's avatar
Gabriel Detraz committed
387 388
        fields = '__all__'

389
    def __init__(self, *args, **kwargs):
390
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
391
        super(TxtForm, self).__init__(*args, prefix=prefix, **kwargs)
392

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
393

394
class DelTxtForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
395 396
    """Suppression d'un ou plusieurs TXT"""
    txt = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
397
        queryset=Txt.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
398 399 400 401
        label="Enregistrements Txt actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
402 403 404 405 406 407 408 409 410
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelTxtForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['txt'].queryset = instances
        else:
            self.fields['txt'].queryset = Txt.objects.all()


411
class SrvForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Fix #52  
Gabriel Detraz committed
412 413 414 415 416 417 418 419 420 421
    """Ajout d'un srv pour une zone"""
    class Meta:
        model = Srv
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
        super(SrvForm, self).__init__(*args, prefix=prefix, **kwargs)


422
class DelSrvForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Fix #52  
Gabriel Detraz committed
423 424
    """Suppression d'un ou plusieurs Srv"""
    srv = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
425
        queryset=Srv.objects.none(),
Gabriel Detraz's avatar
Fix #52  
Gabriel Detraz committed
426 427 428 429
        label="Enregistrements Srv actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
430 431 432 433 434 435 436 437
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelSrvForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['srv'].queryset = instances
        else:
            self.fields['srv'].queryset = Srv.objects.all()

Gabriel Detraz's avatar
Gabriel Detraz committed
438

439
class NasForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
440 441
    """Ajout d'un type de nas (machine d'authentification,
    swicths, bornes...)"""
442 443 444 445
    class Meta:
        model = Nas
        fields = '__all__'

446
    def __init__(self, *args, **kwargs):
447
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
448 449
        super(NasForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
450

451
class DelNasForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
452 453
    """Suppression d'un ou plusieurs nas"""
    nas = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
454
        queryset=Nas.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
455 456 457 458
        label="Enregistrements Nas actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
459 460 461 462 463 464 465 466
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelNasForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['nas'].queryset = instances
        else:
            self.fields['nas'].queryset = Nas.objects.all()

467

468
class ServiceForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
469
    """Ajout et edition d'une classe de service : dns, dhcp, etc"""
470
    class Meta:
471 472 473
        model = Service
        fields = '__all__'

474
    def __init__(self, *args, **kwargs):
475
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
476
        super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
Maël Kervella's avatar
Maël Kervella committed
477 478 479 480
        self.fields['servers'].queryset = (Interface.objects.all()
                                           .select_related(
                                               'domain__extension'
                                           ))
481

482
    def save(self, commit=True):
Maël Kervella's avatar
Maël Kervella committed
483 484
        # TODO : None of the parents of ServiceForm use the commit
        # parameter in .save()
485 486 487 488 489 490
        instance = super(ServiceForm, self).save(commit=False)
        if commit:
            instance.save()
        instance.process_link(self.cleaned_data.get('servers'))
        return instance

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
491

492
class DelServiceForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
493 494
    """Suppression d'un ou plusieurs service"""
    service = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
495
        queryset=Service.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
496 497 498 499
        label="Services actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
500 501 502 503 504 505 506 507
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelServiceForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['service'].queryset = instances
        else:
            self.fields['service'].queryset = Service.objects.all()

508

509
class VlanForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
510
    """Ajout d'un vlan : id, nom"""
511 512 513 514
    class Meta:
        model = Vlan
        fields = '__all__'

515
    def __init__(self, *args, **kwargs):
516
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
517 518
        super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
519

520
class DelVlanForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
521 522
    """Suppression d'un ou plusieurs vlans"""
    vlan = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
523
        queryset=Vlan.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
524 525 526 527
        label="Vlan actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
528 529 530 531 532 533 534 535
    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelVlanForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['vlan'].queryset = instances
        else:
            self.fields['vlan'].queryset = Vlan.objects.all()

536

537
class EditOuverturePortConfigForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
538 539
    """Edition de la liste des profils d'ouverture de ports
    pour l'interface"""
540
    class Meta:
541 542
        model = Interface
        fields = ['port_lists']
543

544
    def __init__(self, *args, **kwargs):
545
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
546 547 548 549 550 551
        super(EditOuverturePortConfigForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )

552

553
class EditOuverturePortListForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
554 555
    """Edition de la liste des ports et profils d'ouverture
    des ports"""
556
    class Meta:
557
        model = OuverturePortList
558
        fields = '__all__'
559

560
    def __init__(self, *args, **kwargs):
561
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
562 563 564 565 566
        super(EditOuverturePortListForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )