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.5 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 _l
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 79
        self.fields['name'].label = 'Nom de la machine'

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

128

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


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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
152

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

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


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

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

184

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
197

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

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

214

215
class IpTypeForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
216 217
    """Formulaire d'ajout d'un iptype. Pas d'edition de l'ip de start et de
    stop après creation"""
218 219
    class Meta:
        model = IpType
Gabriel Detraz's avatar
Gabriel Detraz committed
220
        fields = ['type', 'extension', 'need_infra', 'domaine_ip_start',
221
                  'domaine_ip_stop', 'dnssec_reverse_v4', 'prefix_v6',
222 223
                  'prefix_v6_length','dnssec_reverse_v6', 'vlan',
                  'ouverture_ports']
Gabriel Detraz's avatar
Gabriel Detraz committed
224

225
    def __init__(self, *args, **kwargs):
226
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
227
        super(IpTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
228 229
        self.fields['type'].label = 'Type ip à ajouter'

Gabriel Detraz's avatar
Gabriel Detraz committed
230

231
class EditIpTypeForm(IpTypeForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
232 233
    """Edition d'un iptype. Pas d'edition du rangev4 possible, car il faudrait
    synchroniser les objets iplist"""
234
    class Meta(IpTypeForm.Meta):
235 236
        fields = ['extension', 'type', 'need_infra', 'prefix_v6', 'prefix_v6_length',
                  'vlan', 'dnssec_reverse_v4', 'dnssec_reverse_v6',
Gabriel Detraz's avatar
Gabriel Detraz committed
237 238
                  'ouverture_ports']

239

240
class DelIpTypeForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
241 242
    """Suppression d'un ou plusieurs iptype"""
    iptypes = forms.ModelMultipleChoiceField(
243
        queryset=IpType.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
244 245 246 247
        label="Types d'ip actuelles",
        widget=forms.CheckboxSelectMultiple
    )

248 249 250 251 252 253 254 255
    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()

256

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

    def __init__(self, *args, **kwargs):
264
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
265
        super(ExtensionForm, self).__init__(*args, prefix=prefix, **kwargs)
chirac's avatar
chirac committed
266
        self.fields['name'].label = 'Extension à ajouter'
267
        self.fields['origin'].label = 'Enregistrement A origin'
268
        self.fields['origin_v6'].label = 'Enregistrement AAAA origin'
269
        self.fields['soa'].label = 'En-tête SOA à utiliser'
270

Gabriel Detraz's avatar
Gabriel Detraz committed
271

272
class DelExtensionForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
273 274
    """Suppression d'une ou plusieurs extensions"""
    extensions = forms.ModelMultipleChoiceField(
275
        queryset=Extension.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
276 277 278 279
        label="Extensions actuelles",
        widget=forms.CheckboxSelectMultiple
    )

280 281 282 283 284 285 286 287
    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()

288

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


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


311
class DelSOAForm(FormRevMixin, Form):
312 313
    """Suppression d'un ou plusieurs SOA"""
    soa = forms.ModelMultipleChoiceField(
314
        queryset=SOA.objects.none(),
315 316 317 318
        label="SOA actuels",
        widget=forms.CheckboxSelectMultiple
    )

319 320 321 322 323 324 325 326
    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()

327

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

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

341

342
class DelMxForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
343 344
    """Suppression d'un ou plusieurs MX"""
    mx = forms.ModelMultipleChoiceField(
345
        queryset=Mx.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
346 347 348 349
        label="MX actuels",
        widget=forms.CheckboxSelectMultiple
    )

350 351 352 353 354 355 356 357
    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()

358

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
374

375
class DelNsForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
376 377
    """Suppresion d'un ou plusieurs NS"""
    ns = forms.ModelMultipleChoiceField(
378
        queryset=Ns.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
379 380 381 382
        label="Enregistrements NS actuels",
        widget=forms.CheckboxSelectMultiple
    )

383 384 385 386 387 388 389 390
    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()

391

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

398
    def __init__(self, *args, **kwargs):
399
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
400
        super(TxtForm, self).__init__(*args, prefix=prefix, **kwargs)
401

Gabriel Detraz's avatar
Gabriel Detraz committed
402

403
class DelTxtForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
404 405
    """Suppression d'un ou plusieurs TXT"""
    txt = forms.ModelMultipleChoiceField(
406
        queryset=Txt.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
407 408 409 410
        label="Enregistrements Txt actuels",
        widget=forms.CheckboxSelectMultiple
    )

411 412 413 414 415 416 417 418
    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
419

Charlie Jacomme's avatar
Charlie Jacomme committed
420
class DNameForm(FormRevMixin, ModelForm):
Maël Kervella's avatar
Maël Kervella committed
421
    """Add a DNAME entry for a zone"""
Charlie Jacomme's avatar
Charlie Jacomme committed
422 423 424 425 426 427 428 429 430 431
    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
432
    """Delete a set of DNAME entries"""
Charlie Jacomme's avatar
Charlie Jacomme committed
433 434
    dnames = forms.ModelMultipleChoiceField(
        queryset=Txt.objects.none(),
Maël Kervella's avatar
Maël Kervella committed
435
        label="Existing DNAME entries",
Charlie Jacomme's avatar
Charlie Jacomme committed
436 437 438 439 440 441 442 443 444 445 446 447
        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()


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


459
class DelSrvForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
460 461
    """Suppression d'un ou plusieurs Srv"""
    srv = forms.ModelMultipleChoiceField(
462
        queryset=Srv.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
463 464 465 466
        label="Enregistrements Srv actuels",
        widget=forms.CheckboxSelectMultiple
    )

467 468 469 470 471 472 473 474
    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
475

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

483
    def __init__(self, *args, **kwargs):
484
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
485 486
        super(NasForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Gabriel Detraz committed
487

488
class DelNasForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
489 490
    """Suppression d'un ou plusieurs nas"""
    nas = forms.ModelMultipleChoiceField(
491
        queryset=Nas.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
492 493 494 495
        label="Enregistrements Nas actuels",
        widget=forms.CheckboxSelectMultiple
    )

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

504

505
class RoleForm(FormRevMixin, ModelForm):
506
    """Add and edit role."""
507 508 509 510 511 512 513 514 515 516 517 518 519 520
    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):
521
    """Deletion of one or several roles."""
522 523
    role = forms.ModelMultipleChoiceField(
        queryset=Role.objects.none(),
524
        label=_l("Current roles"),
525 526 527 528 529 530 531 532 533
        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:
534
            self.fields['role'].queryset = Role.objects.all()
535 536


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

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

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

561
class DelServiceForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
562 563
    """Suppression d'un ou plusieurs service"""
    service = forms.ModelMultipleChoiceField(
564
        queryset=Service.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
565 566 567 568
        label="Services actuels",
        widget=forms.CheckboxSelectMultiple
    )

569 570 571 572 573 574 575 576
    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()

577

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

584
    def __init__(self, *args, **kwargs):
585
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
586 587
        super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Gabriel Detraz committed
588

589
class DelVlanForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
590 591
    """Suppression d'un ou plusieurs vlans"""
    vlan = forms.ModelMultipleChoiceField(
592
        queryset=Vlan.objects.none(),
Gabriel Detraz's avatar
Gabriel Detraz committed
593 594 595 596
        label="Vlan actuels",
        widget=forms.CheckboxSelectMultiple
    )

597 598 599 600 601 602 603 604
    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()

605

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

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

621

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

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

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

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