views.py 6.75 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 render, redirect
35 36
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
37
from django.db.models import ProtectedError
38 39 40 41 42
from django.db import transaction

from reversion.models import Version
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
45
from .forms import ServiceForm, DelServiceForm
46 47
from .models import Service, OptionalUser, OptionalMachine, AssoOption
from .models import MailMessageOption, GeneralOption, OptionalTopologie
48 49
from . import models
from . import forms
50 51 52


@login_required
Levy--Falk Hugo's avatar
Levy--Falk Hugo committed
53 54 55 56 57 58
@can_view_all(OptionalUser)
@can_view_all(OptionalMachine)
@can_view_all(OptionalTopologie)
@can_view_all(GeneralOption)
@can_view_all(AssoOption)
@can_view_all(MailMessageOption)
59
def display_options(request):
60 61 62 63 64 65 66 67
    """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()
    mailmessageoptions, _created = MailMessageOption.objects.get_or_create()
68
    service_list = Service.objects.all()
69 70 71 72 73 74 75 76 77 78
    return form({
        'useroptions': useroptions,
        'machineoptions': machineoptions,
        'topologieoptions': topologieoptions,
        'generaloptions': generaloptions,
        'assooptions': assooptions,
        'mailmessageoptions': mailmessageoptions,
        'service_list': service_list
        }, 'preferences/display_preferences.html', request)

79 80

@login_required
81 82 83 84
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)
85 86 87 88 89 90 91 92 93 94 95 96
    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,
97
        request.FILES or None,
98 99 100 101 102 103 104 105 106
        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
107 108
                )
            )
109
            messages.success(request, "Préférences modifiées")
110
        return redirect(reverse('preferences:display-options'))
111 112 113 114 115
    return form(
        {'options': options},
        'preferences/edit_preferences.html',
        request
    )
116

117

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

136 137

@login_required
138 139
@can_edit(Service)
def edit_service(request, service_instance, serviceid):
140
    """Edition des services affichés sur la page d'accueil"""
141 142
    service = ServiceForm(request.POST or None, instance=service_instance)
    if service.is_valid():
143
        with transaction.atomic(), reversion.create_revision():
144
            service.save()
145
            reversion.set_user(request.user)
146 147
            reversion.set_comment(
                "Champs modifié(s) : %s" % ', '.join(
148
                    field for field in service.changed_data
149 150
                    )
            )
151
        messages.success(request, "Service modifié")
152
        return redirect(reverse('preferences:display-options'))
153
    return form(
Gabriel Detraz's avatar
Gabriel Detraz committed
154
        {'preferenceform': service, 'action_name' : 'Editer'},
155 156 157 158
        'preferences/preferences.html',
        request
    )

159 160

@login_required
161 162
@can_delete_set(Service)
def del_services(request, instances):
163
    """Suppression d'un service de la page d'accueil"""
164
    services = DelServiceForm(request.POST or None, instances=instances)
165 166 167 168 169 170 171
    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)
172
                messages.success(request, "Le service a été supprimée")
173
            except ProtectedError:
174 175
                messages.error(request, "Erreur le service\
                suivant %s ne peut être supprimé" % services_del)
176
        return redirect(reverse('preferences:display-options'))
177
    return form(
Gabriel Detraz's avatar
Gabriel Detraz committed
178
        {'preferenceform': services, 'action_name' : 'Supprimer'},
179 180 181
        'preferences/preferences.html',
        request
    )