Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

forms.py 19 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 98
            self.fields['ipv4'].empty_label = ("Assignation automatique de "
                                               "l'ipv4")
Gabriel Detraz's avatar
Gabriel Detraz committed
99 100 101
            self.fields['ipv4'].queryset = IpList.objects.filter(
                interface__isnull=True
            )
102 103
            can_use_all_iptype, _reason = IpType.can_use_all(user)
            if not can_use_all_iptype:
104 105 106 107 108 109 110
                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
                )
111
            # Add it's own address
Gabriel Detraz's avatar
Gabriel Detraz committed
112 113 114
            self.fields['ipv4'].queryset |= IpList.objects.filter(
                interface=self.instance
            )
115
        if "machine" in self.fields:
Gabriel Detraz's avatar
Gabriel Detraz committed
116 117
            self.fields['machine'].queryset = Machine.objects.all()\
                .select_related('user')
118 119
        can_use_all_machinetype, _reason = MachineType.can_use_all(user)
        if not can_use_all_machinetype:
Gabriel Detraz's avatar
Gabriel Detraz committed
120 121 122 123
            self.fields['type'].queryset = MachineType.objects.filter(
                ip_type__in=IpType.objects.filter(need_infra=False)
            )

124

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


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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
148

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

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


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

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

180

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
193

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

202 203 204 205 206 207 208 209
    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()

210

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
224

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

232

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

241 242 243 244 245 246 247 248
    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()

249

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
264

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

273 274 275 276 277 278 279 280
    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()

281

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


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


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

312 313 314 315 316 317 318 319
    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()

320

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

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

334

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

343 344 345 346 347 348 349 350
    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()

351

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
367

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

376 377 378 379 380 381 382 383
    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()

384

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
395

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

404 405 406 407 408 409 410 411 412
    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()


413
class SrvForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
414 415 416 417 418 419 420 421 422 423
    """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)


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

432 433 434 435 436 437 438 439
    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
440

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
452

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

461 462 463 464 465 466 467 468
    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()

469

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

476
    def __init__(self, *args, **kwargs):
477
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
478
        super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
479 480 481 482
        self.fields['servers'].queryset = (Interface.objects.all()
                                           .select_related(
                                               'domain__extension'
                                           ))
483

484
    def save(self, commit=True):
485 486
        # TODO : None of the parents of ServiceForm use the commit
        # parameter in .save()
487 488 489 490 491 492
        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
493

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

502 503 504 505 506 507 508 509
    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()

510

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
521

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

530 531 532 533 534 535 536 537
    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()

538

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

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

554

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

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