forms.py 21.5 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
2 3 4 5 6
# 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
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
7
# Copyright © 2017  Lara Kermarec
8
# 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
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
"""
35

36 37
from __future__ import unicode_literals

chirac's avatar
chirac committed
38
from django import forms
erdnaxe's avatar
erdnaxe committed
39
from django.forms import ModelForm, Form
40
from django.utils.translation import ugettext_lazy as _
chirac's avatar
chirac committed
41

42
from re2o.field_permissions import FieldPermissionFormMixin
43
from re2o.mixins import FormRevMixin
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
    Ns,
Charlie Jacomme's avatar
Charlie Jacomme committed
56
    Role,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
57 58
    Service,
    Vlan,
Gabriel Detraz's avatar
Gabriel Detraz committed
59
    Srv,
60
    SshFp,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
61 62 63
    Nas,
    IpType,
    OuverturePortList,
64
    Ipv6List,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
65 66
)

67

68
class EditMachineForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
69
    """Formulaire d'édition d'une machine"""
erdnaxe's avatar
erdnaxe committed
70

chirac's avatar
chirac committed
71 72
    class Meta:
        model = Machine
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
73
        fields = "__all__"
chirac's avatar
chirac committed
74 75

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

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
80

chirac's avatar
chirac committed
81
class NewMachineForm(EditMachineForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
82
    """Creation d'une machine, ne renseigne que le nom"""
erdnaxe's avatar
erdnaxe committed
83

chirac's avatar
chirac committed
84
    class Meta(EditMachineForm.Meta):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
85
        fields = ["name"]
chirac's avatar
chirac committed
86

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
87

88
class EditInterfaceForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
89
    """Edition d'une interface. Edition complète"""
erdnaxe's avatar
erdnaxe committed
90

chirac's avatar
chirac committed
91 92
    class Meta:
        model = Interface
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
93
        fields = ["machine", "machine_type", "ipv4", "mac_address", "details"]
chirac's avatar
chirac committed
94 95

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

chirac's avatar
chirac committed
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"""
erdnaxe's avatar
erdnaxe committed
132

133
    class Meta(EditInterfaceForm.Meta):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
134
        fields = ["machine_type", "ipv4", "mac_address", "details"]
135 136


Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
137
class AliasForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
138
    """Ajout d'un alias (et edition), CNAME, contenant nom et extension"""
erdnaxe's avatar
erdnaxe committed
139

140
    class Meta:
chirac's avatar
chirac committed
141
        model = Domain
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
142
        fields = ["name", "extension", "ttl"]
143

144
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
145 146
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
        user = kwargs["user"]
147
        super(AliasForm, self).__init__(*args, prefix=prefix, **kwargs)
klafyvel's avatar
klafyvel committed
148
        can_use_all, _reason, _permissions = Extension.can_use_all(user)
Gabriel Detraz's avatar
Gabriel Detraz committed
149
        if not can_use_all:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
150
            self.fields["extension"].queryset = Extension.objects.filter(
151
                need_infra=False
chirac's avatar
chirac committed
152
            )
153

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
154

Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
155
class DomainForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
156
    """Ajout et edition d'un enregistrement de nom, relié à interface"""
erdnaxe's avatar
erdnaxe committed
157

chirac's avatar
chirac committed
158 159
    class Meta:
        model = Domain
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
160
        fields = ["name", "ttl"]
161 162

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
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
    """Suppression d'un ou plusieurs objets alias"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
169

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
170 171
    alias = forms.ModelMultipleChoiceField(
        queryset=Domain.objects.all(),
172
        label=_("Current aliases"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
173
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
174
    )
175 176

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

183

184
class MachineTypeForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
185
    """Ajout et edition d'un machinetype, relié à un iptype"""
erdnaxe's avatar
erdnaxe committed
186

chirac's avatar
chirac committed
187 188
    class Meta:
        model = MachineType
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
189
        fields = ["name", "ip_type"]
