Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

forms.py 8.08 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 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
"""
Formulaire d'edition des réglages : user, machine, topologie, asso...
"""
25

26 27
from __future__ import unicode_literals

28
from django.forms import ModelForm, Form
29
from django import forms
Gabriel Detraz's avatar
Gabriel Detraz committed
30 31 32 33 34 35 36
from .models import (
    OptionalUser,
    OptionalMachine,
    OptionalTopologie,
    GeneralOption,
    AssoOption,
    MailMessageOption,
Gabriel Detraz's avatar
Gabriel Detraz committed
37
    HomeOption,
Gabriel Detraz's avatar
Gabriel Detraz committed
38 39
    Service
)
40

41
class EditOptionalUserForm(ModelForm):
42
    """Formulaire d'édition des options de l'user. (solde, telephone..)"""
43 44 45 46 47
    class Meta:
        model = OptionalUser
        fields = '__all__'

    def __init__(self, *args, **kwargs):
48
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
49 50 51 52 53
        super(EditOptionalUserForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
Maël Kervella's avatar
Maël Kervella committed
54 55 56 57 58 59
        self.fields['is_tel_mandatory'].label = (
            'Exiger un numéro de téléphone'
        )
        self.fields['user_solde'].label = (
            'Activation du solde pour les utilisateurs'
        )
60
        self.fields['max_solde'].label = 'Solde maximum'
Maël Kervella's avatar
Maël Kervella committed
61 62 63
        self.fields['min_online_payment'].label = (
            'Montant de rechargement minimum en ligne'
        )
64
        self.fields['self_adhesion'].label = 'Auto inscription'
65

66

67
class EditOptionalMachineForm(ModelForm):
68
    """Options machines (max de machines, etc)"""
69 70 71 72 73
    class Meta:
        model = OptionalMachine
        fields = '__all__'

    def __init__(self, *args, **kwargs):
74
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
75 76 77 78 79 80 81 82 83 84 85 86
        super(EditOptionalMachineForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
        self.fields['password_machine'].label = "Possibilité d'attribuer\
        un mot de passe par interface"
        self.fields['max_lambdauser_interfaces'].label = "Maximum\
        d'interfaces autorisées pour un user normal"
        self.fields['max_lambdauser_aliases'].label = "Maximum d'alias\
        dns autorisés pour un user normal"

87

88
class EditOptionalTopologieForm(ModelForm):
89
    """Options de topologie, formulaire d'edition (vlan par default etc)"""
90 91 92 93 94
    class Meta:
        model = OptionalTopologie
        fields = '__all__'

    def __init__(self, *args, **kwargs):
95
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
96 97 98 99 100 101 102 103 104 105
        super(EditOptionalTopologieForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
        self.fields['vlan_decision_ok'].label = "Vlan où placer les\
        machines après acceptation RADIUS"
        self.fields['vlan_decision_nok'].label = "Vlan où placer les\
        machines après rejet RADIUS"

106

107
class EditGeneralOptionForm(ModelForm):
108
    """Options générales (affichages de résultats de recherche, etc)"""
109 110 111 112 113
    class Meta:
        model = GeneralOption
        fields = '__all__'

    def __init__(self, *args, **kwargs):
114
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
115 116 117 118 119 120 121 122 123 124 125 126 127
        super(EditGeneralOptionForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
        self.fields['search_display_page'].label = 'Resultats\
        affichés dans une recherche'
        self.fields['pagination_number'].label = 'Items par page,\
        taille normale (ex users)'
        self.fields['pagination_large_number'].label = 'Items par page,\
        taille élevée (machines)'
        self.fields['req_expire_hrs'].label = 'Temps avant expiration du lien\
        de reinitialisation de mot de passe (en heures)'
128
        self.fields['site_name'].label = 'Nom du site web'
129 130
        self.fields['email_from'].label = "Adresse mail d\
        'expedition automatique"
131
        self.fields['GTU_sum_up'].label = "Résumé des CGU"
132

133 134

class EditAssoOptionForm(ModelForm):
135
    """Options de l'asso (addresse, telephone, etc)"""
136 137 138 139
    class Meta:
        model = AssoOption
        fields = '__all__'

140
    def __init__(self, *args, **kwargs):
141
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
142 143 144 145 146
        super(EditAssoOptionForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
147 148 149 150 151 152 153
        self.fields['name'].label = 'Nom de l\'asso'
        self.fields['siret'].label = 'SIRET'
        self.fields['adresse1'].label = 'Adresse (ligne 1)'
        self.fields['adresse2'].label = 'Adresse (ligne 2)'
        self.fields['contact'].label = 'Email de contact'
        self.fields['telephone'].label = 'Numéro de téléphone'
        self.fields['pseudo'].label = 'Pseudo d\'usage'
154 155 156
        self.fields['utilisateur_asso'].label = 'Compte utilisé pour\
        faire les modifications depuis /admin'

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    def clean(self):
        cleaned_data = super().clean()
        payment = cleaned_data.get('payment')

        if payment == 'NONE':
            return cleaned_data

        if not cleaned_data.get('payment_id', ''):
            msg = forms.ValidationError("Vous devez spécifier un identifiant \
                                        de paiement.")
            self.add_error('payment_id', msg)
        if not cleaned_data.get('payment_pass', ''):
            msg = forms.ValidationError("Vous devez spécifier un mot de passe \
                                        de paiement.")
            self.add_error('payment_pass', msg)

        return cleaned_data


176
class EditMailMessageOptionForm(ModelForm):
177
    """Formulaire d'edition des messages de bienvenue personnalisés"""
178 179 180 181
    class Meta:
        model = MailMessageOption
        fields = '__all__'

182
    def __init__(self, *args, **kwargs):
183
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
184 185 186 187 188 189 190 191 192 193
        super(EditMailMessageOptionForm, self).__init__(
            *args,
            prefix=prefix,
            **kwargs
        )
        self.fields['welcome_mail_fr'].label = 'Message dans le\
        mail de bienvenue en français'
        self.fields['welcome_mail_en'].label = 'Message dans le\
        mail de bienvenue en anglais'

194

Gabriel Detraz's avatar
Gabriel Detraz committed
195 196
class EditHomeOptionForm(ModelForm):
    """Edition forms of Home options"""
Gabriel Detraz's avatar
Gabriel Detraz committed
197
    class Meta:
Gabriel Detraz's avatar
Gabriel Detraz committed
198
        model = HomeOption
Gabriel Detraz's avatar
Gabriel Detraz committed
199 200 201 202
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
Gabriel Detraz's avatar
Gabriel Detraz committed
203
        super(EditHomeOptionForm, self).__init__(
Gabriel Detraz's avatar
Gabriel Detraz committed
204 205 206 207 208 209
            *args,
            prefix=prefix,
            **kwargs
        )


210
class ServiceForm(ModelForm):
211
    """Edition, ajout de services sur la page d'accueil"""
212 213 214
    class Meta:
        model = Service
        fields = '__all__'
215

216
    def __init__(self, *args, **kwargs):
217
        prefix = kwargs.pop('prefix', self.Meta.model.__name__)
218 219 220
        super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)


221 222 223
class DelServiceForm(Form):
    """Suppression de services sur la page d'accueil"""
    services = forms.ModelMultipleChoiceField(
224
        queryset=Service.objects.none(),
225 226 227
        label="Enregistrements service actuels",
        widget=forms.CheckboxSelectMultiple
    )
228 229 230 231 232 233 234 235

    def __init__(self, *args, **kwargs):
        instances = kwargs.pop('instances', None)
        super(DelServiceForm, self).__init__(*args, **kwargs)
        if instances:
            self.fields['services'].queryset = instances
        else:
            self.fields['services'].queryset = Service.objects.all()