2017-06-25 22:50:55 +00:00
|
|
|
# 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
|
2017-10-14 04:03:53 +00:00
|
|
|
"""
|
|
|
|
Vue d'affichage, et de modification des réglages (réglages machine,
|
|
|
|
topologie, users, service...)
|
|
|
|
"""
|
2017-06-25 22:50:55 +00:00
|
|
|
|
2017-09-10 23:29:24 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-06-25 22:50:55 +00:00
|
|
|
|
2017-10-14 04:03:53 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2017-06-25 22:50:55 +00:00
|
|
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2017-10-14 04:03:53 +00:00
|
|
|
from django.db.models import ProtectedError
|
2017-06-25 22:50:55 +00:00
|
|
|
from django.db import transaction
|
|
|
|
|
|
|
|
from reversion.models import Version
|
|
|
|
from reversion import revisions as reversion
|
|
|
|
|
2017-10-14 04:03:53 +00:00
|
|
|
from re2o.views import form
|
2017-08-25 02:35:49 +00:00
|
|
|
from .forms import ServiceForm, DelServiceForm
|
2017-10-14 04:03:53 +00:00
|
|
|
from .models import Service, OptionalUser, OptionalMachine, AssoOption
|
|
|
|
from .models import MailMessageOption, GeneralOption, OptionalTopologie
|
2017-08-24 19:36:59 +00:00
|
|
|
from . import models
|
|
|
|
from . import forms
|
2017-06-25 22:50:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@permission_required('cableur')
|
|
|
|
def display_options(request):
|
2017-10-14 04:03:53 +00:00
|
|
|
"""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()
|
2017-08-25 02:35:49 +00:00
|
|
|
service_list = Service.objects.all()
|
2017-10-14 04:03:53 +00:00
|
|
|
return form({
|
|
|
|
'useroptions': useroptions,
|
|
|
|
'machineoptions': machineoptions,
|
|
|
|
'topologieoptions': topologieoptions,
|
|
|
|
'generaloptions': generaloptions,
|
|
|
|
'assooptions': assooptions,
|
|
|
|
'mailmessageoptions': mailmessageoptions,
|
|
|
|
'service_list': service_list
|
|
|
|
}, 'preferences/display_preferences.html', request)
|
|
|
|
|
2017-06-25 22:50:55 +00:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
@permission_required('admin')
|
2017-08-24 19:36:59 +00:00
|
|
|
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)
|
|
|
|
if model and form:
|
2017-10-14 04:03:53 +00:00
|
|
|
options_instance, _created = model.objects.get_or_create()
|
|
|
|
options = form_instance(
|
|
|
|
request.POST or None,
|
|
|
|
instance=options_instance
|
|
|
|
)
|
2017-08-24 19:36:59 +00:00
|
|
|
if options.is_valid():
|
2017-08-06 18:01:08 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
2017-08-24 19:36:59 +00:00
|
|
|
options.save()
|
2017-08-06 18:01:08 +00:00
|
|
|
reversion.set_user(request.user)
|
2017-10-14 04:03:53 +00:00
|
|
|
reversion.set_comment(
|
|
|
|
"Champs modifié(s) : %s" % ', '.join(
|
|
|
|
field for field in options.changed_data
|
|
|
|
)
|
|
|
|
)
|
2017-08-24 19:36:59 +00:00
|
|
|
messages.success(request, "Préférences modifiées")
|
|
|
|
return redirect("/preferences/")
|
2017-10-14 04:03:53 +00:00
|
|
|
return form(
|
|
|
|
{'options': options},
|
|
|
|
'preferences/edit_preferences.html',
|
|
|
|
request
|
|
|
|
)
|
2017-08-24 19:36:59 +00:00
|
|
|
else:
|
|
|
|
messages.error(request, "Objet inconnu")
|
2017-08-06 18:01:08 +00:00
|
|
|
return redirect("/preferences/")
|
2017-06-25 22:50:55 +00:00
|
|
|
|
2017-10-14 04:03:53 +00:00
|
|
|
|
2017-08-25 02:35:49 +00:00
|
|
|
@login_required
|
|
|
|
@permission_required('admin')
|
|
|
|
def add_services(request):
|
2017-10-14 04:03:53 +00:00
|
|
|
"""Ajout d'un service de la page d'accueil"""
|
2017-08-25 02:35:49 +00:00
|
|
|
services = ServiceForm(request.POST or None)
|
|
|
|
if services.is_valid():
|
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
services.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
|
|
|
messages.success(request, "Cet enregistrement ns a été ajouté")
|
|
|
|
return redirect("/preferences/")
|
2017-10-14 04:03:53 +00:00
|
|
|
return form(
|
|
|
|
{'preferenceform': services},
|
|
|
|
'preferences/preferences.html',
|
|
|
|
request
|
|
|
|
)
|
|
|
|
|
2017-08-25 02:35:49 +00:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
@permission_required('admin')
|
|
|
|
def edit_services(request, servicesid):
|
2017-10-14 04:03:53 +00:00
|
|
|
"""Edition des services affichés sur la page d'accueil"""
|
2017-08-25 02:35:49 +00:00
|
|
|
try:
|
|
|
|
services_instance = Service.objects.get(pk=servicesid)
|
|
|
|
except Service.DoesNotExist:
|
2017-10-14 04:03:53 +00:00
|
|
|
messages.error(request, u"Entrée inexistante")
|
2017-08-25 02:35:49 +00:00
|
|
|
return redirect("/preferences/")
|
|
|
|
services = ServiceForm(request.POST or None, instance=services_instance)
|
|
|
|
if services.is_valid():
|
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
services.save()
|
|
|
|
reversion.set_user(request.user)
|
2017-10-14 04:03:53 +00:00
|
|
|
reversion.set_comment(
|
|
|
|
"Champs modifié(s) : %s" % ', '.join(
|
|
|
|
field for field in services.changed_data
|
|
|
|
)
|
|
|
|
)
|
2017-08-25 02:35:49 +00:00
|
|
|
messages.success(request, "Service modifié")
|
|
|
|
return redirect("/preferences/")
|
2017-10-14 04:03:53 +00:00
|
|
|
return form(
|
|
|
|
{'preferenceform': services},
|
|
|
|
'preferences/preferences.html',
|
|
|
|
request
|
|
|
|
)
|
|
|
|
|
2017-08-25 02:35:49 +00:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
@permission_required('admin')
|
|
|
|
def del_services(request):
|
2017-10-14 04:03:53 +00:00
|
|
|
"""Suppression d'un service de la page d'accueil"""
|
2017-08-25 02:35:49 +00:00
|
|
|
services = DelServiceForm(request.POST or None)
|
|
|
|
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)
|
|
|
|
messages.success(request, "Le services a été supprimée")
|
|
|
|
except ProtectedError:
|
2017-10-14 04:03:53 +00:00
|
|
|
messages.error(request, "Erreur le service\
|
|
|
|
suivant %s ne peut être supprimé" % services_del)
|
2017-08-25 02:35:49 +00:00
|
|
|
return redirect("/preferences/")
|
2017-10-14 04:03:53 +00:00
|
|
|
return form(
|
|
|
|
{'preferenceform': services},
|
|
|
|
'preferences/preferences.html',
|
|
|
|
request
|
|
|
|
)
|
|
|
|
|
2017-08-25 02:35:49 +00:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
@permission_required('cableur')
|
2017-10-14 04:03:53 +00:00
|
|
|
def history(request, object_name, object_id):
|
|
|
|
"""Historique de creation et de modification d'un service affiché sur
|
|
|
|
la page d'accueil"""
|
|
|
|
if object_name == 'service':
|
2017-08-25 02:35:49 +00:00
|
|
|
try:
|
2017-10-14 04:03:53 +00:00
|
|
|
object_instance = Service.objects.get(pk=object_id)
|
2017-08-25 02:35:49 +00:00
|
|
|
except Service.DoesNotExist:
|
2017-10-14 04:03:53 +00:00
|
|
|
messages.error(request, "Service inexistant")
|
|
|
|
return redirect("/preferences/")
|
|
|
|
options, _created = GeneralOption.objects.get_or_create()
|
2017-08-25 02:35:49 +00:00
|
|
|
pagination_number = options.pagination_number
|
|
|
|
reversions = Version.objects.get_for_object(object_instance)
|
|
|
|
paginator = Paginator(reversions, pagination_number)
|
|
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
|
|
reversions = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
# If page is not an integer, deliver first page.
|
|
|
|
reversions = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
# If page is out of range (e.g. 9999), deliver last page of results.
|
|
|
|
reversions = paginator.page(paginator.num_pages)
|
2017-10-14 04:03:53 +00:00
|
|
|
return render(request, 're2o/history.html', {
|
|
|
|
'reversions': reversions,
|
|
|
|
'object': object_instance
|
|
|
|
})
|