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 !

views.py 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 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.

# App de gestion des machines pour re2o
# Gabriel Détraz, Augustin Lemesle
# Gplv2
26 27 28 29
"""
Vue d'affichage, et de modification des réglages (réglages machine,
topologie, users, service...)
"""
30

31
from __future__ import unicode_literals
32

33
from django.urls import reverse
34
from django.shortcuts import redirect
35
from django.contrib import messages
36
from django.contrib.auth.decorators import login_required
37
from django.db.models import ProtectedError
38
from django.db import transaction
39
from django.utils.translation import ugettext as _
40 41 42

from reversion import revisions as reversion

43
from re2o.views import form
44
from re2o.acl import can_create, can_edit, can_delete_set, can_view_all, can_delete
45

46 47 48 49
from .forms import MailContactForm, DelMailContactForm
from .forms import (
    ServiceForm,
    ReminderForm,
50 51
    RadiusKeyForm,
    SwitchManagementCredForm
52
)
Gabriel Detraz's avatar
Gabriel Detraz committed
53 54
from .models import (
    Service,
55
    MailContact,
Gabriel Detraz's avatar
Gabriel Detraz committed
56 57 58 59 60 61
    OptionalUser,
    OptionalMachine,
    AssoOption,
    MailMessageOption,
    GeneralOption,
    OptionalTopologie,
62 63
    HomeOption,
    Reminder,
64 65
    RadiusKey,
    SwitchManagementCred
Gabriel Detraz's avatar
Gabriel Detraz committed
66
)
67 68
from . import models
from . import forms
69 70 71


@login_required
72 73
@can_view_all(OptionalUser, OptionalMachine, OptionalTopologie, GeneralOption,
              AssoOption, MailMessageOption, HomeOption)
74
def display_options(request):
75 76 77 78 79 80 81
    """Vue pour affichage des options (en vrac) classé selon les models
    correspondants dans un tableau"""
    useroptions, _created = OptionalUser.objects.get_or_create()
    machineoptions, _created = OptionalMachine.objects.get_or_create()
    topologieoptions, _created = OptionalTopologie.objects.get_or_create()
    generaloptions, _created = GeneralOption.objects.get_or_create()
    assooptions, _created = AssoOption.objects.get_or_create()
Gabriel Detraz's avatar
Gabriel Detraz committed
82
    homeoptions, _created = HomeOption.objects.get_or_create()
83
    mailmessageoptions, _created = MailMessageOption.objects.get_or_create()
84
    service_list = Service.objects.all()
85
    mailcontact_list = MailContact.objects.all()
86 87
    reminder_list = Reminder.objects.all()
    radiuskey_list = RadiusKey.objects.all()
88
    switchmanagementcred_list = SwitchManagementCred.objects.all()
89 90 91 92 93 94
    return form({
        'useroptions': useroptions,
        'machineoptions': machineoptions,
        'topologieoptions': topologieoptions,
        'generaloptions': generaloptions,
        'assooptions': assooptions,
Gabriel Detraz's avatar
Gabriel Detraz committed
95
        'homeoptions': homeoptions,
96
        'mailmessageoptions': mailmessageoptions,
97
        'service_list': service_list,
98
        'mailcontact_list': mailcontact_list,
99
        'reminder_list': reminder_list,
100
        'radiuskey_list' : radiuskey_list,
101
        'switchmanagementcred_list': switchmanagementcred_list,  
102 103
        }, 'preferences/display_preferences.html', request)

104 105

@login_required
106 107 108 109
def edit_options(request, section):
    """ Edition des préférences générales"""
    model = getattr(models, section, None)
    form_instance = getattr(forms, 'Edit' + section + 'Form', None)
110
    if not (model or form_instance):
111
        messages.error(request, _("Unknown object"))
112 113 114 115 116
        return redirect(reverse('preferences:display-options'))

    options_instance, _created = model.objects.get_or_create()
    can, msg = options_instance.can_edit(request.user)
    if not can:
117 118
        messages.error(request, msg or _("You don't have the right to edit"
                                         " this option."))
119 120 121
        return redirect(reverse('index'))
    options = form_instance(
        request.POST or None,
122
        request.FILES or None,
123 124 125 126 127 128 129
        instance=options_instance
    )
    if options.is_valid():
        with transaction.atomic(), reversion.create_revision():
            options.save()
            reversion.set_user(request.user)
            reversion.set_comment(
130
                "Field(s) edited: %s" % ', '.join(
131
                    field for field in options.changed_data
132 133
                )
            )
134
            messages.success(request, _("The preferences were edited."))
