2016-07-06 19:50:15 +00:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.contrib import messages
|
2016-07-09 02:12:09 +00:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2016-07-06 21:29:31 +00:00
|
|
|
from django.db import IntegrityError
|
2016-07-06 19:50:15 +00:00
|
|
|
|
|
|
|
from topologie.models import Switch, Port
|
2016-07-06 21:29:31 +00:00
|
|
|
from topologie.forms import EditPortForm, EditSwitchForm, AddPortForm
|
2016-07-06 19:50:15 +00:00
|
|
|
from users.views import form
|
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2016-07-09 15:16:44 +00:00
|
|
|
@permission_required('cableur')
|
2016-07-06 19:50:15 +00:00
|
|
|
def index(request):
|
|
|
|
switch_list = Switch.objects.order_by('building', 'number')
|
2016-07-10 02:02:48 +00:00
|
|
|
return render(request, 'topologie/index.html', {'switch_list': switch_list})
|
2016-07-06 19:50:15 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2016-07-09 15:16:44 +00:00
|
|
|
@permission_required('cableur')
|
2016-07-06 19:50:15 +00:00
|
|
|
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')
|
2016-07-10 02:02:48 +00:00
|
|
|
return render(request, 'topologie/index_p.html', {'port_list':port_list, 'id_switch':switch_id, 'nom_switch':switch})
|
2016-07-06 21:29:31 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2016-07-09 13:47:10 +00:00
|
|
|
@permission_required('infra')
|
2016-07-06 21:29:31 +00:00
|
|
|
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:
|
|
|
|
port.save()
|
|
|
|
messages.success(request, "Port ajouté")
|
|
|
|
except IntegrityError:
|
2016-07-06 22:25:12 +00:00
|
|
|
messages.error(request,"Ce port existe déjà" )
|
2016-07-06 21:29:31 +00:00
|
|
|
return redirect("/topologie/switch/" + switch_id)
|
|
|
|
return form({'topoform':port}, 'topologie/port.html', request)
|
2016-07-06 19:50:15 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2016-07-09 13:47:10 +00:00
|
|
|
@permission_required('infra')
|
2016-07-06 19:50:15 +00:00
|
|
|
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():
|
|
|
|
port.save()
|
|
|
|
messages.success(request, "Le port a bien été modifié")
|
2016-07-06 21:29:31 +00:00
|
|
|
return redirect("/topologie/")
|
2016-07-06 19:50:15 +00:00
|
|
|
return form({'topoform':port}, 'topologie/port.html', request)
|
2016-07-08 10:35:53 +00:00
|
|
|
|
|
|
|
@login_required
|
2016-07-09 13:47:10 +00:00
|
|
|
@permission_required('infra')
|
2016-07-06 21:29:31 +00:00
|
|
|
def new_switch(request):
|
|
|
|
switch = EditSwitchForm(request.POST or None)
|
|
|
|
if switch.is_valid():
|
|
|
|
switch.save()
|
|
|
|
messages.success(request, "Le switch a été créé")
|
|
|
|
return redirect("/topologie/")
|
|
|
|
return form({'topoform':switch}, 'topologie/port.html', request)
|
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2016-07-09 13:47:10 +00:00
|
|
|
@permission_required('infra')
|
2016-07-06 21:29:31 +00:00
|
|
|
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():
|
|
|
|
switch.save()
|
|
|
|
messages.success(request, "Le switch a bien été modifié")
|
|
|
|
return redirect("/topologie/")
|
|
|
|
return form({'topoform':switch}, 'topologie/port.html', request)
|