views.py 3.02 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
Dalahro's avatar
Dalahro committed
5
6

from topologie.models import Switch, Port
Dalahro's avatar
Dalahro committed
7
from topologie.forms import EditPortForm, EditSwitchForm, AddPortForm
Dalahro's avatar
Dalahro committed
8
9
from users.views import form

chirac's avatar
chirac committed
10
@login_required
Dalahro's avatar
Dalahro committed
11
12
13
14
def index(request):
    switch_list = Switch.objects.order_by('building', 'number')
    return render(request, 'topologie/index.html', {'switch_list': switch_list})

chirac's avatar
chirac committed
15
@login_required
Dalahro's avatar
Dalahro committed
16
17
18
19
20
21
22
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')
Dalahro's avatar
Dalahro committed
23
24
    return render(request, 'topologie/index_p.html', {'port_list':port_list, 'id_switch':switch_id, 'nom_switch':switch})

chirac's avatar
chirac committed
25
@login_required
26
@permission_required('admin')
Dalahro's avatar
Dalahro committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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:
41
            messages.error(request,"Ce port existe déjà" )
Dalahro's avatar
Dalahro committed
42
43
        return redirect("/topologie/switch/" + switch_id)
    return form({'topoform':port}, 'topologie/port.html', request)
Dalahro's avatar
Dalahro committed
44

chirac's avatar
chirac committed
45
@login_required
46
@permission_required('admin')
Dalahro's avatar
Dalahro committed
47
48
49
50
51
52
53
54
55
56
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é")
Dalahro's avatar
Dalahro committed
57
        return redirect("/topologie/")
Dalahro's avatar
Dalahro committed
58
    return form({'topoform':port}, 'topologie/port.html', request)
chirac's avatar
chirac committed
59
60

@login_required
61
@permission_required('admin')
Dalahro's avatar
Dalahro committed
62
63
64
65
66
67
68
69
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)

chirac's avatar
chirac committed
70
@login_required
71
@permission_required('admin')
Dalahro's avatar
Dalahro committed
72
73
74
75
76
77
78
79
80
81
82
83
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)