views.py 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Dalahro's avatar
Dalahro committed
23 24
from django.shortcuts import render, redirect
from django.contrib import messages
25
from django.contrib.auth.decorators import login_required, permission_required
Dalahro's avatar
Dalahro committed
26
from django.db import IntegrityError
27
from django.db import transaction
chirac's avatar
chirac committed
28
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
29
from reversion import revisions as reversion
root's avatar
root committed
30
from reversion.models import Version
Dalahro's avatar
Dalahro committed
31

chirac's avatar
chirac committed
32
from topologie.models import Switch, Port, Room
33
from topologie.forms import EditPortForm, NewSwitchForm, EditSwitchForm, AddPortForm, EditRoomForm
Dalahro's avatar
Dalahro committed
34
from users.views import form
35
from users.models import User
Dalahro's avatar
Dalahro committed
36

chirac's avatar
chirac committed
37
from machines.forms import AliasForm, NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm
38 39 40
from machines.views import free_ip, full_domain_validator, assign_ipv4

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

chirac's avatar
chirac committed
42
@login_required
chirac's avatar
chirac committed
43
@permission_required('cableur')
Dalahro's avatar
Dalahro committed
44
def index(request):
chirac's avatar
chirac committed
45
    switch_list = Switch.objects.order_by('location')
46
    return render(request, 'topologie/index.html', {'switch_list': switch_list})
Dalahro's avatar
Dalahro committed
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
@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
72
    reversions = Version.objects.get_for_object(object_instance)
chirac's avatar
chirac committed
73 74 75 76 77 78 79 80 81 82
    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)
83 84
    return render(request, 're2o/history.html', {'reversions': reversions, 'object': object_instance})

chirac's avatar
chirac committed
85
@login_required
chirac's avatar
chirac committed
86
@permission_required('cableur')
Dalahro's avatar
Dalahro committed
87 88 89 90 91 92 93
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')
94
    return render(request, 'topologie/index_p.html', {'port_list':port_list, 'id_switch':switch_id, 'nom_switch':switch})
Dalahro's avatar
Dalahro committed
95

chirac's avatar
chirac committed
96 97 98 99 100 101
@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
102
@login_required
103
@permission_required('infra')
Dalahro's avatar
Dalahro committed
104 105 106 107 108 109 110 111 112 113 114
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:
115 116 117 118
            with transaction.atomic(), reversion.create_revision():
                port.save()
                reversion.set_user(request.user)
                reversion.set_comment("Création")
Dalahro's avatar
Dalahro committed
119 120
            messages.success(request, "Port ajouté")
        except IntegrityError:
121
            messages.error(request,"Ce port existe déjà" )
Dalahro's avatar
Dalahro committed
122
        return redirect("/topologie/switch/" + switch_id)
chirac's avatar
chirac committed
123
    return form({'topoform':port}, 'topologie/topo.html', request)
Dalahro's avatar
Dalahro committed
124

chirac's avatar
chirac committed
125
@login_required
126
@permission_required('infra')
Dalahro's avatar
Dalahro committed
127 128
def edit_port(request, port_id):
    try:
129
        port_object = Port.objects.select_related('switch__switch_interface__domain__extension').select_related('machine_interface').select_related('room').select_related('related').get(pk=port_id)
Dalahro's avatar
Dalahro committed
130 131 132
    except Port.DoesNotExist:
        messages.error(request, u"Port inexistant")
        return redirect("/topologie/")
133
    port = EditPortForm(request.POST or None, instance=port_object)
Dalahro's avatar
Dalahro committed
134
    if port.is_valid():
135 136 137 138
        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
139
        messages.success(request, "Le port a bien été modifié")
140
        return redirect("/topologie/switch/" + str(port_object.switch.id))
chirac's avatar
chirac committed
141
    return form({'topoform':port}, 'topologie/topo.html', request)
chirac's avatar
chirac committed
142 143

@login_required
144
@permission_required('infra')
Dalahro's avatar
Dalahro committed
145
def new_switch(request):
146 147 148
    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
149
    domain = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)))
150 151 152 153 154 155 156 157 158 159
    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
160 161
        new_domain = domain.save(commit=False)
        if full_domain_validator(request, new_domain):
162 163 164 165 166 167 168 169 170 171 172 173 174
            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
175 176 177 178 179
            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")
180 181 182 183 184 185 186
            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
187
    return form({'topoform':switch, 'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'topologie/switch.html', request)
Dalahro's avatar
Dalahro committed
188

chirac's avatar
chirac committed
189
@login_required
190
@permission_required('infra')
Dalahro's avatar
Dalahro committed
191 192 193 194 195 196
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/")
197 198 199
    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
200
    domain_form = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)), instance=switch.switch_interface.domain)
201 202 203 204
    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
205 206
        new_domain = domain_form.save(commit=False)
        if full_domain_validator(request, new_domain):
207 208 209 210 211 212 213 214
            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
215 216 217 218
            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))
219 220 221 222 223 224
            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
225
    return form({'topoform':switch_form, 'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'topologie/switch.html', request)
chirac's avatar
chirac committed
226 227 228 229 230 231

@login_required
@permission_required('infra')
def new_room(request):
    room = EditRoomForm(request.POST or None)
    if room.is_valid():
232 233 234 235
        with transaction.atomic(), reversion.create_revision():
            room.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
chirac's avatar
chirac committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249
        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():
250 251 252 253
        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
254 255 256 257 258 259 260 261 262 263 264 265 266
        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":
267 268 269 270
        with transaction.atomic(), reversion.create_revision():
            room.delete()
            reversion.set_user(request.user)
            reversion.set_comment("Destruction")
chirac's avatar
chirac committed
271 272 273
        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)