forms.py 8.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 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
#
# 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.
22 23 24 25 26 27 28 29 30 31
"""
Un forms le plus simple possible pour les objets topologie de re2o.

Permet de créer et supprimer : un Port de switch, relié à un switch.

Permet de créer des stacks et d'y ajouter des switchs (StackForm)

Permet de créer, supprimer et editer un switch (EditSwitchForm,
NewSwitchForm)
"""
32

33 34
from __future__ import unicode_literals

35 36 37 38
from django import forms
from django.forms import ModelForm
from django.db.models import Prefetch

39
from machines.models import Interface
Gabriel Detraz's avatar
Gabriel Detraz committed
40 41 42 43
from machines.forms import (
    EditMachineForm,
    NewMachineForm
)
44 45
from re2o.mixins import FormRevMixin

46
from .models import (
Gabriel Detraz's avatar
Gabriel Detraz committed
47 48 49 50 51 52
    Port,
    Switch,
    Room,
    Stack,
    ModelSwitch,
    ConstructorSwitch,
53 54 55
    AccessPoint,
    SwitchBay,
    Building,
Gabriel Detraz's avatar
Gabriel Detraz committed
56
)
57

58

59
class PortForm(FormRevMixin, ModelForm):
60 61
    """Formulaire pour la création d'un port d'un switch
    Relié directement au modèle port"""
Dalahro's avatar
Dalahro committed
62 63 64 65
    class Meta:
        model = Port
        fields = '__all__'

66
    def __init__(self, *args, **kwargs):
67
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
68 69
        super(PortForm, self).__init__(*args, prefix=prefix, **kwargs)

70

71
class EditPortForm(FormRevMixin, ModelForm):
72 73 74 75 76 77 78 79
    """Form pour l'édition d'un port de switche : changement des reglages
    radius ou vlan, ou attribution d'une chambre, autre port ou machine

    Un port est relié à une chambre, un autre port (uplink) ou une machine
    (serveur ou borne), mutuellement exclusif
    Optimisation sur les queryset pour machines et port_related pour
    optimiser le temps de chargement avec select_related (vraiment
    lent sans)"""
Dalahro's avatar
Dalahro committed
80
    class Meta(PortForm.Meta):
81 82
        fields = ['room', 'related', 'machine_interface', 'radius',
                  'vlan_force', 'details']
Dalahro's avatar
Dalahro committed
83

84
    def __init__(self, *args, **kwargs):
85
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
86
        super(EditPortForm, self).__init__(*args, prefix=prefix, **kwargs)
87 88 89
        self.fields['machine_interface'].queryset = (
            Interface.objects.all().select_related('domain__extension')
        )
Gabriel Detraz's avatar
Gabriel Detraz committed
90
        self.fields['related'].queryset = Port.objects.all().prefetch_related('switch__machine_ptr__interface_set__domain__extension')
91

92

93
class AddPortForm(FormRevMixin, ModelForm):
94 95
    """Permet d'ajouter un port de switch. Voir EditPortForm pour plus
    d'informations"""
Dalahro's avatar
Dalahro committed
96
    class Meta(PortForm.Meta):
97 98 99 100 101 102 103 104 105
        fields = [
            'port',
            'room',
            'machine_interface',
            'related',
            'radius',
            'vlan_force',
            'details'
        ]
Dalahro's avatar
Dalahro committed
106

107
    def __init__(self, *args, **kwargs):
108
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
109
        super(AddPortForm, self).__init__(*args, prefix=prefix, **kwargs)
110 111 112 113 114 115 116 117 118
        self.fields['machine_interface'].queryset = (
            Interface.objects.all().select_related('domain__extension')
        )
        self.fields['related'].queryset = (
            Port.objects.all().prefetch_related(Prefetch(
                'switch__interface_set',
                queryset=(Interface.objects
                          .select_related('ipv4__ip_type__extension')
                          .select_related('domain__extension'))
119
            ))
120
        )
121

122

123
class StackForm(FormRevMixin, ModelForm):
124 125
    """Permet d'edition d'une stack : stack_id, et switches membres
    de la stack"""
126 127 128 129
    class Meta:
        model = Stack
        fields = '__all__'

130
    def __init__(self, *args, **kwargs):
131
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
132 133
        super(StackForm, self).__init__(*args, prefix=prefix, **kwargs)

134

