From f42ed988cba0cb59bb1a20d0f8275037f291d547 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Thu, 21 Jul 2016 02:30:36 +0200 Subject: [PATCH] Gestion de l'historique dans topologie --- re2o/templates/re2o/aff_history.html | 16 +++++ re2o/templates/re2o/history.html | 12 ++++ topologie/admin.py | 7 +- topologie/models.py | 1 + .../templates/topologie/aff_chambres.html | 3 +- topologie/templates/topologie/aff_port.html | 3 +- topologie/templates/topologie/aff_switch.html | 3 +- topologie/urls.py | 3 + topologie/views.py | 64 +++++++++++++++++-- 9 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 re2o/templates/re2o/aff_history.html create mode 100644 re2o/templates/re2o/history.html diff --git a/re2o/templates/re2o/aff_history.html b/re2o/templates/re2o/aff_history.html new file mode 100644 index 00000000..d2019c67 --- /dev/null +++ b/re2o/templates/re2o/aff_history.html @@ -0,0 +1,16 @@ + + + + + + + + + {% for rev in reversions %} + + + + + + {% endfor %} +
DateCableurCommentaire
{{ rev.revision.date_created }}{{ rev.revision.user }}{{ rev.revision.comment }}
diff --git a/re2o/templates/re2o/history.html b/re2o/templates/re2o/history.html new file mode 100644 index 00000000..d78eb377 --- /dev/null +++ b/re2o/templates/re2o/history.html @@ -0,0 +1,12 @@ +{% extends "re2o/sidebar.html" %} +{% load bootstrap3 %} + +{% block title %}Historique{% endblock %} + +{% block content %} +

Historique de {{ object }}

