views.py 7.63 KB
Newer Older
Dalahro's avatar
Dalahro committed
1 2
from django.shortcuts import render, redirect
from django.contrib import messages
3
from django.contrib.auth.decorators import login_required, permission_required
Dalahro's avatar
Dalahro committed
4
from django.db import IntegrityError
5
from django.db import transaction
chirac's avatar
chirac committed
6
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
7
from reversion import revisions as reversion
Dalahro's avatar
Dalahro committed
8

chirac's avatar
chirac committed
9 10
from topologie.models import Switch, Port, Room
from topologie.forms import EditPortForm, EditSwitchForm, AddPortForm, EditRoomForm
Dalahro's avatar
Dalahro committed
11 12
from users.views import form

chirac's avatar
chirac committed
13 14
from re2o.settings import PAGINATION_NUMBER

chirac's avatar
chirac committed
15
@login_required
chirac's avatar
chirac committed
16
@permission_required('cableur')
Dalahro's avatar
Dalahro committed
17 18
def index(request):
    switch_list = Switch.objects.order_by('building', 'number')
19
    return render(request, 'topologie/index.html', {'switch_list': switch_list})
Dalahro's avatar
Dalahro committed
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
@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)
chirac's avatar
chirac committed
46 47 48 49 50 51 52 53 54 55
    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)
56 57
    return render(request, 're2o/history.html', {'reversions': reversions, 'object': object_instance})

chirac's avatar
chirac committed
58
@login_required
chirac's avatar
chirac committed
59
@permission_required('cableur')
Dalahro's avatar
Dalahro committed
60 61 62 63 64 65 66
def index_port(request, switch_id):
    try:
        switch = Switch.objects.get(pk=switch_id)
    except Switch.DoesNotExist:
        messages.error(request, u"Switch inexistant")
        return redirect("/topologie/")
    port_list = Port.objects.filter(switch = switch).order_by('port')
67
    return render(request, 'topologie/index_p.html', {'port_list':port_list, 'id_switch':switch_id, 'nom_switch':switch})
Dalahro's avatar
Dalahro committed
68

chirac's avatar
chirac committed
69 70 71 72 73 74
@login_required
@permission_required('cableur')
def index_room(request):
    room_list = Room.objects.order_by('name')
    return render(request, 'topologie/index_room.html', {'room_list': room_list})

chirac's avatar
chirac committed
75
@login_required
76
@permission_required('infra')
Dalahro's avatar
Dalahro committed
77 78 79 80 81 82 83 84 85 86 87
def new_port(request, switch_id):
    try:
        switch = Switch.objects.get(pk=switch_id)
    except Switch.DoesNotExist:
        messages.error(request, u"Switch inexistant")
        return redirect("/topologie/")
    port = AddPortForm(request.POST or None)
    if port.is_valid():
        port = port.save(commit=False)
        port.switch = switch
        try:
88 89 90 91
            with transaction.atomic(), reversion.create_revision():
                port.save()
                reversion.set_user(request.user)
                reversion.set_comment("Création")
Dalahro's avatar
Dalahro committed
92 93
            messages.success(request, "Port ajouté")
        except IntegrityError:
94
            messages.error(request,"Ce port existe déjà" )
Dalahro's avatar
Dalahro committed
95
        return redirect("/topologie/switch/" + switch_id)
chirac's avatar
chirac committed
96
    return form({'topoform':port}, 'topologie/topo.html', request)
Dalahro's avatar
Dalahro committed
97

chirac's avatar
chirac committed
98
@login_required
99
@permission_required('infra')
Dalahro's avatar
Dalahro committed
100 101 102 103 104 105 106 107
def edit_port(request, port_id):
    try:
        port = Port.objects.get(pk=port_id)
    except Port.DoesNotExist:
        messages.error(request, u"Port inexistant")
        return redirect("/topologie/")
    port = EditPortForm(request.POST or None, instance=port)
    if port.is_valid():
108 109 110 111
        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))
Dalahro's avatar
Dalahro committed
112
        messages.success(request, "Le port a bien été modifié")
Dalahro's avatar
Dalahro committed
113
        return redirect("/topologie/")
chirac's avatar
chirac committed
114
    return form({'topoform':port}, 'topologie/topo.html', request)
chirac's avatar
chirac committed
115 116

@login_required
117
@permission_required('infra')
Dalahro's avatar
Dalahro committed
118 119 120
def new_switch(request):
    switch = EditSwitchForm(request.POST or None)
    if switch.is_valid():
121 122 123 124
        with transaction.atomic(), reversion.create_revision():
            switch.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
Dalahro's avatar
Dalahro committed
125 126
        messages.success(request, "Le switch a été créé")
        return redirect("/topologie/")
chirac's avatar
chirac committed
127
    return form({'topoform':switch}, 'topologie/topo.html', request)
Dalahro's avatar
Dalahro committed
128

chirac's avatar
chirac committed
129
@login_required
130
@permission_required('infra')
Dalahro's avatar
Dalahro committed
131 132 133 134 135 136 137 138
def edit_switch(request, switch_id):
    try:
        switch = Switch.objects.get(pk=switch_id)
    except Switch.DoesNotExist:
        messages.error(request, u"Switch inexistant")
        return redirect("/topologie/")
    switch = EditSwitchForm(request.POST or None, instance=switch)
    if switch.is_valid():
139 140 141 142
        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))
Dalahro's avatar
Dalahro committed
143 144
        messages.success(request, "Le switch a bien été modifié")
        return redirect("/topologie/")
chirac's avatar
chirac committed
145 146 147 148 149 150 151
    return form({'topoform':switch}, 'topologie/topo.html', request)

@login_required
@permission_required('infra')
def new_room(request):
    room = EditRoomForm(request.POST or None)
    if room.is_valid():
152 153 154 155
        with transaction.atomic(), reversion.create_revision():
            room.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
chirac's avatar
chirac committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169
        messages.success(request, "La chambre a été créé")
        return redirect("/topologie/index_room/")
    return form({'topoform':room}, 'topologie/topo.html', request)

@login_required
@permission_required('infra')
def edit_room(request, room_id):
    try:
        room = Room.objects.get(pk=room_id)
    except Room.DoesNotExist:
        messages.error(request, u"Chambre inexistante")
        return redirect("/topologie/index_room/")
    room = EditRoomForm(request.POST or None, instance=room)
    if room.is_valid():
170 171 172 173
        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))
chirac's avatar
chirac committed
174 175 176 177 178 179 180 181 182 183 184 185 186
        messages.success(request, "La chambre a bien été modifiée")
        return redirect("/topologie/index_room/")
    return form({'topoform':room}, 'topologie/topo.html', request)

@login_required
@permission_required('infra')
def del_room(request, room_id):
    try:
        room = Room.objects.get(pk=room_id)
    except Room.DoesNotExist:
        messages.error(request, u"Chambre inexistante" )
        return redirect("/topologie/index_room/")
    if request.method == "POST":
187 188 189 190
        with transaction.atomic(), reversion.create_revision():
            room.delete()
            reversion.set_user(request.user)
            reversion.set_comment("Destruction")
chirac's avatar
chirac committed
191 192 193
        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)