135
        return redirect(reverse('preferences:display-options'))
136 137 138 139 140
    return form(
        {'options': options},
        'preferences/edit_preferences.html',
        request
    )
141

142

143
@login_required
144 145
@can_create(Service)
def add_service(request):
146
    """Ajout d'un service de la page d'accueil"""
Gabriel Detraz's avatar
Gabriel Detraz committed
147
    service = ServiceForm(request.POST or None, request.FILES or None)
148
    if service.is_valid():
149
        service.save()
150
        messages.success(request, _("The service was added."))
151
        return redirect(reverse('preferences:display-options'))
152
    return form(
153
        {'preferenceform': service, 'action_name': _("Add a service")},
154 155 156 157
        'preferences/preferences.html',
        request
        )

158 159

@login_required
160
@can_edit(Service)
161
def edit_service(request, service_instance, **_kwargs):
162
    """Edition des services affichés sur la page d'accueil"""
163
    service = ServiceForm(
164 165 166 167
        request.POST or None,
        request.FILES or None,
        instance=service_instance
    )
168
    if service.is_valid():
169
        service.save()
170
        messages.success(request, _("The service was edited."))
171
        return redirect(reverse('preferences:display-options'))
172
    return form(
173
        {'preferenceform': service, 'action_name': _("Edit")},
174 175 176 177
        'preferences/preferences.html',
        request
    )

178
@login_required
179 180
@can_delete(Service)
def del_service(request, service_instance, **_kwargs):
181
    """Suppression d'un service de la page d'accueil"""
182 183 184
    if request.method == "POST":
        service_instance.delete()
        messages.success(request, "Le service a été détruit")
185
        return redirect(reverse('preferences:display-options'))
186
    return form(
187 188
        {'objet': service_instance, 'objet_name': 'service'},
        'preferences/delete.html',
189
        request
190
        )
191

192 193 194
@login_required
@can_create(Reminder)
def add_reminder(request):
195
    """Ajout d'un mail de rappel"""
196
    reminder = ReminderForm(request.POST or None, request.FILES or None)
197 198 199
    if reminder.is_valid():
        reminder.save()
        messages.success(request, _("The reminder was added."))
200 201
        return redirect(reverse('preferences:display-options'))
    return form(
202
        {'preferenceform': reminder, 'action_name': _("Add a service")},
203 204 205 206 207 208
        'preferences/preferences.html',
        request
        )

@login_required
@can_edit(Reminder)
209 210
def edit_reminder(request, reminder_instance, **_kwargs):
    """Edition reminder"""
211 212 213 214 215 216
    reminder = ReminderForm(
        request.POST or None,
        request.FILES or None,
        instance=reminder_instance
    )
    if reminder.is_valid():
217
        reminder.save()
218 219 220
        messages.success(request, _("The service was edited."))
        return redirect(reverse('preferences:display-options'))
    return form(
221
        {'preferenceform': reminder, 'action_name': _("Edit")},
222 223 224 225 226
        'preferences/preferences.html',
        request
    )


227 228

@login_required
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
@can_delete(Reminder)
def del_reminder(request, reminder_instance, **_kwargs):
    """Destruction d'un reminder"""
    if request.method == "POST":
        reminder_instance.delete()
        messages.success(request, "Le reminder a été détruit")
        return redirect(reverse('preferences:display-options'))
    return form(
        {'objet': reminder_instance, 'objet_name': 'reminder'},
        'preferences/delete.html',
        request
        )


@login_required
@can_create(RadiusKey)
def add_radiuskey(request):
    """Ajout d'une clef radius"""
    radiuskey = RadiusKeyForm(request.POST or None)
    if radiuskey.is_valid():
        radiuskey.save()
        messages.success(request, "Cette clef a été ajouté")
        return redirect(reverse('preferences:display-options'))
    return form(
        {'preferenceform': radiuskey, 'action_name': 'Ajouter'},
        'preferences/preferences.html',
        request
        )

@can_edit(RadiusKey)
def edit_radiuskey(request, radiuskey_instance, **_kwargs):
    """Edition des clefs radius"""
    radiuskey = RadiusKeyForm(request.POST or None, instance=radiuskey_instance)
    if radiuskey.is_valid():
        radiuskey.save()
        messages.success(request, "Radiuskey modifié")
        return redirect(reverse('preferences:display-options'))
    return form(
        {'preferenceform': radiuskey, 'action_name': 'Editer'},
        'preferences/preferences.html',
        request
    )


@login_required
@can_delete(RadiusKey)
def del_radiuskey(request, radiuskey_instance, **_kwargs):
    """Destruction d'un radiuskey"""
    if request.method == "POST":
