8
0
Fork 0
mirror of https://gitlab.federez.net/re2o/re2o synced 2024-07-04 05:04:06 +00:00
re2o/machines/forms.py

483 lines
16 KiB
Python
Raw Normal View History

# -*- mode: python; coding: utf-8 -*-
2017-01-15 23:01:18 +00:00
# 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
# Copyright © 2017 Maël Kervella
2017-01-15 23:01:18 +00:00
#
# 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.
2017-10-16 00:41:21 +00:00
"""
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
"""
2017-01-15 23:01:18 +00:00
from __future__ import unicode_literals
2017-10-16 00:41:21 +00:00
from django.forms import ModelForm, Form
2016-07-07 11:19:03 +00:00
from django import forms
2016-07-06 20:49:16 +00:00
2017-10-16 00:41:21 +00:00
from .models import (
Domain,
Machine,
Interface,
IpList,
MachineType,
Extension,
SOA,
2017-10-16 00:41:21 +00:00
Mx,
Text,
Ns,
Service,
Vlan,
Nas,
IpType,
OuverturePortList,
)
2016-07-06 20:49:16 +00:00
class EditMachineForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Formulaire d'édition d'une machine"""
2016-07-06 20:49:16 +00:00
class Meta:
model = Machine
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditMachineForm, self).__init__(*args, prefix=prefix, **kwargs)
2016-07-06 20:49:16 +00:00
self.fields['name'].label = 'Nom de la machine'
2017-10-16 00:41:21 +00:00
2016-07-06 20:49:16 +00:00
class NewMachineForm(EditMachineForm):
2017-10-16 00:41:21 +00:00
"""Creation d'une machine, ne renseigne que le nom"""
2016-07-06 20:49:16 +00:00
class Meta(EditMachineForm.Meta):
2016-07-18 17:14:48 +00:00
fields = ['name']
2016-07-06 20:49:16 +00:00
2017-10-16 00:41:21 +00:00
class BaseEditMachineForm(EditMachineForm):
2017-10-16 00:41:21 +00:00
"""Edition basique, ne permet que de changer le nom et le statut.
Réservé aux users sans droits spécifiques"""
class Meta(EditMachineForm.Meta):
2017-10-16 00:41:21 +00:00
fields = ['name', 'active']
2016-07-06 20:49:16 +00:00
class EditInterfaceForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Edition d'une interface. Edition complète"""
2016-07-06 20:49:16 +00:00
class Meta:
model = Interface
fields = ['machine', 'type', 'ipv4', 'mac_address', 'details']
2016-07-06 20:49:16 +00:00
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditInterfaceForm, self).__init__(*args, prefix=prefix, **kwargs)
2016-07-06 20:49:16 +00:00
self.fields['mac_address'].label = 'Adresse mac'
2016-07-18 17:14:48 +00:00
self.fields['type'].label = 'Type de machine'
self.fields['type'].empty_label = "Séléctionner un type de machine"
if "ipv4" in self.fields:
2017-10-16 00:41:21 +00:00
self.fields['ipv4'].empty_label = "Assignation automatique\
de l'ipv4"
self.fields['ipv4'].queryset = IpList.objects.filter(
interface__isnull=True
)
# Add it's own address
2017-10-16 00:41:21 +00:00
self.fields['ipv4'].queryset |= IpList.objects.filter(
interface=self.instance
)
2017-05-28 17:22:45 +00:00
if "machine" in self.fields:
2017-10-16 00:41:21 +00:00
self.fields['machine'].queryset = Machine.objects.all()\
.select_related('user')
2016-07-06 20:49:16 +00:00
class AddInterfaceForm(EditInterfaceForm):
2017-10-16 00:41:21 +00:00
"""Ajout d'une interface à une machine. En fonction des droits,
affiche ou non l'ensemble des ip disponibles"""
2016-07-06 20:49:16 +00:00
class Meta(EditInterfaceForm.Meta):
2017-10-16 00:41:21 +00:00
fields = ['type', 'ipv4', 'mac_address', 'details']
2016-07-06 20:49:16 +00:00
def __init__(self, *args, **kwargs):
infra = kwargs.pop('infra')
super(AddInterfaceForm, self).__init__(*args, **kwargs)
self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
if not infra:
2017-10-16 00:41:21 +00:00
self.fields['type'].queryset = MachineType.objects.filter(
ip_type__in=IpType.objects.filter(need_infra=False)
)
self.fields['ipv4'].queryset = IpList.objects.filter(
interface__isnull=True
).filter(ip_type__in=IpType.objects.filter(need_infra=False))
2016-11-30 03:48:47 +00:00
else:
2017-10-16 00:41:21 +00:00
self.fields['ipv4'].queryset = IpList.objects.filter(
interface__isnull=True
)
2016-07-06 20:49:16 +00:00
class NewInterfaceForm(EditInterfaceForm):
2017-10-16 00:41:21 +00:00
"""Formulaire light, sans choix de l'ipv4; d'ajout d'une interface"""
2016-07-06 20:49:16 +00:00
class Meta(EditInterfaceForm.Meta):
2017-10-16 00:41:21 +00:00
fields = ['type', 'mac_address', 'details']
2016-07-06 20:49:16 +00:00
class BaseEditInterfaceForm(EditInterfaceForm):
2017-10-16 00:41:21 +00:00
"""Edition basique d'une interface. En fonction des droits,
ajoute ou non l'ensemble des ipv4 disponibles (infra)"""
class Meta(EditInterfaceForm.Meta):
2017-10-16 00:41:21 +00:00
fields = ['type', 'ipv4', 'mac_address', 'details']
2016-10-24 17:52:29 +00:00
def __init__(self, *args, **kwargs):
infra = kwargs.pop('infra')
super(BaseEditInterfaceForm, self).__init__(*args, **kwargs)
self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
if not infra:
2017-10-16 00:41:21 +00:00
self.fields['type'].queryset = MachineType.objects.filter(
ip_type__in=IpType.objects.filter(need_infra=False)
)
self.fields['ipv4'].queryset = IpList.objects.filter(
interface__isnull=True
).filter(ip_type__in=IpType.objects.filter(need_infra=False))
# Add it's own address
2017-10-16 00:41:21 +00:00
self.fields['ipv4'].queryset |= IpList.objects.filter(
interface=self.instance
)
2016-11-30 03:48:47 +00:00
else:
2017-10-16 00:41:21 +00:00
self.fields['ipv4'].queryset = IpList.objects.filter(
interface__isnull=True
)
self.fields['ipv4'].queryset |= IpList.objects.filter(
interface=self.instance
)
class AliasForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Ajout d'un alias (et edition), CNAME, contenant nom et extension"""
class Meta:
2016-12-24 19:04:53 +00:00
model = Domain
2017-10-16 00:41:21 +00:00
fields = ['name', 'extension']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
2017-10-21 21:25:22 +00:00
infra = kwargs.pop('infra')
super(AliasForm, self).__init__(*args, prefix=prefix, **kwargs)
2017-10-21 21:25:22 +00:00
if not infra:
self.fields['extension'].queryset = Extension.objects.filter(
need_infra=False
)
2017-10-16 00:41:21 +00:00
class DomainForm(AliasForm):
2017-10-16 00:41:21 +00:00
"""Ajout et edition d'un enregistrement de nom, relié à interface"""
class Meta(AliasForm.Meta):
fields = ['name']
def __init__(self, *args, **kwargs):
if 'user' in kwargs:
user = kwargs.pop('user')
2017-01-09 15:20:13 +00:00
initial = kwargs.get('initial', {})
initial['name'] = user.get_next_domain_name()
2017-10-16 00:41:21 +00:00
kwargs['initial'] = initial
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(DomainForm, self).__init__(*args, prefix=prefix, **kwargs)
2017-10-16 00:41:21 +00:00
class DelAliasForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppression d'un ou plusieurs objets alias"""
alias = forms.ModelMultipleChoiceField(
queryset=Domain.objects.all(),
label="Alias actuels",
widget=forms.CheckboxSelectMultiple
)
def __init__(self, *args, **kwargs):
interface = kwargs.pop('interface')
super(DelAliasForm, self).__init__(*args, **kwargs)
2017-10-16 00:41:21 +00:00
self.fields['alias'].queryset = Domain.objects.filter(
cname__in=Domain.objects.filter(interface_parent=interface)
)
2016-07-07 11:19:03 +00:00
class MachineTypeForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Ajout et edition d'un machinetype, relié à un iptype"""
2016-07-07 11:19:03 +00:00
class Meta:
model = MachineType
2017-10-16 00:41:21 +00:00
fields = ['type', 'ip_type']
2016-07-07 11:19:03 +00:00
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(MachineTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
2016-07-07 11:19:03 +00:00
self.fields['type'].label = 'Type de machine à ajouter'
2016-10-22 22:55:58 +00:00
self.fields['ip_type'].label = "Type d'ip relié"
2016-07-07 11:19:03 +00:00
2017-10-16 00:41:21 +00:00
class DelMachineTypeForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppression d'un ou plusieurs machinetype"""
machinetypes = forms.ModelMultipleChoiceField(
queryset=MachineType.objects.all(),
label="Types de machines actuelles",
widget=forms.CheckboxSelectMultiple
)
2016-07-07 11:19:03 +00:00
2016-10-22 22:55:58 +00:00
class IpTypeForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Formulaire d'ajout d'un iptype. Pas d'edition de l'ip de start et de
stop après creation"""
2016-10-22 22:55:58 +00:00
class Meta:
model = IpType
2017-10-16 00:41:21 +00:00
fields = ['type', 'extension', 'need_infra', 'domaine_ip_start',
'domaine_ip_stop', 'prefix_v6', 'vlan', 'ouverture_ports']
2016-10-22 22:55:58 +00:00
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(IpTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
2016-10-22 22:55:58 +00:00
self.fields['type'].label = 'Type ip à ajouter'
2017-10-16 00:41:21 +00:00
class EditIpTypeForm(IpTypeForm):
2017-10-16 00:41:21 +00:00
"""Edition d'un iptype. Pas d'edition du rangev4 possible, car il faudrait
synchroniser les objets iplist"""
class Meta(IpTypeForm.Meta):
2017-10-16 00:41:21 +00:00
fields = ['extension', 'type', 'need_infra', 'prefix_v6', 'vlan',
'ouverture_ports']
2016-10-22 22:55:58 +00:00
class DelIpTypeForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppression d'un ou plusieurs iptype"""
iptypes = forms.ModelMultipleChoiceField(
queryset=IpType.objects.all(),
label="Types d'ip actuelles",
widget=forms.CheckboxSelectMultiple
)
2016-10-22 22:55:58 +00:00
2016-07-08 15:54:06 +00:00
class ExtensionForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Formulaire d'ajout et edition d'une extension"""
2016-07-08 15:54:06 +00:00
class Meta:
model = Extension
fields = '__all__'
2016-07-08 15:54:06 +00:00
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(ExtensionForm, self).__init__(*args, prefix=prefix, **kwargs)
2016-10-26 12:05:40 +00:00
self.fields['name'].label = 'Extension à ajouter'
self.fields['origin'].label = 'Enregistrement A origin'
self.fields['origin_v6'].label = 'Enregistrement AAAA origin'
self.fields['soa'].label = 'En-tête SOA à utiliser'
2016-07-08 15:54:06 +00:00
2017-10-16 00:41:21 +00:00
class DelExtensionForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppression d'une ou plusieurs extensions"""
extensions = forms.ModelMultipleChoiceField(
queryset=Extension.objects.all(),
label="Extensions actuelles",
widget=forms.CheckboxSelectMultiple
)
2016-07-08 15:54:06 +00:00
class SOAForm(ModelForm):
"""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)
class DelSOAForm(Form):
"""Suppression d'un ou plusieurs SOA"""
soa = forms.ModelMultipleChoiceField(
queryset=SOA.objects.all(),
label="SOA actuels",
widget=forms.CheckboxSelectMultiple
)
class MxForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Ajout et edition d'un MX"""
class Meta:
model = Mx
fields = ['zone', 'priority', 'name']
2017-10-16 00:41:21 +00:00
2017-01-05 22:48:45 +00:00
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(MxForm, self).__init__(*args, prefix=prefix, **kwargs)
2017-10-16 00:41:21 +00:00
self.fields['name'].queryset = Domain.objects.exclude(
interface_parent=None
2017-10-18 15:19:51 +00:00
).select_related('extension')
2017-10-16 00:41:21 +00:00
class DelMxForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppression d'un ou plusieurs MX"""
mx = forms.ModelMultipleChoiceField(
queryset=Mx.objects.all(),
label="MX actuels",
widget=forms.CheckboxSelectMultiple
)
class NsForm(ModelForm):
2017-10-18 15:19:51 +00:00
"""Ajout d'un NS pour une zone
On exclue les CNAME dans les objets domain (interdit par la rfc)
donc on prend uniquemet """
class Meta:
model = Ns
2016-12-26 16:43:41 +00:00
fields = ['zone', 'ns']
2016-12-26 18:45:43 +00:00
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(NsForm, self).__init__(*args, prefix=prefix, **kwargs)
2017-10-16 00:41:21 +00:00
self.fields['ns'].queryset = Domain.objects.exclude(
interface_parent=None
2017-10-18 15:19:51 +00:00
).select_related('extension')
2017-10-16 00:41:21 +00:00
2016-12-26 18:45:43 +00:00
class DelNsForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppresion d'un ou plusieurs NS"""
ns = forms.ModelMultipleChoiceField(
queryset=Ns.objects.all(),
label="Enregistrements NS actuels",
widget=forms.CheckboxSelectMultiple
)
class TxtForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Ajout d'un txt pour une zone"""
2017-09-05 16:18:41 +00:00
class Meta:
model = Text
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(TxtForm, self).__init__(*args, prefix=prefix, **kwargs)
2017-10-16 00:41:21 +00:00
class DelTxtForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppression d'un ou plusieurs TXT"""
txt = forms.ModelMultipleChoiceField(
queryset=Text.objects.all(),
label="Enregistrements Txt actuels",
widget=forms.CheckboxSelectMultiple
)
2017-09-05 16:18:41 +00:00
class NasForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Ajout d'un type de nas (machine d'authentification,
swicths, bornes...)"""
class Meta:
model = Nas
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(NasForm, self).__init__(*args, prefix=prefix, **kwargs)
2017-10-16 00:41:21 +00:00
class DelNasForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppression d'un ou plusieurs nas"""
nas = forms.ModelMultipleChoiceField(
queryset=Nas.objects.all(),
label="Enregistrements Nas actuels",
widget=forms.CheckboxSelectMultiple
)
class ServiceForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Ajout et edition d'une classe de service : dns, dhcp, etc"""
class Meta:
model = Service
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
2017-10-18 02:11:27 +00:00
self.fields['servers'].queryset = Interface.objects.all()\
.select_related('domain__extension')
def save(self, commit=True):
instance = super(ServiceForm, self).save(commit=False)
if commit:
instance.save()
instance.process_link(self.cleaned_data.get('servers'))
return instance
2017-10-16 00:41:21 +00:00
class DelServiceForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppression d'un ou plusieurs service"""
service = forms.ModelMultipleChoiceField(
queryset=Service.objects.all(),
label="Services actuels",
widget=forms.CheckboxSelectMultiple
)
class VlanForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Ajout d'un vlan : id, nom"""
class Meta:
model = Vlan
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs)
2017-10-16 00:41:21 +00:00
class DelVlanForm(Form):
2017-10-16 00:41:21 +00:00
"""Suppression d'un ou plusieurs vlans"""
vlan = forms.ModelMultipleChoiceField(
queryset=Vlan.objects.all(),
label="Vlan actuels",
widget=forms.CheckboxSelectMultiple
)
class EditOuverturePortConfigForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Edition de la liste des profils d'ouverture de ports
pour l'interface"""
class Meta:
model = Interface
fields = ['port_lists']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
2017-10-16 00:41:21 +00:00
super(EditOuverturePortConfigForm, self).__init__(
*args,
prefix=prefix,
**kwargs
)
class EditOuverturePortListForm(ModelForm):
2017-10-16 00:41:21 +00:00
"""Edition de la liste des ports et profils d'ouverture
des ports"""
class Meta:
model = OuverturePortList
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
2017-10-16 00:41:21 +00:00
super(EditOuverturePortListForm, self).__init__(
*args,
prefix=prefix,
**kwargs
)