views.py 15.8 KB
Newer Older
lhark's avatar
lhark committed
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
28
from django.db.models import ProtectedError
chirac's avatar
chirac committed
29
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
30
from reversion import revisions as reversion
root's avatar
root committed
31
from reversion.models import Version
Dalahro's avatar
Dalahro committed
32

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

chirac's avatar
chirac committed
38
from machines.forms import AliasForm, NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm
39
from preferences.models import AssoOption, GeneralOption
40

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):
45
    switch_list = Switch.objects.order_by('stack','stack_member_id','location').select_related('switch_interface__domain__extension').select_related('switch_interface__ipv4').select_related('switch_interface__domain')
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
@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/")
69
70
71
72
73
74
    elif object == 'stack':  
        try:
             object_instance = Stack.objects.get(pk=id)
        except Room.DoesNotExist:
             messages.error(request, "Stack inexistante")
             return redirect("/topologie/")
75
76
77
    else:
        messages.error(request, "Objet  inconnu")
        return redirect("/topologie/")
78
79
    options, created = GeneralOption.objects.get_or_create()
    pagination_number = options.pagination_number
root's avatar
root committed
80
    reversions = Version.objects.get_for_object(object_instance)
81
    paginator = Paginator(reversions, pagination_number)
chirac's avatar
chirac committed
82
83
84
85
86
87
88
89
90
    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)
91
92
    return render(request, 're2o/history.html', {'reversions': reversions, 'object': object_instance})

chirac's avatar
chirac committed
93
@login_required
chirac's avatar
chirac committed
94
@permission_required('cableur')
Dalahro's avatar
Dalahro committed
95
96
97
98
99
100
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/")
101
    port_list = Port.objects.filter(switch = switch).select_related('room').select_related('machine_interface__domain__extension').select_related('related').select_related('switch').order_by('port')
102
    return render(request, 'topologie/index_p.html', {'port_list':port_list, 'id_switch':switch_id, 'nom_switch':switch})
Dalahro's avatar
Dalahro committed
103

chirac's avatar
chirac committed
104
105
106
107
108
109
@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})

110
111
112
113
114
115
116
@login_required
@permission_required('infra')
def index_stack(request):
    stack_list = Stack.objects.order_by('name')
    return render(request, 'topologie/index_stack.html', {'stack_list': stack_list})


chirac's avatar
chirac committed
117
@login_required
118
@permission_required('infra')
Dalahro's avatar
Dalahro committed
119
120
121
122
123
124
125
126
127
128
129
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:
130
131
132
133
            with transaction.atomic(), reversion.create_revision():
                port.save()
                reversion.set_user(request.user)
                reversion.set_comment("Création")
Dalahro's avatar
Dalahro committed
134
135
            messages.success(request, "Port ajouté")
        except IntegrityError:
136
            messages.error(request,"Ce port existe déjà" )
Dalahro's avatar
Dalahro committed
137
        return redirect("/topologie/switch/" + switch_id)
chirac's avatar
chirac committed
138
    return form({'topoform':port}, 'topologie/topo.html', request)
Dalahro's avatar
Dalahro committed
139

chirac's avatar
chirac committed
140
@login_required
141
@permission_required('infra')
Dalahro's avatar
Dalahro committed
142
143
def edit_port(request, port_id):
    try:
144
        port_object = Port.objects.select_related('switch__switch_interface__domain__extension').select_related('machine_interface__domain__extension').select_related('machine_interface__switch').select_related('room').select_related('related').get(pk=port_id)
Dalahro's avatar
Dalahro committed
145
146
147
    except Port.DoesNotExist:
        messages.error(request, u"Port inexistant")
        return redirect("/topologie/")
148
    port = EditPortForm(request.POST or None, instance=port_object)
Dalahro's avatar
Dalahro committed
149
    if port.is_valid():
150
151
152
153
        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
154
        messages.success(request, "Le port a bien été modifié")
155
        return redirect("/topologie/switch/" + str(port_object.switch.id))
chirac's avatar
chirac committed
156
    return form({'topoform':port}, 'topologie/topo.html', request)
chirac's avatar
chirac committed
157

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
@login_required
@permission_required('infra')
def new_stack(request):
    stack = StackForm(request.POST or None)
    #if stack.is_valid():
    if request.POST:
        try:
            with transaction.atomic(), reversion.create_revision():
                stack.save()
                reversion.set_user(request.user)
                reversion.set_comment("Création")
            messages.success(request, "Stack crée")
        except:
            messages.error(request, "Cette stack existe déjà")
    return form({'topoform':stack}, 'topologie/topo.html', request)


