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 21.3 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
from django.utils.translation import ugettext_lazy as _
41

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

Gabriel Detraz's avatar
Gabriel Detraz committed
45 46 47 48 49 50 51
from .models import (
    Domain,
    Machine,
    Interface,
    IpList,
    MachineType,
    Extension,
52
    SOA,
Gabriel Detraz's avatar
Gabriel Detraz committed
53
    Mx,
Gabriel Detraz's avatar
Gabriel Detraz committed
54
    Txt,
Charlie Jacomme's avatar
Charlie Jacomme committed
55
    DName,
Gabriel Detraz's avatar
Gabriel Detraz committed
56
    Ns,
57
    Role,
Gabriel Detraz's avatar
Gabriel Detraz committed
58 59
    Service,
    Vlan,
Gabriel Detraz's avatar
Gabriel Detraz committed
60
    Srv,
61
    SshFp,
Gabriel Detraz's avatar
Gabriel Detraz committed
62 63 64
    Nas,
    IpType,
    OuverturePortList,
65
    Ipv6List,
Gabriel Detraz's avatar
Gabriel Detraz committed
66 67
)

68

69
class EditMachineForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
70
    """Formulaire d'édition d'une machine"""
71 72 73 74 75
    class Meta:
        model = Machine
        fields = '__all__'

    def __init__(self, *args, **kwargs):
76
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
77
        super(EditMachineForm, self).__init__(*args, prefix=prefix, **kwargs)
78
        self.fields['name'].label = _("Machine name")
79

Gabriel Detraz's avatar
Gabriel Detraz committed
80

81
class NewMachineForm(EditMachineForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
82
    """Creation d'une machine, ne renseigne que le nom"""
83
    class Meta(EditMachineForm.Meta):
84
        fields = ['name']
85

Gabriel Detraz's avatar
Gabriel Detraz committed
86

87
class EditInterfaceForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
88
    """Edition d'une interface. Edition complète"""
89 90
    class Meta:
        model = Interface
91
        fields = ['machine', 'type', 'ipv4', 'mac_address', 'details']
92 93

    def __init__(self, *args, **kwargs):
94
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
95
        user = kwargs.get('user')
96
        super(EditInterfaceForm, self).__init__(*args, prefix=prefix, **kwargs)
97 98 99
        self.fields['mac_address'].label = _("MAC address")
        self.fields['type'].label = _("Machine type")
        self.fields['type'].empty_label = _("Select a machine type")
100
        if "ipv4" in self.fields:
101
            self.fields['ipv4'].empty_label = _("Automatic IPv4 assignment")
Gabriel Detraz's avatar
Gabriel Detraz committed
102 103 104
            self.fields['ipv4'].queryset = IpList.objects.filter(
                interface__isnull=True
            )
105 106
            can_use_all_iptype, _reason = IpType.can_use_all(user)
            if not can_use_all_iptype:
107 108 109 110 111 112 113
                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
                )
114
            # Add it's own address
Gabriel Detraz's avatar
Gabriel Detraz committed
115 116 117
            self.fields['ipv4'].queryset |= IpList.objects.filter(
                interface=self.instance
            )
118
        if "machine" in self.fields:
Gabriel Detraz's avatar
Gabriel Detraz committed
119 120
            self.fields['machine'].queryset = Machine.objects.all()\
                .select_related('user')
121 122
        can_use_all_machinetype, _reason = MachineType.can_use_all(user)
        if not can_use_all_machinetype:
Gabriel Detraz's avatar
Gabriel Detraz committed
123 124 125 126
            self.fields['type'].queryset = MachineType.objects.filter(
                ip_type__in=IpType.objects.filter(need_infra=False)
            )

127

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


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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
151

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

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


168
class DelAliasForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
169 170 171
    """Suppression d'un ou plusieurs objets alias"""
    alias = forms.ModelMultipleChoiceField(
        queryset=Domain.objects.all(),
172
        label=_("Current aliases"),
Gabriel Detraz's avatar
Gabriel Detraz committed
173 174
        widget=forms.CheckboxSelectMultiple
    )
175 176 177 178

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

183

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

    def __init__(self, *args, **kwargs):