Gabriel Detraz's avatar
Gabriel Detraz committed
135
class AddAccessPointForm(NewMachineForm):
Gabriel Detraz's avatar
Gabriel Detraz committed
136 137 138
    """Formulaire pour la création d'une borne
    Relié directement au modèle borne"""
    class Meta:
Gabriel Detraz's avatar
Gabriel Detraz committed
139
        model = AccessPoint
Gabriel Detraz's avatar
Gabriel Detraz committed
140
        fields = ['location', 'name']
Gabriel Detraz's avatar
Gabriel Detraz committed
141 142


Gabriel Detraz's avatar
Gabriel Detraz committed
143 144
class EditAccessPointForm(EditMachineForm):
    """Edition d'une borne. Edition complète"""
Gabriel Detraz's avatar
Gabriel Detraz committed
145
    class Meta:
Gabriel Detraz's avatar
Gabriel Detraz committed
146
        model = AccessPoint
Gabriel Detraz's avatar
Gabriel Detraz committed
147
        fields = '__all__'
Gabriel Detraz's avatar
Gabriel Detraz committed
148 149


150
class EditSwitchForm(EditMachineForm):
151
    """Permet d'éditer un switch : nom et nombre de ports"""
Dalahro's avatar
Dalahro committed
152 153
    class Meta:
        model = Switch
154
        fields = '__all__'
Dalahro's avatar
Dalahro committed
155

156

157
class NewSwitchForm(NewMachineForm):
158 159
    """Permet de créer un switch : emplacement, paramètres machine,
    membre d'un stack (option), nombre de ports (number)"""
chirac's avatar
chirac committed
160
    class Meta(EditSwitchForm.Meta):
161
        fields = ['name', 'switchbay', 'number', 'stack', 'stack_member_id']
Dalahro's avatar
Dalahro committed
162

163

164
class EditRoomForm(FormRevMixin, ModelForm):
165
    """Permet d'éediter le nom et commentaire d'une prise murale"""
chirac's avatar
chirac committed
166 167 168
    class Meta:
        model = Room
        fields = '__all__'
169

170
    def __init__(self, *args, **kwargs):
171
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
172
        super(EditRoomForm, self).__init__(*args, prefix=prefix, **kwargs)
173 174


175
class CreatePortsForm(forms.Form):
176 177 178 179 180
    """Permet de créer une liste de ports pour un switch."""
    begin = forms.IntegerField(label="Début :", min_value=0)
    end = forms.IntegerField(label="Fin :", min_value=0)


181
class EditModelSwitchForm(FormRevMixin, ModelForm):
182
    """Permet d'éediter un modèle de switch : nom et constructeur"""
183 184 185 186 187
    members = forms.ModelMultipleChoiceField(
        Switch.objects.all(),
        required=False
    )

188 189 190 191 192 193
    class Meta:
        model = ModelSwitch
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
194 195 196 197 198
        super(EditModelSwitchForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
199 200 201 202 203 204 205 206
        instance = kwargs.get('instance', None)
        if instance:
            self.initial['members'] = Switch.objects.filter(model=instance)

    def save(self, commit=True):
        instance = super().save(commit)
        instance.switch_set = self.cleaned_data['members']
        return instance
207 208


209
class EditConstructorSwitchForm(FormRevMixin, ModelForm):
210 211 212 213 214 215 216
    """Permet d'éediter le nom d'un constructeur"""
    class Meta:
        model = ConstructorSwitch
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
217 218 219 220 221
        super(EditConstructorSwitchForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
222 223 224 225


class EditSwitchBayForm(FormRevMixin, ModelForm):
    """Permet d'éditer une baie de brassage"""
226 227 228 229 230
    members = forms.ModelMultipleChoiceField(
        Switch.objects.all(),
        required=False
    )

231 232 233 234 235 236 237
    class Meta:
        model = SwitchBay
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
        super(EditSwitchBayForm, self).__init__(*args, prefix=prefix, **kwargs)
238 239 240 241 242 243 244 245
        instance = kwargs.get('instance', None)
        if instance:
            self.initial['members'] = Switch.objects.filter(switchbay=instance)

    def save(self, commit=True):
        instance = super().save(commit)
        instance.switch_set = self.cleaned_data['members']
        return instance
246 247 248 249 250 251 252 253 254 255 256


class EditBuildingForm(FormRevMixin, ModelForm):
    """Permet d'éditer le batiment"""
    class Meta:
        model = Building
        fields = '__all__'

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