Gabriel Detraz's avatar
Gabriel Detraz committed
278 279 280 281 282 283
        try:
            radiuskey_instance.delete()
            messages.success(request, "La radiuskey a été détruite")
        except ProtectedError:
            messages.error(request, "Erreur la\
                clef ne peut être supprimé, elle est affectée à des switchs")
284 285 286 287 288 289 290 291 292
        return redirect(reverse('preferences:display-options'))
    return form(
        {'objet': radiuskey_instance, 'objet_name': 'radiuskey'},
        'preferences/delete.html',
        request
        )


@login_required
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
@can_create(SwitchManagementCred)
def add_switchmanagementcred(request):
    """Ajout de creds de management"""
    switchmanagementcred = SwitchManagementCredForm(request.POST or None)
    if switchmanagementcred.is_valid():
        switchmanagementcred.save()
        messages.success(request, "Ces creds ont été ajoutés")
        return redirect(reverse('preferences:display-options'))
    return form(
        {'preferenceform': switchmanagementcred, 'action_name': 'Ajouter'},
        'preferences/preferences.html',
        request
        )

@can_edit(SwitchManagementCred)
def edit_switchmanagementcred(request, switchmanagementcred_instance, **_kwargs):
    """Edition des creds de management"""
    switchmanagementcred = SwitchManagementCredForm(request.POST or None, instance=switchmanagementcred_instance)
    if switchmanagementcred.is_valid():
        switchmanagementcred.save()
        messages.success(request, "Creds de managament modifié")
        return redirect(reverse('preferences:display-options'))
    return form(
        {'preferenceform': switchmanagementcred, 'action_name': 'Editer'},
        'preferences/preferences.html',
        request
    )


@login_required
@can_delete(SwitchManagementCred)
def del_switchmanagementcred(request, switchmanagementcred_instance, **_kwargs):
    """Destruction d'un switchmanagementcred"""
    if request.method == "POST":
Gabriel Detraz's avatar
Gabriel Detraz committed
327 328 329 330 331 332
        try:
            switchmanagementcred_instance.delete()
            messages.success(request, "Ces creds ont été détruits")
        except ProtectedError:
            messages.error(request, "Erreur ces\
                creds ne peuvent être supprimés, ils sont affectés à des switchs")
333 334 335 336 337 338 339 340 341
        return redirect(reverse('preferences:display-options'))
    return form(
        {'objet': switchmanagementcred_instance, 'objet_name': 'switchmanagementcred'},
        'preferences/delete.html',
        request
        )


@login_required
342 343
@can_create(MailContact)
def add_mailcontact(request):
344
    """Add a contact email adress."""
345 346 347 348 349
    mailcontact = MailContactForm(
        request.POST or None,
        request.FILES or None
    )
    if mailcontact.is_valid():
350
        mailcontact.save()
351
        messages.success(request, _("The contact email address was created."))
352 353
        return redirect(reverse('preferences:display-options'))
    return form(
354 355
        {'preferenceform': mailcontact,
            'action_name': _("Add a contact email address")},
356 357 358 359 360 361 362 363
        'preferences/preferences.html',
        request
        )


@login_required
@can_edit(MailContact)
def edit_mailcontact(request, mailcontact_instance, **_kwargs):
364
    """Edit contact email adress."""
365 366 367 368 369 370
    mailcontact = MailContactForm(
        request.POST or None,
        request.FILES or None,
        instance=mailcontact_instance
    )
    if mailcontact.is_valid():
371
        mailcontact.save()
372
        messages.success(request, _("The contact email address was edited."))
373 374
        return redirect(reverse('preferences:display-options'))
    return form(
375
        {'preferenceform': mailcontact, 'action_name': _("Edit")},
376 377 378 379 380 381 382 383
        'preferences/preferences.html',
        request
    )


@login_required
@can_delete_set(MailContact)
def del_mailcontact(request, instances):
384
    """Delete an email adress"""
385 386 387 388 389 390 391
    mailcontacts = DelMailContactForm(
        request.POST or None,
        instances=instances
    )
    if mailcontacts.is_valid():
        mailcontacts_dels = mailcontacts.cleaned_data['mailcontacts']
        for mailcontacts_del in mailcontacts_dels:
392
            mailcontacts_del.delete()
393 394
            messages.success(request,
                    _("The contact email adress was deleted."))
395 396
        return redirect(reverse('preferences:display-options'))
    return form(
397
        {'preferenceform': mailcontacts, 'action_name': _("Delete")},
398 399 400
        'preferences/preferences.html',
        request
    )
401