191
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
192
        super(MachineTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
193 194
        self.fields['type'].label = _("Machine type to add")
        self.fields['ip_type'].label = _("Related IP type")
195

Gabriel Detraz's avatar
Gabriel Detraz committed
196

197
class DelMachineTypeForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
198 199
    """Suppression d'un ou plusieurs machinetype"""
    machinetypes = forms.ModelMultipleChoiceField(
200
        queryset=MachineType.objects.none(),
201
        label=_("Current machine types"),
Gabriel Detraz's avatar
Gabriel Detraz committed
202 203 204
        widget=forms.CheckboxSelectMultiple
    )

205 206 207 208 209 210 211 212
    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()

213

214
class IpTypeForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
215 216
    """Formulaire d'ajout d'un iptype. Pas d'edition de l'ip de start et de
    stop après creation"""
217 218
    class Meta:
        model = IpType
219
        fields = '__all__'
Gabriel Detraz's avatar
Gabriel Detraz committed
220

221
    def __init__(self, *args, **kwargs):
222
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
223
        super(IpTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
224
        self.fields['type'].label = _("IP type to add")
225

Gabriel Detraz's avatar
Gabriel Detraz committed
226

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

236

237
class DelIpTypeForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
238 239
    """Suppression d'un ou plusieurs iptype"""
    iptypes = forms.ModelMultipleChoiceField(
240
        queryset=IpType.objects.none(),
241
        label=_("Current IP types"),
Gabriel Detraz's avatar
Gabriel Detraz committed
242 243 244
        widget=forms.CheckboxSelectMultiple
    )

245 246 247 248 249 250 251 252
    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()

253

254
class ExtensionForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
255
    """Formulaire d'ajout et edition d'une extension"""
256 257
    class Meta:
        model = Extension
258
        fields = '__all__'
259 260

    def __init__(self, *args, **kwargs):
261
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
262
        super(ExtensionForm, self).__init__(*args, prefix=prefix, **kwargs)
263 264 265 266
        self.fields['name'].label = _("Extension to add")
        self.fields['origin'].label = _("A record origin")
        self.fields['origin_v6'].label = _("AAAA record origin")
        self.fields['soa'].label = _("SOA record to use")
267

Gabriel Detraz's avatar
Gabriel Detraz committed
268

269
class DelExtensionForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
270 271
    """Suppression d'une ou plusieurs extensions"""
    extensions = forms.ModelMultipleChoiceField(
272
        queryset=Extension.objects.none(),
273
        label=_("Current extensions"),
Gabriel Detraz's avatar
Gabriel Detraz committed
274 275 276
        widget=forms.CheckboxSelectMultiple
    )

277 278 279 280 281 282 283 284
    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()

285

286
class Ipv6ListForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
287 288 289 290 291 292 293 294 295 296
    """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)


297
class SOAForm(FormRevMixin, ModelForm):
298 299 300 301 302 303 304 305 306 307
    """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)


308
class DelSOAForm(FormRevMixin, Form):
309 310
    """Suppression d'un ou plusieurs SOA"""
    soa = forms.ModelMultipleChoiceField(
311
        queryset=SOA.objects.none(),
312
        label=_("Current SOA records"),
313 314 315
        widget=forms.CheckboxSelectMultiple
    )

316 317 318 319 320 321 322 323
    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()

324

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

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

338

339
class DelMxForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
340 341
    """Suppression d'un ou plusieurs MX"""
    mx = forms.ModelMultipleChoiceField(
342
        queryset=Mx.objects.none(),
343
        label=_("Current MX records"),
Gabriel Detraz's avatar
Gabriel Detraz committed
344 345 346
        widget=forms.CheckboxSelectMultiple
    )

347 348 349 350 351 352 353 354
    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()

355

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
371

372
class DelNsForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
373 374
    """Suppresion d'un ou plusieurs NS"""
    ns = forms.ModelMultipleChoiceField(
375
        queryset=Ns.objects.none(),
376
        label=_("Current NS records"),
Gabriel Detraz's avatar
Gabriel Detraz committed
377 378 379
        widget=forms.CheckboxSelectMultiple
    )

380 381 382 383 384 385 386 387
    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()

388

389
class TxtForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
390
    """Ajout d'un txt pour une zone"""
Gabriel Detraz's avatar
Gabriel Detraz committed
391
    class Meta:
Gabriel Detraz's avatar
Gabriel Detraz committed
392
        model = Txt
Gabriel Detraz's avatar
Gabriel Detraz committed
393 394
        fields = '__all__'

395
    def __init__(self, *args, **kwargs):
396
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
397
        super(TxtForm, self).__init__(*args, prefix=prefix, **kwargs)
398

Gabriel Detraz's avatar
Gabriel Detraz committed
399

400
class DelTxtForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
401 402
    """Suppression d'un ou plusieurs TXT"""
    txt = forms.ModelMultipleChoiceField(
403
        queryset=Txt.objects.none(),
404
        label=_("Current TXT records"),
Gabriel Detraz's avatar
Gabriel Detraz committed
405 406 407
        widget=forms.CheckboxSelectMultiple
    )

408 409 410 411 412 413 414 415
    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()

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

Charlie Jacomme's avatar
Charlie Jacomme committed
417
class DNameForm(FormRevMixin, ModelForm):
Maël Kervella's avatar
Maël Kervella committed
418
    """Add a DNAME entry for a zone"""
Charlie Jacomme's avatar
Charlie Jacomme committed
419 420 421 422 423 424 425 426 427 428
    class Meta:
        model = DName
        fields = '__all__'

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


class DelDNameForm(FormRevMixin, Form):
Maël Kervella's avatar
Maël Kervella committed
429
    """Delete a set of DNAME entries"""
Charlie Jacomme's avatar
Charlie Jacomme committed
430 431
    dnames = forms.ModelMultipleChoiceField(
        queryset=Txt.objects.none(),
432
        label=_("Current DNAME records"),
Charlie Jacomme's avatar
Charlie Jacomme committed
433 434 435 436 437 438 439 440 441 442 443 444
        widget=forms.CheckboxSelectMultiple
    )

    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelDNameForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['dnames'].queryset = instances
        else:
            self.fields['dnames'].queryset = DName.objects.all()


445
class SrvForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
446 447 448 449 450 451 452 453 454 455
    """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)


456
class DelSrvForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
457 458
    """Suppression d'un ou plusieurs Srv"""
    srv = forms.ModelMultipleChoiceField(
459
        queryset=Srv.objects.none(),
460
        label=_("Current SRV records"),
Gabriel Detraz's avatar
Gabriel Detraz committed
461 462 463
        widget=forms.CheckboxSelectMultiple
    )

464 465 466 467 468 469 470 471
    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
472

473
class NasForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
474 475
    """Ajout d'un type de nas (machine d'authentification,
    swicths, bornes...)"""
476 477 478 479
    class Meta:
        model = Nas
        fields = '__all__'

480
    def __init__(self, *args, **kwargs):
481
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
482 483
        super(NasForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Gabriel Detraz committed
484

485
class DelNasForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
486 487
    """Suppression d'un ou plusieurs nas"""
    nas = forms.ModelMultipleChoiceField(
488
        queryset=Nas.objects.none(),
489
        label=_("Current NAS devices"),
Gabriel Detraz's avatar
Gabriel Detraz committed
490 491 492
        widget=forms.CheckboxSelectMultiple
    )

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

501

502
class RoleForm(FormRevMixin, ModelForm):
503
    """Add and edit role."""
504 505 506 507 508 509 510 511 512 513 514 515 516 517
    class Meta:
        model = Role
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
        super(RoleForm, self).__init__(*args, prefix=prefix, **kwargs)
        self.fields['servers'].queryset = (Interface.objects.all()
                                           .select_related(
                                               'domain__extension'
                                           ))


class DelRoleForm(FormRevMixin, Form):
518
    """Deletion of one or several roles."""
519 520
    role = forms.ModelMultipleChoiceField(
        queryset=Role.objects.none(),
521
        label=_("Current roles"),
522 523 524 525 526 527 528 529 530
        widget=forms.CheckboxSelectMultiple
    )

    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelRoleForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['role'].queryset = instances
        else:
531
            self.fields['role'].queryset = Role.objects.all()
532 533


534
class ServiceForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
535
    """Ajout et edition d'une classe de service : dns, dhcp, etc"""
536
    class Meta:
537 538 539
        model = Service
        fields = '__all__'

540
    def __init__(self, *args, **kwargs):
541
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
542
        super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
543 544 545 546
        self.fields['servers'].queryset = (Interface.objects.all()
                                           .select_related(
                                               'domain__extension'
                                           ))
547

548
    def save(self, commit=True):
549 550
        # TODO : None of the parents of ServiceForm use the commit
        # parameter in .save()
551 552 553 554 555 556
        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
557

558
class DelServiceForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
559 560
    """Suppression d'un ou plusieurs service"""
    service = forms.ModelMultipleChoiceField(
561
        queryset=Service.objects.none(),
562
        label=_("Current services"),
Gabriel Detraz's avatar
Gabriel Detraz committed
563 564 565
        widget=forms.CheckboxSelectMultiple
    )

566 567 568 569 570 571 572 573
    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()

574

575
class VlanForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
576
    """Ajout d'un vlan : id, nom"""
577 578 579 580
    class Meta:
        model = Vlan
        fields = '__all__'

581
    def __init__(self, *args, **kwargs):
582
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
583 584
        super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Gabriel Detraz committed
585

586
class DelVlanForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
587 588
    """Suppression d'un ou plusieurs vlans"""
    vlan = forms.ModelMultipleChoiceField(
589
        queryset=Vlan.objects.none(),
590
        label=_("Current VLANs"),
Gabriel Detraz's avatar
Gabriel Detraz committed
591 592 593
        widget=forms.CheckboxSelectMultiple
    )

594 595 596 597 598 599 600 601
    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()

602

603
class EditOuverturePortConfigForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
604 605
    """Edition de la liste des profils d'ouverture de ports
    pour l'interface"""
606
    class Meta:
607 608
        model = Interface
        fields = ['port_lists']
609

610
    def __init__(self, *args, **kwargs):
611
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Gabriel Detraz committed
612 613 614 615 616 617
        super(EditOuverturePortConfigForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )

618

619
class EditOuverturePortListForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
620 621
    """Edition de la liste des ports et profils d'ouverture
    des ports"""
622
    class Meta:
623
        model = OuverturePortList
624
        fields = '__all__'
625

626
    def __init__(self, *args, **kwargs):
627
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Gabriel Detraz committed
628 629 630 631 632
        super(EditOuverturePortListForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
633 634

        
635 636
class SshFpForm(FormRevMixin, ModelForm):
    """Edits a SSHFP record."""
637
    class Meta:
638
        model = SshFp
639 640 641 642
        exclude = ('machine',)

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
643
        super(SshFpForm, self).__init__(
644 645 646 647
            *args,
            prefix=prefix,
            **kwargs
        )
648