forms.py 18.6 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
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
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
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
"""
35

36 37
from __future__ import unicode_literals

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

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

Gabriel Detraz's avatar
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
Gabriel Detraz committed
52
    Mx,
Gabriel Detraz's avatar
Gabriel Detraz committed
53
    Txt,
Gabriel Detraz's avatar
Gabriel Detraz committed
54 55 56
    Ns,
    Service,
    Vlan,
Gabriel Detraz's avatar
Gabriel Detraz committed
57
    Srv,
Gabriel Detraz's avatar
Gabriel Detraz committed
58 59 60
    Nas,
    IpType,
    OuverturePortList,
61
    Ipv6List,
Gabriel Detraz's avatar
Gabriel Detraz committed
62 63
)

64

65
class EditMachineForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
66
    """Formulaire d'édition d'une machine"""
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)
74 75
        self.fields['name'].label = 'Nom de la machine'

Gabriel Detraz's avatar
Gabriel Detraz committed
76

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

Gabriel Detraz's avatar
Gabriel Detraz committed
82

83
class EditInterfaceForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
84
    """Edition d'une interface. Edition complète"""
85 86
    class Meta:
        model = Interface
87
        fields = ['machine', 'type', 'ipv4', 'mac_address', 'details']
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)
93
        self.fields['mac_address'].label = 'Adresse mac'
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:
97
            self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
Gabriel Detraz's avatar
Gabriel Detraz committed
98 99 100
            self.fields['ipv4'].queryset = IpList.objects.filter(
                interface__isnull=True
            )
101 102 103 104 105 106 107 108
            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
                )
109
            # Add it's own address
Gabriel Detraz's avatar
Gabriel Detraz committed
110 111 112
            self.fields['ipv4'].queryset |= IpList.objects.filter(
                interface=self.instance
            )
113
        if "machine" in self.fields:
Gabriel Detraz's avatar
Gabriel Detraz committed
114 115
            self.fields['machine'].queryset = Machine.objects.all()\
                .select_related('user')
116
        if not MachineType.can_use_all(user):
Gabriel Detraz's avatar
Gabriel Detraz committed
117 118 119 120
            self.fields['type'].queryset = MachineType.objects.filter(
                ip_type__in=IpType.objects.filter(need_infra=False)
            )

121

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


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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
145

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

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


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

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

177

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
190

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

199 200 201 202 203 204 205 206
    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()

207

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
221

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

229

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

238 239 240 241 242 243 244 245
    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()

246

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
261

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

270 271 272 273 274 275 276 277
    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()

278

279
class Ipv6ListForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
280 281 282 283 284 285 286 287 288 289
    """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)


290
class SOAForm(FormRevMixin, ModelForm):
291 292 293 294 295 296 297 298 299 300
    """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)


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

309 310 311 312 313 314 315 316
    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()

317

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

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

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

339 340 341 342 343 344 345 346
    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()

347

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
363

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

372 373 374 375 376 377 378 379
    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()

380

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
391

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

400 401 402 403 404 405 406 407 408
    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()


409
class SrvForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
410 411 412 413 414 415 416 417 418 419
    """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)


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

428 429 430 431 432 433 434 435
    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
436

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
448

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

457 458 459 460 461 462 463 464
    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()

465

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

472
    def __init__(self, *args, **kwargs):
473
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
474
        super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
475 476
        self.fields['servers'].queryset = Interface.objects.all()\
        .select_related('domain__extension')
477

478 479 480 481 482 483 484
    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

Gabriel Detraz's avatar
Gabriel Detraz committed
485

486
class DelServiceForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
487 488
    """Suppression d'un ou plusieurs service"""
    service = forms.ModelMultipleChoiceField(
489
        queryset=Service.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
490 491 492 493
        label="Services actuels",
        widget=forms.CheckboxSelectMultiple
    )

494 495 496 497 498 499 500 501
    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()

502

503
class VlanForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
504
    """Ajout d'un vlan : id, nom"""
505 506 507 508
    class Meta:
        model = Vlan
        fields = '__all__'

509
    def __init__(self, *args, **kwargs):
510
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
511 512
        super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Gabriel Detraz committed
513

514
class DelVlanForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
515 516
    """Suppression d'un ou plusieurs vlans"""
    vlan = forms.ModelMultipleChoiceField(
517
        queryset=Vlan.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
518 519 520 521
        label="Vlan actuels",
        widget=forms.CheckboxSelectMultiple
    )

522 523 524 525 526 527 528 529
    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()

530

531
class EditOuverturePortConfigForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
532 533
    """Edition de la liste des profils d'ouverture de ports
    pour l'interface"""
534
    class Meta:
535 536
        model = Interface
        fields = ['port_lists']
537

538
    def __init__(self, *args, **kwargs):
539
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Gabriel Detraz committed
540 541 542 543 544 545
        super(EditOuverturePortConfigForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )

546

547
class EditOuverturePortListForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
548 549
    """Edition de la liste des ports et profils d'ouverture
    des ports"""
550
    class Meta:
551
        model = OuverturePortList
552
        fields = '__all__'
553

554
    def __init__(self, *args, **kwargs):
555
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Gabriel Detraz committed
556 557 558 559 560
        super(EditOuverturePortListForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )