views.py 11.4 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
root's avatar
root committed
8
from reversion.models import Version
Dalahro's avatar
Dalahro committed
9

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

15 16 17 18
from machines.forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm
from machines.views import free_ip, full_domain_validator, assign_ipv4

from re2o.settings import ASSO_PSEUDO, PAGINATION_NUMBER
chirac's avatar
chirac committed
19

chirac's avatar
chirac committed
20
@login_required
chirac's avatar
chirac committed
21
@permission_required('cableur')
Dalahro's avatar
Dalahro committed
22
def index(request):
chirac's avatar
chirac committed
23
    switch_list = Switch.objects.order_by('location')
24
    return render(request, 'topologie/index.html', {'switch_list': switch_list})
Dalahro's avatar
Dalahro committed
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
@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/")
root's avatar
root committed
50
    reversions = Version.objects.get_for_object(object_instance)
chirac's avatar
chirac committed
51 52 53 54 55 56 57 58 59 60
    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)
61 62
    return render(request, 're2o/history.html', {'reversions': reversions, 'object': object_instance})

chirac's avatar
chirac committed
63
@login_required
chirac's avatar
chirac committed
64
@permission_required('cableur')
Dalahro's avatar
Dalahro committed
65 66 67 68 69 70 71
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')
72
    return render(request, 'topologie/index_p.html', {'port_list':port_list, 'id_switch':switch_id, 'nom_switch':switch})
Dalahro's avatar
Dalahro committed
73

chirac's avatar
chirac committed
74 75 76 77 78 79
@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
80
@login_required
81
@permission_required('infra')
Dalahro's avatar
Dalahro committed
82 83 84 85 86 87 88 89 90 91 92
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:
93 94 95 96
            with transaction.atomic(), reversion.create_revision():
                port.save()
                reversion.set_user(request.user)
                reversion.set_comment("Création")
Dalahro's avatar
Dalahro committed
97 98
            messages.success(request, "Port ajouté")
        except IntegrityError:
99
            messages.error(request,"Ce port existe déjà" )
Dalahro's avatar
Dalahro committed
100
        return redirect("/topologie/switch/" + switch_id)
chirac's avatar
chirac committed
101
    return form({'topoform':port}, 'topologie/topo.html', request)
Dalahro's avatar
Dalahro committed
102

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

@login_required
122
@permission_required('infra')
Dalahro's avatar
Dalahro committed
123
def new_switch(request):
124 125 126
    switch = NewSwitchForm(request.POST or None)
    machine = NewMachineForm(request.POST or None)
    interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',)))
chirac's avatar
chirac committed
127
    domain = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)))
128 129 130 131 132 133 134 135 136 137
    if switch.is_valid() and machine.is_valid() and interface.is_valid():
        try:
            user = User.objects.get(pseudo=ASSO_PSEUDO)
        except User.DoesNotExist:
            messages.error(request, "L'user %s n'existe pas encore, veuillez le créer" % ASSO_PSEUDO)
            return redirect("/topologie/")
        new_machine = machine.save(commit=False)
        new_machine.user = user
        new_interface = interface.save(commit=False)
        new_switch = switch.save(commit=False)
chirac's avatar
chirac committed
138 139
        new_domain = domain.save(commit=False)
        if full_domain_validator(request, new_domain):
140 141 142 143 144 145 146 147 148 149 150 151 152
            with transaction.atomic(), reversion.create_revision():
                new_machine.save()
                reversion.set_user(request.user)
                reversion.set_comment("Création")
            new_interface.machine = new_machine
            if free_ip(new_interface.type.ip_type) and not new_interface.ipv4:
                new_interface = assign_ipv4(new_interface)
            elif not new_interface.ipv4:
                messages.error(request, "Il n'y a plus d'ip disponibles")
            with transaction.atomic(), reversion.create_revision():
                new_interface.save()
                reversion.set_user(request.user)
                reversion.set_comment("Création")
chirac's avatar
chirac committed
153 154 155 156 157
            new_domain.interface_parent = new_interface
            with transaction.atomic(), reversion.create_revision():
                new_domain.save()
                reversion.set_user(request.user)
                reversion.set_comment("Création")
158 159 160 161 162 163 164
            new_switch.switch_interface = new_interface
            with transaction.atomic(), reversion.create_revision():
                new_switch.save()
                reversion.set_user(request.user)
                reversion.set_comment("Création")
            messages.success(request, "Le switch a été crée")
            return redirect("/topologie/")
chirac's avatar
chirac committed
165
    return form({'topoform':switch, 'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'topologie/switch.html', request)
Dalahro's avatar
Dalahro committed
166

chirac's avatar
chirac committed
167
@login_required
168
@permission_required('infra')
Dalahro's avatar
Dalahro committed
169 170 171 172 173 174
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/")
chirac's avatar
chirac committed
175 176 177
    switch_form = EditSwitchForm(request.POST or None, instance=switch)
    machine_form = EditMachineForm(request.POST or None, instance=switch.switch_interface.machine)
    interface_form = EditInterfaceForm(request.POST or None, instance=switch.switch_interface)
chirac's avatar
chirac committed
178
    domain_form = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)), instance=switch.switch_interface.domain)
chirac's avatar
chirac committed
179 180 181 182
    if switch_form.is_valid() and machine_form.is_valid() and interface_form.is_valid():
        new_interface = interface_form.save(commit=False)
        new_machine = machine_form.save(commit=False)
        new_switch = switch_form.save(commit=False)
chirac's avatar
chirac committed
183
        new_domain = domain.save(commit=False)
chirac's avatar
chirac committed
184 185 186 187 188 189 190 191 192
        if full_domain_validator(request, new_interface):
            with transaction.atomic(), reversion.create_revision():
                new_machine.save()
                reversion.set_user(request.user)
                reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in machine_form.changed_data))
            with transaction.atomic(), reversion.create_revision():
                new_interface.save()
                reversion.set_user(request.user)
                reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in interface_form.changed_data))
chirac's avatar
chirac committed
193 194 195 196
            with transaction.atomic(), reversion.create_revision():
                new_domain.save()
                reversion.set_user(request.user)
                reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in domain_form.changed_data))
chirac's avatar
chirac committed
197 198 199 200 201 202
            with transaction.atomic(), reversion.create_revision():
                new_switch.save()
                reversion.set_user(request.user)
                reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in switch_form.changed_data))
            messages.success(request, "Le switch a bien été modifié")
            return redirect("/topologie/")
chirac's avatar
chirac committed
203
    return form({'topoform':switch_form, 'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'topologie/switch.html', request)
chirac's avatar
chirac committed
204 205 206 207 208 209

@login_required
@permission_required('infra')
def new_room(request):
    room = EditRoomForm(request.POST or None)
    if room.is_valid():
210 211 212 213
        with transaction.atomic(), reversion.create_revision():
            room.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
chirac's avatar
chirac committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227
        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():
228 229 230 231
        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
232 233 234 235 236 237 238 239 240 241 242 243 244
        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":
245 246 247 248
        with transaction.atomic(), reversion.create_revision():
            room.delete()
            reversion.set_user(request.user)
            reversion.set_comment("Destruction")
chirac's avatar
chirac committed
249 250 251
        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)