@login_required
@permission_required('infra')
def edit_stack(request,stack_id):
    try:
        stack = Stack.objects.get(pk=stack_id)
    except Stack.DoesNotExist:
        messages.error(request, u"Stack inexistante")
guimoz's avatar
guimoz committed
182
        return redirect('/topologie/index_stack/')
183
184
185
186
187
188
189
190
191
    stack = StackForm(request.POST or None, instance=stack)
    if stack.is_valid():
        with transaction.atomic(), reversion.create_revision():
            stack.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in stack.changed_data))
        return redirect('/topologie/index_stack')
    return form({'topoform':stack}, 'topologie/topo.html', request)

guimoz's avatar
guimoz committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
@login_required
@permission_required('infra')
def del_stack(request,stack_id):
    try:
        stack = Stack.objects.get(pk=stack_id)
    except Stack.DoesNotExist:
        messages.error(request, u"Stack inexistante")
        return redirect('/topologie/index_stack')
    if request.method == "POST":
        try:
            with transaction.atomic(), reversion.create_revision():
                stack.delete()
                reversion.set_user(request.user)
                reversion.set_comment("Destruction")
                messages.success(request, "La stack a eté détruite")
        except ProtectedError:
            messages.error(request, "La stack %s est affectée à un autre objet, impossible de la supprimer" % stack)
        return redirect('/topologie/index_stack')
    return form({'objet':stack}, 'topologie/delete.html', request)

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
@login_required
@permission_required('infra')
def edit_switchs_stack(request,stack_id):
    try:
        stack = Stack.objects.get(pk=stack_id)
    except Stack.DoesNotExist:
        messages.error(request, u"Stack inexistante")
        return redirect('/topologie/index_stack')
    if request.method == "POST":
        pass
    else:
        context = {'stack': stack}
        context['switchs_stack'] = stack.switchs_set.all()
        context['switchs_autres'] = Switch.object.filter(stack=None)
        pass


chirac's avatar
chirac committed
229
@login_required
230
@permission_required('infra')
Dalahro's avatar
Dalahro committed
231
def new_switch(request):
232
233
234
    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
235
    domain = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)))
236
    if switch.is_valid() and machine.is_valid() and interface.is_valid():
237
238
239
240
        options, created = AssoOption.objects.get_or_create()
        user = options.utilisateur_asso
        if not user:
            messages.error(request, "L'user association n'existe pas encore, veuillez le créer ou le linker dans preferences")
241
242
243
244
245
            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
246
        new_domain = domain.save(commit=False)
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
        with transaction.atomic(), reversion.create_revision():
            new_machine.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        new_interface.machine = new_machine
        with transaction.atomic(), reversion.create_revision():
            new_interface.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        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")
        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
268
    return form({'topoform':switch, 'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'topologie/switch.html', request)
Dalahro's avatar
Dalahro committed
269

chirac's avatar
chirac committed
270
@login_required
271
@permission_required('infra')
Dalahro's avatar
Dalahro committed
272
273
274
275
276
277
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
278
279
280
    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
281
    domain_form = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)), instance=switch.switch_interface.domain)
chirac's avatar
chirac committed
282
283
284
285
    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
286
        new_domain = domain_form.save(commit=False)
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
        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))
        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))
        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
305
    return form({'topoform':switch_form, 'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'topologie/switch.html', request)
chirac's avatar
chirac committed
306
307
308
309
310
311

@login_required
@permission_required('infra')
def new_room(request):
    room = EditRoomForm(request.POST or None)
    if room.is_valid():
312
313
314
315
        with transaction.atomic(), reversion.create_revision():
            room.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
chirac's avatar
chirac committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
        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():
330
331
332
333
        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
334
335
336
337
338
339
340
341
342
343
344
345
346
        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":
347
348
349
350
351
352
353
354
        try:
            with transaction.atomic(), reversion.create_revision():
                room.delete()
                reversion.set_user(request.user)
                reversion.set_comment("Destruction")
                messages.success(request, "La chambre/prise a été détruite")
        except ProtectedError:
            messages.error(request, "La chambre %s est affectée à un autre objet, impossible de la supprimer (switch ou user)" % room)
chirac's avatar
chirac committed
355
356
        return redirect("/topologie/index_room/")
    return form({'objet': room, 'objet_name': 'Chambre'}, 'topologie/delete.html', request)