forms.py 20.3 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
lhark's avatar
lhark committed
2 3 4 5 6 7 8
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
9
# Copyright © 2017  Maël Kervella
lhark's avatar
lhark committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
24 25 26 27 28 29 30 31 32 33 34
"""
Formulaires d'ajout, edition et suppressions de :
    - machines
    - interfaces
    - domain (noms de machine)
    - alias (cname)
    - service (dhcp, dns..)
    - ns (serveur dns)
    - mx (serveur mail)
    - ports ouverts et profils d'ouverture par interface
"""
lhark's avatar
lhark committed
35

36 37
from __future__ import unicode_literals

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

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

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

66

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

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

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
78

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

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
84

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

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

chirac's avatar
chirac committed
126

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


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

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

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
150

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

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


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

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

182

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

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

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
195

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

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

chirac's avatar
chirac committed
212

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

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

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
226

227
class EditIpTypeForm(IpTypeForm):
Gabriel Detraz's avatar
Doc  
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):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
231 232 233
        fields = ['extension', 'type', 'need_infra', 'prefix_v6', 'vlan',
                  'ouverture_ports']

chirac's avatar
chirac committed
234

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

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

chirac's avatar
chirac committed
251

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

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

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
266

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

Gabriel Detraz's avatar
Gabriel Detraz committed
275 276 277 278 279 280 281 282
    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()

283

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


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


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

Gabriel Detraz's avatar
Gabriel Detraz committed
314 315 316 317 318 319 320 321
    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()

322

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

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

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
345 346 347 348 349 350 351 352
    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()

353

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

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

Gabriel Detraz's avatar
Fix bug  
Gabriel Detraz committed
369

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

Gabriel Detraz's avatar
Gabriel Detraz committed
378 379 380 381 382 383 384 385
    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()

386

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

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

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
397

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

Gabriel Detraz's avatar
Gabriel Detraz committed
406 407 408 409 410 411 412 413
    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
414

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


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


454
class DelSrvForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Fix #52  
Gabriel Detraz committed
455 456
    """Suppression d'un ou plusieurs Srv"""
    srv = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
457
        queryset=Srv.objects.none(),
Gabriel Detraz's avatar
Fix #52  
Gabriel Detraz committed
458 459 460 461
        label="Enregistrements Srv actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
462 463 464 465 466 467 468 469
    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
470

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

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

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
482

483
class DelNasForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
484 485
    """Suppression d'un ou plusieurs nas"""
    nas = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
486
        queryset=Nas.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
487 488 489 490
        label="Enregistrements Nas actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
491 492 493 494 495 496 497 498
    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()

499

500
class ServiceForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
501
    """Ajout et edition d'une classe de service : dns, dhcp, etc"""
502
    class Meta:
503 504 505
        model = Service
        fields = '__all__'

506
    def __init__(self, *args, **kwargs):
507
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
508
        super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
Maël Kervella's avatar
Maël Kervella committed
509 510 511 512
        self.fields['servers'].queryset = (Interface.objects.all()
                                           .select_related(
                                               'domain__extension'
                                           ))
513

514
    def save(self, commit=True):
Maël Kervella's avatar
Maël Kervella committed
515 516
        # TODO : None of the parents of ServiceForm use the commit
        # parameter in .save()
517 518 519 520 521 522
        instance = super(ServiceForm, self).save(commit=False)
        if commit:
            instance.save()
        instance.process_link(self.cleaned_data.get('servers'))
        return instance

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
523

524
class DelServiceForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
525 526
    """Suppression d'un ou plusieurs service"""
    service = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
527
        queryset=Service.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
528 529 530 531
        label="Services actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
532 533 534 535 536 537 538 539
    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()

540

541
class VlanForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
542
    """Ajout d'un vlan : id, nom"""
543 544 545 546
    class Meta:
        model = Vlan
        fields = '__all__'

547
    def __init__(self, *args, **kwargs):
548
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
549 550
        super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
551

552
class DelVlanForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
553 554
    """Suppression d'un ou plusieurs vlans"""
    vlan = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
555
        queryset=Vlan.objects.none(),
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
556 557 558 559
        label="Vlan actuels",
        widget=forms.CheckboxSelectMultiple
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
560 561 562 563 564 565 566 567
    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()

568

569
class EditOuverturePortConfigForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
570 571
    """Edition de la liste des profils d'ouverture de ports
    pour l'interface"""
572
    class Meta:
573 574
        model = Interface
        fields = ['port_lists']
575

576
    def __init__(self, *args, **kwargs):
577
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
578 579 580 581 582 583
        super(EditOuverturePortConfigForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )

584

585
class EditOuverturePortListForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
586 587
    """Edition de la liste des ports et profils d'ouverture
    des ports"""
588
    class Meta:
589
        model = OuverturePortList
590
        fields = '__all__'
591

592
    def __init__(self, *args, **kwargs):
593
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
594 595 596 597 598
        super(EditOuverturePortListForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
599 600 601

        
class SshFingerprintForm(FormRevMixin, ModelForm):
Maël Kervella's avatar
Maël Kervella committed
602
    """Edits a SSH fingerprint."""
603 604 605 606 607 608 609 610 611 612 613
    class Meta:
        model = SshFingerprint
        exclude = ('machine',)

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