forms.py 5.54 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
from machines.models import Interface
36 37
from django import forms
from django.forms import ModelForm, Form
38 39
from .models import Port, Switch, Room, Stack

Dalahro's avatar
Dalahro committed
40 41

class PortForm(ModelForm):
42 43
    """Formulaire pour la création d'un port d'un switch
    Relié directement au modèle port"""
Dalahro's avatar
Dalahro committed
44 45 46 47
    class Meta:
        model = Port
        fields = '__all__'

48
    def __init__(self, *args, **kwargs):
49
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
50 51
        super(PortForm, self).__init__(*args, prefix=prefix, **kwargs)

52

Dalahro's avatar
Dalahro committed
53
class EditPortForm(ModelForm):
54 55 56 57 58 59 60 61
    """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
62
    class Meta(PortForm.Meta):
63 64
        fields = ['room', 'related', 'machine_interface', 'radius',
                  'vlan_force', 'details']
Dalahro's avatar
Dalahro committed
65

66
    def __init__(self, *args, **kwargs):
67
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
68
        super(EditPortForm, self).__init__(*args, prefix=prefix, **kwargs)
69 70 71 72 73 74
        self.fields['machine_interface'].queryset = Interface.objects.all()\
            .select_related('domain__extension')
        self.fields['related'].queryset = Port.objects.all()\
            .select_related('switch__switch_interface__domain__extension')\
            .order_by('switch', 'port')

Gabriel Detraz's avatar
Gabriel Detraz committed
75

Dalahro's avatar
Dalahro committed
76
class AddPortForm(ModelForm):
77 78
    """Permet d'ajouter un port de switch. Voir EditPortForm pour plus
    d'informations"""
Dalahro's avatar
Dalahro committed
79
    class Meta(PortForm.Meta):
80 81
        fields = ['port', 'room', 'machine_interface', 'related',
                  'radius', 'vlan_force', 'details']
Dalahro's avatar
Dalahro committed
82

83
    def __init__(self, *args, **kwargs):
84
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
85
        super(AddPortForm, self).__init__(*args, prefix=prefix, **kwargs)
chirac's avatar
chirac committed
86 87 88 89 90
        self.fields['machine_interface'].queryset = Interface.objects.all()\
            .select_related('domain__extension')
        self.fields['related'].queryset = Port.objects.all()\
            .select_related('switch__switch_interface__domain__extension')\
            .order_by('switch', 'port')
91

92

93
class StackForm(ModelForm):
94 95
    """Permet d'edition d'une stack : stack_id, et switches membres
    de la stack"""
96 97 98 99
    class Meta:
        model = Stack
        fields = '__all__'

100
    def __init__(self, *args, **kwargs):
101
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
102 103
        super(StackForm, self).__init__(*args, prefix=prefix, **kwargs)

104

chirac's avatar
chirac committed
105
class EditSwitchForm(ModelForm):
106
    """Permet d'éditer un switch : nom et nombre de ports"""
Dalahro's avatar
Dalahro committed
107 108 109 110
    class Meta:
        model = Switch
        fields = '__all__'

chirac's avatar
chirac committed
111
    def __init__(self, *args, **kwargs):
112
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
113
        super(EditSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
114 115
        self.fields['switch_interface'].queryset = Interface.objects.all()\
            .select_related('domain__extension')
chirac's avatar
chirac committed
116 117 118
        self.fields['location'].label = 'Localisation'
        self.fields['number'].label = 'Nombre de ports'

119

chirac's avatar
chirac committed
120
class NewSwitchForm(ModelForm):
121 122
    """Permet de créer un switch : emplacement, paramètres machine,
    membre d'un stack (option), nombre de ports (number)"""
chirac's avatar
chirac committed
123
    class Meta(EditSwitchForm.Meta):
124
        fields = ['location', 'number', 'details', 'stack', 'stack_member_id']
Dalahro's avatar
Dalahro committed
125

126
    def __init__(self, *args, **kwargs):
127
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
128 129
        super(NewSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)

130

chirac's avatar
chirac committed
131
class EditRoomForm(ModelForm):
132
    """Permet d'éediter le nom et commentaire d'une prise murale"""
chirac's avatar
chirac committed
133 134 135
    class Meta:
        model = Room
        fields = '__all__'
136

137
    def __init__(self, *args, **kwargs):
138
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
139
        super(EditRoomForm, self).__init__(*args, prefix=prefix, **kwargs)
140 141 142 143 144 145


class CreatePortsForm(Form):
    """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)