+ {% include "re2o/aff_history.html" with reversions=reversions %} +
+
+
+{% endblock %} diff --git a/topologie/admin.py b/topologie/admin.py index 68598ac1..6d651876 100644 --- a/topologie/admin.py +++ b/topologie/admin.py @@ -1,15 +1,16 @@ from django.contrib import admin +from reversion.admin import VersionAdmin from .models import Port, Room, Switch -class SwitchAdmin(admin.ModelAdmin): +class SwitchAdmin(VersionAdmin): list_display = ('building','number','details') -class PortAdmin(admin.ModelAdmin): +class PortAdmin(VersionAdmin): list_display = ('switch', 'port','room','machine_interface','details') -class RoomAdmin(admin.ModelAdmin): +class RoomAdmin(VersionAdmin): list_display = ('name','details') admin.site.register(Port, PortAdmin) diff --git a/topologie/models.py b/topologie/models.py index 47350f76..f85f7fa7 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -3,6 +3,7 @@ from django.forms import ModelForm, Form from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.fields import GenericForeignKey from django.core.exceptions import ValidationError +import reversion def make_port_related(port): related_port = port.related diff --git a/topologie/templates/topologie/aff_chambres.html b/topologie/templates/topologie/aff_chambres.html index 2937a051..b77ddbcc 100644 --- a/topologie/templates/topologie/aff_chambres.html +++ b/topologie/templates/topologie/aff_chambres.html @@ -11,7 +11,8 @@ {{room.name}} {{room.details}} {% if is_infra %} Editer - Supprimer{% endif %} + Supprimer{% endif %} + Historique {% endfor %} diff --git a/topologie/templates/topologie/aff_port.html b/topologie/templates/topologie/aff_port.html index 4976a9fd..bb0b7a3a 100644 --- a/topologie/templates/topologie/aff_port.html +++ b/topologie/templates/topologie/aff_port.html @@ -16,7 +16,8 @@ {{ port.machine_interface }} {{ port.related }} {{ port.details }} - {% if is_infra %} Editer{% endif %} + {% if is_infra %} Editer{% endif %} + Historique {% endfor %} diff --git a/topologie/templates/topologie/aff_switch.html b/topologie/templates/topologie/aff_switch.html index 24d04761..cc80b12e 100644 --- a/topologie/templates/topologie/aff_switch.html +++ b/topologie/templates/topologie/aff_switch.html @@ -12,7 +12,8 @@ {{switch.building}} {{switch.number}} {{switch.details}} - Configurer + Configurer + Historique {% endfor %} diff --git a/topologie/urls.py b/topologie/urls.py index 7f75a096..e8df7722 100644 --- a/topologie/urls.py +++ b/topologie/urls.py @@ -10,6 +10,9 @@ urlpatterns = [ url(r'^edit_room/(?P[0-9]+)$', views.edit_room, name='edit-room'), url(r'^del_room/(?P[0-9]+)$', views.del_room, name='del-room'), url(r'^switch/(?P[0-9]+)$', views.index_port, name='index-port'), + url(r'^history/(?Pswitch)/(?P[0-9]+)$', views.history, name='history'), + url(r'^history/(?Pport)/(?P[0-9]+)$', views.history, name='history'), + url(r'^history/(?Proom)/(?P[0-9]+)$', views.history, name='history'), url(r'^edit_port/(?P[0-9]+)$', views.edit_port, name='edit-port'), url(r'^new_port/(?P[0-9]+)$', views.new_port, name='new-port'), url(r'^edit_switch/(?P[0-9]+)$', views.edit_switch, name='edit-switch'), diff --git a/topologie/views.py b/topologie/views.py index 1b93840d..127add5f 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -2,6 +2,8 @@ from django.shortcuts import render, redirect from django.contrib import messages from django.contrib.auth.decorators import login_required, permission_required from django.db import IntegrityError +from django.db import transaction +from reversion import revisions as reversion from topologie.models import Switch, Port, Room from topologie.forms import EditPortForm, EditSwitchForm, AddPortForm, EditRoomForm @@ -13,6 +15,33 @@ def index(request): switch_list = Switch.objects.order_by('building', 'number') return render(request, 'topologie/index.html', {'switch_list': switch_list}) +@login_required +@permission_required('cableur') +def history(request, object, id): + if object == 'switch': + try: + object_instance = Switch.objects.get(pk=id) + except Switch.DoesNotExist: + messages.error(request, "Switch inexistant") + return redirect("/topologie/") + elif object == 'port': + try: + object_instance = Port.objects.get(pk=id) + except Port.DoesNotExist: + messages.error(request, "Port inexistant") + return redirect("/topologie/") + elif object == 'room': + try: + object_instance = Room.objects.get(pk=id) + except Room.DoesNotExist: + messages.error(request, "Chambre inexistante") + return redirect("/topologie/") + else: + messages.error(request, "Objet inconnu") + return redirect("/topologie/") + reversions = reversion.get_for_object(object_instance) + return render(request, 're2o/history.html', {'reversions': reversions, 'object': object_instance}) + @login_required @permission_required('cableur') def index_port(request, switch_id): @@ -43,7 +72,10 @@ def new_port(request, switch_id): port = port.save(commit=False) port.switch = switch try: - port.save() + with transaction.atomic(), reversion.create_revision(): + port.save() + reversion.set_user(request.user) + reversion.set_comment("Création") messages.success(request, "Port ajouté") except IntegrityError: messages.error(request,"Ce port existe déjà" ) @@ -60,7 +92,10 @@ def edit_port(request, port_id): return redirect("/topologie/") port = EditPortForm(request.POST or None, instance=port) if port.is_valid(): - port.save() + with transaction.atomic(), reversion.create_revision(): + port.save() + reversion.set_user(request.user) + reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in port.changed_data)) messages.success(request, "Le port a bien été modifié") return redirect("/topologie/") return form({'topoform':port}, 'topologie/topo.html', request) @@ -70,7 +105,10 @@ def edit_port(request, port_id): def new_switch(request): switch = EditSwitchForm(request.POST or None) if switch.is_valid(): - switch.save() + with transaction.atomic(), reversion.create_revision(): + switch.save() + reversion.set_user(request.user) + reversion.set_comment("Création") messages.success(request, "Le switch a été créé") return redirect("/topologie/") return form({'topoform':switch}, 'topologie/topo.html', request) @@ -85,7 +123,10 @@ def edit_switch(request, switch_id): return redirect("/topologie/") switch = EditSwitchForm(request.POST or None, instance=switch) if switch.is_valid(): - switch.save() + with transaction.atomic(), reversion.create_revision(): + switch.save() + reversion.set_user(request.user) + reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in switch.changed_data)) messages.success(request, "Le switch a bien été modifié") return redirect("/topologie/") return form({'topoform':switch}, 'topologie/topo.html', request) @@ -95,7 +136,10 @@ def edit_switch(request, switch_id): def new_room(request): room = EditRoomForm(request.POST or None) if room.is_valid(): - room.save() + with transaction.atomic(), reversion.create_revision(): + room.save() + reversion.set_user(request.user) + reversion.set_comment("Création") messages.success(request, "La chambre a été créé") return redirect("/topologie/index_room/") return form({'topoform':room}, 'topologie/topo.html', request) @@ -110,7 +154,10 @@ def edit_room(request, room_id): return redirect("/topologie/index_room/") room = EditRoomForm(request.POST or None, instance=room) if room.is_valid(): - room.save() + with transaction.atomic(), reversion.create_revision(): + room.save() + reversion.set_user(request.user) + reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in room.changed_data)) messages.success(request, "La chambre a bien été modifiée") return redirect("/topologie/index_room/") return form({'topoform':room}, 'topologie/topo.html', request) @@ -124,7 +171,10 @@ def del_room(request, room_id): messages.error(request, u"Chambre inexistante" ) return redirect("/topologie/index_room/") if request.method == "POST": - room.delete() + with transaction.atomic(), reversion.create_revision(): + room.delete() + reversion.set_user(request.user) + reversion.set_comment("Destruction") messages.success(request, "La chambre/prise a été détruite") return redirect("/topologie/index_room/") return form({'objet': room, 'objet_name': 'Chambre'}, 'topologie/delete.html', request)