forms.py 8.71 KB
Newer Older
lhark's avatar
lhark committed
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)
"""
lhark's avatar
lhark committed
32

33 34
from __future__ import unicode_literals

35 36 37
from django import forms
from django.forms import ModelForm
from django.db.models import Prefetch
Laouen Fernet's avatar
Laouen Fernet committed
38
from django.utils.translation import ugettext_lazy as _
39

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

Maël Kervella's avatar
Maël Kervella committed
47
from .models import (
Gabriel Detraz's avatar
Gabriel Detraz committed
48 49 50 51 52 53
    Port,
    Switch,
    Room,
    Stack,
    ModelSwitch,
    ConstructorSwitch,
54 55 56
    AccessPoint,
    SwitchBay,
    Building,
Laouen Fernet's avatar
Laouen Fernet committed
57
    PortProfile,
Gabriel Detraz's avatar
Gabriel Detraz committed
58
)
59

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

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

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

72

73
class EditPortForm(FormRevMixin, ModelForm):
74 75 76 77 78 79 80 81
    """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
82
    class Meta(PortForm.Meta):
chirac's avatar
chirac committed
83 84
        fields = ['room', 'related', 'machine_interface', 'custom_profile',
                  'state', 'details']
Dalahro's avatar
Dalahro committed
85

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

Gabriel Detraz's avatar
Gabriel Detraz committed
94

95
class AddPortForm(FormRevMixin, ModelForm):
96 97
    """Permet d'ajouter un port de switch. Voir EditPortForm pour plus
    d'informations"""
Dalahro's avatar
Dalahro committed
98
    class Meta(PortForm.Meta):
Maël Kervella's avatar
Maël Kervella committed
99 100 101 102 103
        fields = [
            'port',
            'room',
            'machine_interface',
            'related',
chirac's avatar
chirac committed
104
            'custom_profile',
105
            'state',
Maël Kervella's avatar
Maël Kervella committed
106 107
            'details'
        ]
Dalahro's avatar
Dalahro committed
108

109
    def __init__(self, *args, **kwargs):
110
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
111
        super(AddPortForm, self).__init__(*args, prefix=prefix, **kwargs)
Maël Kervella's avatar
Maël Kervella committed
112 113 114 115 116 117 118 119 120
        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'))
121
            ))
Maël Kervella's avatar
Maël Kervella committed
122
        )
123

124

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

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

136

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


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


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

158

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

165

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

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


177
class CreatePortsForm(forms.Form):
178 179 180 181 182
    """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)


183
class EditModelSwitchForm(FormRevMixin, ModelForm):
184
    """Permet d'éediter un modèle de switch : nom et constructeur"""
Maël Kervella's avatar
Maël Kervella committed
185 186 187 188 189
    members = forms.ModelMultipleChoiceField(
        Switch.objects.all(),
        required=False
    )

190 191 192 193 194 195
    class Meta:
        model = ModelSwitch
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Maël Kervella's avatar
Maël Kervella committed
196 197 198 199 200
        super(EditModelSwitchForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
201 202 203 204 205 206 207 208
        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
209 210


211
class EditConstructorSwitchForm(FormRevMixin, ModelForm):
212 213 214 215 216 217 218
    """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__)
Maël Kervella's avatar
Maël Kervella committed
219 220 221 222 223
        super(EditConstructorSwitchForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
224 225 226 227


class EditSwitchBayForm(FormRevMixin, ModelForm):
    """Permet d'éditer une baie de brassage"""
Maël Kervella's avatar
Maël Kervella committed
228 229 230 231 232
    members = forms.ModelMultipleChoiceField(
        Switch.objects.all(),
        required=False
    )

233 234 235 236 237 238 239
    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)
240 241 242 243 244 245 246 247
        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
248 249 250 251 252 253 254 255 256 257 258


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)
Laouen Fernet's avatar
Laouen Fernet committed
259 260 261 262 263 264 265 266 267 268 269 270 271

class EditPortProfileForm(FormRevMixin, ModelForm):
    """Form to edit a port profile"""
    class Meta:
        model = PortProfile
        fields = '__all__'

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