forms.py 9.78 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,
detraz's avatar
detraz committed
57
    Dormitory,
Laouen Fernet's avatar
Laouen Fernet committed
58
    PortProfile,
59 60
    ModuleSwitch,
    ModuleOnSwitch,
Gabriel Detraz's avatar
Gabriel Detraz committed
61
)
62

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

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

71
    def __init__(self, *args, **kwargs):
72
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
73 74
        super(PortForm, self).__init__(*args, prefix=prefix, **kwargs)

75

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

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

Gabriel Detraz's avatar
Gabriel Detraz committed
97

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

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

127

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

135
    def __init__(self, *args, **kwargs):
136
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
137 138
        super(StackForm, self).__init__(*args, prefix=prefix, **kwargs)

139

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


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


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

161

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

168

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

175
    def __init__(self, *args, **kwargs):
176
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
177
        super(EditRoomForm, self).__init__(*args, prefix=prefix, **kwargs)
178 179


180
class CreatePortsForm(forms.Form):
181
    """Permet de créer une liste de ports pour un switch."""
182 183
    begin = forms.IntegerField(label=_("Start:"), min_value=0)
    end = forms.IntegerField(label=_("End:"), min_value=0)
184 185


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

193 194 195 196 197 198
    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
199 200 201 202 203
        super(EditModelSwitchForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
204 205 206 207 208 209 210 211
        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
212 213


214
class EditConstructorSwitchForm(FormRevMixin, ModelForm):
215 216 217 218 219 220 221
    """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
222 223 224 225 226
        super(EditConstructorSwitchForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
227 228 229 230


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

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


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
262

detraz's avatar
detraz committed
263 264

class EditDormitoryForm(FormRevMixin, ModelForm):
klafyvel's avatar
klafyvel committed
265
    """Enable dormitory edition"""
detraz's avatar
detraz committed
266 267 268 269 270 271 272 273 274
    class Meta:
        model = Dormitory
        fields = '__all__'

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


Laouen Fernet's avatar
Laouen Fernet committed
275 276 277 278 279 280 281 282 283 284 285 286
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)

287 288 289 290 291 292 293 294 295 296 297
class EditModuleForm(FormRevMixin, ModelForm):
    """Add and edit module instance"""
    class Meta:
        model = ModuleSwitch
        fields = '__all__'

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


298 299 300 301 302 303 304 305 306
class EditSwitchModuleForm(FormRevMixin, ModelForm):
    """Add/edit a switch to a module"""
    class Meta:
        model = ModuleOnSwitch
        fields = '__all__'

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