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 6.84 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 39 40 41
from django.db import transaction

from reversion import revisions as reversion

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

45
from .forms import ServiceForm, DelServiceForm
Gabriel Detraz's avatar
Gabriel Detraz committed
46 47 48 49 50 51 52 53
from .models import (
    Service,
    OptionalUser,
    OptionalMachine,
    AssoOption,
    MailMessageOption,
    GeneralOption,
    OptionalTopologie,
Gabriel Detraz's avatar
Gabriel Detraz committed
54
    HomeOption
Gabriel Detraz's avatar
Gabriel Detraz committed
55
)
56 57
from . import models
from . import forms
58 59 60


@login_required
61 62
@can_view_all(OptionalUser, OptionalMachine, OptionalTopologie, GeneralOption,
              AssoOption, MailMessageOption, HomeOption)
63
def display_options(request):
64 65 66 67 68 69 70
    """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
71
    homeoptions, _created = HomeOption.objects.get_or_create()
72
    mailmessageoptions, _created = MailMessageOption.objects.get_or_create()
73
    service_list = Service.objects.all()
74 75 76 77 78 79
    return form({
        'useroptions': useroptions,
        'machineoptions': machineoptions,
        'topologieoptions': topologieoptions,
        'generaloptions': generaloptions,
        'assooptions': assooptions,
Gabriel Detraz's avatar
Gabriel Detraz committed
80
        'homeoptions': homeoptions,
81 82 83 84
        'mailmessageoptions': mailmessageoptions,
        'service_list': service_list
        }, 'preferences/display_preferences.html', request)

85 86

@login_required
87 88 89 90
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)
91 92 93 94 95 96 97 98 99 100 101 102
    if not (model or form_instance):
        messages.error(request, "Objet  inconnu")
        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:
        messages.error(request, msg or "Vous ne pouvez pas éditer cette\
                   option.")
        return redirect(reverse('index'))
    options = form_instance(
        request.POST or None,
103
        request.FILES or None,
104 105 106 107 108 109 110 111 112
        instance=options_instance
    )
    if options.is_valid():
        with transaction.atomic(), reversion.create_revision():
            options.save()
            reversion.set_user(request.user)
            reversion.set_comment(
                "Champs modifié(s) : %s" % ', '.join(
                    field for field in options.changed_data
113 114
                )
            )
115
            messages.success(request, "Préférences modifiées")
116
        return redirect(reverse('preferences:display-options'))
117 118 119 120 121
    return form(
        {'options': options},
        'preferences/edit_preferences.html',
        request
    )
122

123

124
@login_required
125 126
@can_create(Service)
def add_service(request):
127
    """Ajout d'un service de la page d'accueil"""
Gabriel Detraz's avatar
Gabriel Detraz committed
128
    service = ServiceForm(request.POST or None, request.FILES or None)
129
    if service.is_valid():
130
        with transaction.atomic(), reversion.create_revision():
131
            service.save()
132 133
            reversion.set_user(request.user)
            reversion.set_comment("Création")
134
        messages.success(request, "Ce service a été ajouté")
135
        return redirect(reverse('preferences:display-options'))
136
    return form(
Maël Kervella's avatar
Maël Kervella committed
137
        {'preferenceform': service, 'action_name': 'Ajouter'},
138 139 140 141
        'preferences/preferences.html',
        request
        )

142 143

@login_required
144
@can_edit(Service)
145
def edit_service(request, service_instance, **_kwargs):
146
    """Edition des services affichés sur la page d'accueil"""
147
    service = ServiceForm(
148 149 150 151
        request.POST or None,
        request.FILES or None,
        instance=service_instance
    )
152
    if service.is_valid():
153
        with transaction.atomic(), reversion.create_revision():
154
            service.save()
155
            reversion.set_user(request.user)
156 157
            reversion.set_comment(
                "Champs modifié(s) : %s" % ', '.join(
158
                    field for field in service.changed_data
159 160
                    )
            )
161
        messages.success(request, "Service modifié")
162
        return redirect(reverse('preferences:display-options'))
163
    return form(
Maël Kervella's avatar
Maël Kervella committed
164
        {'preferenceform': service, 'action_name': 'Editer'},
165 166 167 168
        'preferences/preferences.html',
        request
    )

169 170

@login_required
171 172
@can_delete_set(Service)
def del_services(request, instances):
173
    """Suppression d'un service de la page d'accueil"""
174
    services = DelServiceForm(request.POST or None, instances=instances)
175 176 177 178 179 180 181
    if services.is_valid():
        services_dels = services.cleaned_data['services']
        for services_del in services_dels:
            try:
                with transaction.atomic(), reversion.create_revision():
                    services_del.delete()
                    reversion.set_user(request.user)
182
                messages.success(request, "Le service a été supprimée")
183
            except ProtectedError:
184 185
                messages.error(request, "Erreur le service\
                suivant %s ne peut être supprimé" % services_del)
186
        return redirect(reverse('preferences:display-options'))
187
    return form(
Maël Kervella's avatar
Maël Kervella committed
188
        {'preferenceform': services, 'action_name': 'Supprimer'},
189 190 191
        'preferences/preferences.html',
        request
    )