chirac's avatar
chirac committed
190 191

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
192
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
193
        super(MachineTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
194 195
        self.fields["name"].label = _("Machine type to add")
        self.fields["ip_type"].label = _("Related IP type")
chirac's avatar
chirac committed
196

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
197

198
class DelMachineTypeForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
199
    """Suppression d'un ou plusieurs machinetype"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
200

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
201
    machinetypes = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
202
        queryset=MachineType.objects.none(),
203
        label=_("Current machine types"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
204
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
205 206
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
207
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
208
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
209 210
        super(DelMachineTypeForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
211
            self.fields["machinetypes"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
212
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
213
            self.fields["machinetypes"].queryset = MachineType.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
214

chirac's avatar
chirac committed
215

216
class IpTypeForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
217 218
    """Formulaire d'ajout d'un iptype. Pas d'edition de l'ip de start et de
    stop après creation"""
erdnaxe's avatar
erdnaxe committed
219

chirac's avatar
chirac committed
220 221
    class Meta:
        model = IpType
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
222
        fields = "__all__"
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
223

chirac's avatar
chirac committed
224
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
225
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
226
        super(IpTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
227
        self.fields["name"].label = _("IP type to add")
chirac's avatar
chirac committed
228

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
229

230
class EditIpTypeForm(IpTypeForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
231 232
    """Edition d'un iptype. Pas d'edition du rangev4 possible, car il faudrait
    synchroniser les objets iplist"""
erdnaxe's avatar
erdnaxe committed
233

234
    class Meta(IpTypeForm.Meta):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
235 236 237 238 239 240 241 242 243 244 245 246 247
        fields = [
            "extension",
            "name",
            "need_infra",
            "domaine_ip_network",
            "domaine_ip_netmask",
            "prefix_v6",
            "prefix_v6_length",
            "vlan",
            "reverse_v4",
            "reverse_v6",
            "ouverture_ports",
        ]
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
248

chirac's avatar
chirac committed
249

250
class DelIpTypeForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
251
    """Suppression d'un ou plusieurs iptype"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
252

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
253
    iptypes = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
254
        queryset=IpType.objects.none(),
255
        label=_("Current IP types"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
256
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
257 258
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
259
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
260
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
261 262
        super(DelIpTypeForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
263
            self.fields["iptypes"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
264
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
265
            self.fields["iptypes"].queryset = IpType.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
266

chirac's avatar
chirac committed
267

268
class ExtensionForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
269
    """Formulaire d'ajout et edition d'une extension"""
erdnaxe's avatar
erdnaxe committed
270

271 272
    class Meta:
        model = Extension
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
273
        fields = "__all__"
274 275

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
276
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
277
        super(ExtensionForm, self).__init__(*args, prefix=prefix, **kwargs)
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
278 279 280 281 282
        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")
        self.fields["dnssec"].label = _("Sign with DNSSEC")
283

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
284

285
class DelExtensionForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
286
    """Suppression d'une ou plusieurs extensions"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
287

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
288
    extensions = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
289
        queryset=Extension.objects.none(),
290
        label=_("Current extensions"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
291
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
292 293
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
294
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
295
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
296 297
        super(DelExtensionForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
298
            self.fields["extensions"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
299
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
300
            self.fields["extensions"].queryset = Extension.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
301

302

303
class Ipv6ListForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
304
    """Gestion des ipv6 d'une machine"""
erdnaxe's avatar
erdnaxe committed
305

306 307
    class Meta:
        model = Ipv6List
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
308
        fields = ["ipv6", "slaac_ip"]
309 310

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
311
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
312 313 314
        super(Ipv6ListForm, self).__init__(*args, prefix=prefix, **kwargs)


315
class SOAForm(FormRevMixin, ModelForm):
316
    """Ajout et edition d'un SOA"""
erdnaxe's avatar
erdnaxe committed
317

318 319
    class Meta:
        model = SOA
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
320
        fields = "__all__"
321 322

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
323
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
324 325 326
        super(SOAForm, self).__init__(*args, prefix=prefix, **kwargs)


327
class DelSOAForm(FormRevMixin, Form):
328
    """Suppression d'un ou plusieurs SOA"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
329

330
    soa = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
331
        queryset=SOA.objects.none(),
332
        label=_("Current SOA records"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
333
        widget=forms.CheckboxSelectMultiple,
334 335
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
336
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
337
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
338 339
        super(DelSOAForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
340
            self.fields["soa"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
341
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
342
            self.fields["soa"].queryset = SOA.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
343

344

345
class MxForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
346
    """Ajout et edition d'un MX"""
erdnaxe's avatar
erdnaxe committed
347

348 349
    class Meta:
        model = Mx
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
350
        fields = ["zone", "priority", "name", "ttl"]
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
351

Gabriel Detraz's avatar
Gabriel Detraz committed
352
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
353
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
354
        super(MxForm, self).__init__(*args, prefix=prefix, **kwargs)
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
355
        self.fields["name"].queryset = Domain.objects.exclude(
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
356
            interface_parent=None
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
357
        ).select_related("extension")
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
358

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

360
class DelMxForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
361
    """Suppression d'un ou plusieurs MX"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
362

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
363
    mx = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
364
        queryset=Mx.objects.none(),
365
        label=_("Current MX records"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
366
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
367 368
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
369
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
370
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
371 372
        super(DelMxForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
373
            self.fields["mx"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
374
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
375
            self.fields["mx"].queryset = Mx.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
376

377

378
class NsForm(FormRevMixin, ModelForm):
chirac's avatar
chirac committed
379 380 381
    """Ajout d'un NS pour une zone
    On exclue les CNAME dans les objets domain (interdit par la rfc)
    donc on prend uniquemet """
erdnaxe's avatar
erdnaxe committed
382

383 384
    class Meta:
        model = Ns
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
385
        fields = ["zone", "ns", "ttl"]
386

Gabriel Detraz's avatar
Gabriel Detraz committed
387
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
388
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
389
        super(NsForm, self).__init__(*args, prefix=prefix, **kwargs)
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
390
        self.fields["ns"].queryset = Domain.objects.exclude(
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
391
            interface_parent=None
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
392
        ).select_related("extension")
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
393

Gabriel Detraz's avatar
Gabriel Detraz committed
394

395
class DelNsForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
396
    """Suppresion d'un ou plusieurs NS"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
397

398
    nss = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
399
        queryset=Ns.objects.none(),
400
        label=_("Current NS records"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
401
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
402 403
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
404
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
405
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
406 407
        super(DelNsForm, self).__init__(*args, **kwargs)
        if instances:
408
            self.fields["nss"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
409
        else:
410
            self.fields["nss"].queryset = Ns.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
411

412

413
class TxtForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
414
    """Ajout d'un txt pour une zone"""
erdnaxe's avatar
erdnaxe committed
415

Gabriel Detraz's avatar
Gabriel Detraz committed
416
    class Meta:
Gabriel Detraz's avatar
Gabriel Detraz committed
417
        model = Txt
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
418
        fields = "__all__"
Gabriel Detraz's avatar
Gabriel Detraz committed
419

420
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
421
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
422
        super(TxtForm, self).__init__(*args, prefix=prefix, **kwargs)
423

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
424

425
class DelTxtForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
426
    """Suppression d'un ou plusieurs TXT"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
427

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
428
    txt = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
429
        queryset=Txt.objects.none(),
430
        label=_("Current TXT records"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
431
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
432 433
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
434
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
435
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
436 437
        super(DelTxtForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
438
            self.fields["txt"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
439
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
440
            self.fields["txt"].queryset = Txt.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
441

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

Charlie Jacomme's avatar
Charlie Jacomme committed
443
class DNameForm(FormRevMixin, ModelForm):
Maël Kervella's avatar
Maël Kervella committed
444
    """Add a DNAME entry for a zone"""
erdnaxe's avatar
erdnaxe committed
445

Charlie Jacomme's avatar
Charlie Jacomme committed
446 447
    class Meta:
        model = DName
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
448
        fields = "__all__"
Charlie Jacomme's avatar
Charlie Jacomme committed
449 450

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
451
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
Charlie Jacomme's avatar
Charlie Jacomme committed
452 453 454 455
        super(DNameForm, self).__init__(*args, prefix=prefix, **kwargs)


class DelDNameForm(FormRevMixin, Form):
Maël Kervella's avatar
Maël Kervella committed
456
    """Delete a set of DNAME entries"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
457

Charlie Jacomme's avatar
Charlie Jacomme committed
458 459
    dnames = forms.ModelMultipleChoiceField(
        queryset=Txt.objects.none(),
460
        label=_("Current DNAME records"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
461
        widget=forms.CheckboxSelectMultiple,
Charlie Jacomme's avatar
Charlie Jacomme committed
462 463 464
    )

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
465
        instances = kwargs.pop("instances", None)
Charlie Jacomme's avatar
Charlie Jacomme committed
466 467
        super(DelDNameForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
468
            self.fields["dnames"].queryset = instances
Charlie Jacomme's avatar
Charlie Jacomme committed
469
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
470
            self.fields["dnames"].queryset = DName.objects.all()
Charlie Jacomme's avatar
Charlie Jacomme committed
471 472


473
class SrvForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
474
    """Ajout d'un srv pour une zone"""
erdnaxe's avatar
erdnaxe committed
475

Gabriel Detraz's avatar
Gabriel Detraz committed
476 477
    class Meta:
        model = Srv
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
478
        fields = "__all__"
Gabriel Detraz's avatar
Gabriel Detraz committed
479 480

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
481
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
Gabriel Detraz's avatar
Gabriel Detraz committed
482 483 484
        super(SrvForm, self).__init__(*args, prefix=prefix, **kwargs)


485
class DelSrvForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Gabriel Detraz committed
486
    """Suppression d'un ou plusieurs Srv"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
487

Gabriel Detraz's avatar
Gabriel Detraz committed
488
    srv = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
489
        queryset=Srv.objects.none(),
490
        label=_("Current SRV records"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
491
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Gabriel Detraz committed
492 493
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
494
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
495
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
496 497
        super(DelSrvForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
498
            self.fields["srv"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
499
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
500
            self.fields["srv"].queryset = Srv.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
501

Gabriel Detraz's avatar
Gabriel Detraz committed
502

503
class NasForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
504 505
    """Ajout d'un type de nas (machine d'authentification,
    swicths, bornes...)"""
erdnaxe's avatar
erdnaxe committed
506

507 508
    class Meta:
        model = Nas
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
509
        fields = "__all__"
510

511
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
512
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
513 514
        super(NasForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
515

516
class DelNasForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
517
    """Suppression d'un ou plusieurs nas"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
518

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
519
    nas = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
520
        queryset=Nas.objects.none(),
521
        label=_("Current NAS devices"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
522
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
523 524
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
525
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
526
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
527 528
        super(DelNasForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
529
            self.fields["nas"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
530
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
531
            self.fields["nas"].queryset = Nas.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
532

533

Charlie Jacomme's avatar
Charlie Jacomme committed
534
class RoleForm(FormRevMixin, ModelForm):
535
    """Add and edit role."""
erdnaxe's avatar
erdnaxe committed
536

Charlie Jacomme's avatar
Charlie Jacomme committed
537 538
    class Meta:
        model = Role
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
539
        fields = "__all__"
Charlie Jacomme's avatar
Charlie Jacomme committed
540 541

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
542
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
Charlie Jacomme's avatar
Charlie Jacomme committed
543
        super(RoleForm, self).__init__(*args, prefix=prefix, **kwargs)
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
544 545 546
        self.fields["servers"].queryset = Interface.objects.all().select_related(
            "domain__extension"
        )
Charlie Jacomme's avatar
Charlie Jacomme committed
547 548 549


class DelRoleForm(FormRevMixin, Form):
550
    """Deletion of one or several roles."""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
551

Charlie Jacomme's avatar
Charlie Jacomme committed
552 553
    role = forms.ModelMultipleChoiceField(
        queryset=Role.objects.none(),
554
        label=_("Current roles"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
555
        widget=forms.CheckboxSelectMultiple,
Charlie Jacomme's avatar
Charlie Jacomme committed
556 557 558
    )

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
559
        instances = kwargs.pop("instances", None)
Charlie Jacomme's avatar
Charlie Jacomme committed
560 561
        super(DelRoleForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
562
            self.fields["role"].queryset = instances
Charlie Jacomme's avatar
Charlie Jacomme committed
563
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
564
            self.fields["role"].queryset = Role.objects.all()
Charlie Jacomme's avatar
Charlie Jacomme committed
565 566


567
class ServiceForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
568
    """Ajout et edition d'une classe de service : dns, dhcp, etc"""
erdnaxe's avatar
erdnaxe committed
569

570
    class Meta:
571
        model = Service
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
572
        fields = "__all__"
573

574
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
575
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
576
        super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
577 578 579
        self.fields["servers"].queryset = Interface.objects.all().select_related(
            "domain__extension"
        )
580

581
    def save(self, commit=True):
582 583
        # TODO : None of the parents of ServiceForm use the commit
        # parameter in .save()
584 585 586
        instance = super(ServiceForm, self).save(commit=False)
        if commit:
            instance.save()
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
587
        instance.process_link(self.cleaned_data.get("servers"))
588 589
        return instance

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
590

591
class DelServiceForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
592
    """Suppression d'un ou plusieurs service"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
593

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
594
    service = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
595
        queryset=Service.objects.none(),
596
        label=_("Current services"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
597
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
598 599
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
600
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
601
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
602 603
        super(DelServiceForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
604
            self.fields["service"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
605
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
606
            self.fields["service"].queryset = Service.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
607

608

609
class VlanForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
610
    """Ajout d'un vlan : id, nom"""
erdnaxe's avatar
erdnaxe committed
611

612 613
    class Meta:
        model = Vlan
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
614
        fields = ["vlan_id", "name", "comment"]
615

616
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
617
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
618 619
        super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs)

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
620

621 622
class EditOptionVlanForm(FormRevMixin, ModelForm):
    """Ajout d'un vlan : id, nom"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
623

624 625
    class Meta:
        model = Vlan
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
626
        fields = ["dhcp_snooping", "dhcpv6_snooping", "arp_protect", "igmp", "mld"]
627 628

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
629
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
630 631 632
        super(EditOptionVlanForm, self).__init__(*args, prefix=prefix, **kwargs)


633
class DelVlanForm(FormRevMixin, Form):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
634
    """Suppression d'un ou plusieurs vlans"""
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
635

Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
636
    vlan = forms.ModelMultipleChoiceField(
Gabriel Detraz's avatar
Gabriel Detraz committed
637
        queryset=Vlan.objects.none(),
638
        label=_("Current VLANs"),
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
639
        widget=forms.CheckboxSelectMultiple,
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
640 641
    )

Gabriel Detraz's avatar
Gabriel Detraz committed
642
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
643
        instances = kwargs.pop("instances", None)
Gabriel Detraz's avatar
Gabriel Detraz committed
644 645
        super(DelVlanForm, self).__init__(*args, **kwargs)
        if instances:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
646
            self.fields["vlan"].queryset = instances
Gabriel Detraz's avatar
Gabriel Detraz committed
647
        else:
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
648
            self.fields["vlan"].queryset = Vlan.objects.all()
Gabriel Detraz's avatar
Gabriel Detraz committed
649

650

651
class EditOuverturePortConfigForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
652 653
    """Edition de la liste des profils d'ouverture de ports
    pour l'interface"""
erdnaxe's avatar
erdnaxe committed
654

655
    class Meta:
656
        model = Interface
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
657
        fields = ["port_lists"]
658

659
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
660
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
661
        super(EditOuverturePortConfigForm, self).__init__(
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
662
            *args, prefix=prefix, **kwargs
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
663 664
        )

665

666
class EditOuverturePortListForm(FormRevMixin, ModelForm):
Gabriel Detraz's avatar
Doc  
Gabriel Detraz committed
667 668
    """Edition de la liste des ports et profils d'ouverture
    des ports"""
erdnaxe's avatar
erdnaxe committed
669

670
    class Meta:
671
        model = OuverturePortList
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
672
        fields = "__all__"
673

674
    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
675 676
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
        super(EditOuverturePortListForm, self).__init__(*args, prefix=prefix, **kwargs)
677

erdnaxe's avatar
erdnaxe committed
678

679 680
class SshFpForm(FormRevMixin, ModelForm):
    """Edits a SSHFP record."""
erdnaxe's avatar
erdnaxe committed
681

682
    class Meta:
683
        model = SshFp
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
684
        exclude = ("machine",)
685 686

    def __init__(self, *args, **kwargs):
Hugo Levy-Falk's avatar
Hugo Levy-Falk committed
687 688
        prefix = kwargs.pop("prefix", self.Meta.model.__name__)
        super(SshFpForm, self).__init__(*args, prefix=prefix, **kwargs)