views.py 47.4 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.

23
# App de gestion des machines pour re2o
Dalahro's avatar
Dalahro committed
24
# Gabriel Détraz, Augustin Lemesle
25
# Gplv2
26 27 28

from __future__ import unicode_literals

Dalahro's avatar
Dalahro committed
29
from django.http import HttpResponse
30
from django.shortcuts import render, redirect
31 32
from django.shortcuts import get_object_or_404
from django.template.context_processors import csrf
33
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
34 35
from django.template import Context, RequestContext, loader
from django.contrib import messages
36
from django.contrib.auth.decorators import login_required, permission_required
chirac's avatar
chirac committed
37
from django.db.models import ProtectedError
chirac's avatar
chirac committed
38
from django.forms import ValidationError
39
from django.db import transaction
Dalahro's avatar
Dalahro committed
40 41
from django.contrib.auth import authenticate, login
from django.views.decorators.csrf import csrf_exempt
Dalahro's avatar
Dalahro committed
42

Dalahro's avatar
Dalahro committed
43
from rest_framework.renderers import JSONRenderer
Gabriel Detraz's avatar
Gabriel Detraz committed
44
from machines.serializers import InterfaceSerializer, TypeSerializer, DomainSerializer, TextSerializer, MxSerializer, ExtensionSerializer, ServiceServersSerializer, NsSerializer
45
from reversion import revisions as reversion
root's avatar
root committed
46
from reversion.models import Version
Dalahro's avatar
Dalahro committed
47

chirac's avatar
chirac committed
48
import re
49
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm
50
from .forms import EditIpTypeForm, IpTypeForm, DelIpTypeForm, DomainForm, AliasForm, DelAliasForm, NsForm, DelNsForm, TextForm, DelTextForm, MxForm, DelMxForm, VlanForm, DelVlanForm, ServiceForm, DelServiceForm, NasForm, DelNasForm
51
from .forms import EditPortListForm
52
from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Domain, Service, Service_link, Vlan, Nas, Text, PortList
53
from users.models import User
54
from users.models import all_has_access
55
from preferences.models import GeneralOption, OptionalMachine
56

57 58
def all_active_interfaces():
    """Renvoie l'ensemble des machines autorisées à sortir sur internet """
Gabriel Detraz's avatar
Gabriel Detraz committed
59
    return Interface.objects.filter(machine__in=Machine.objects.filter(user__in=all_has_access()).filter(active=True)).select_related('domain').select_related('machine').select_related('type').select_related('ipv4').select_related('domain__extension').select_related('ipv4__ip_type').distinct()
60 61 62 63 64

def all_active_assigned_interfaces():
    """ Renvoie l'ensemble des machines qui ont une ipv4 assignées et disposant de l'accès internet"""
    return all_active_interfaces().filter(ipv4__isnull=False)

65 66 67 68 69 70 71 72
def all_active_interfaces_count():
    """ Version light seulement pour compter"""
    return Interface.objects.filter(machine__in=Machine.objects.filter(user__in=all_has_access()).filter(active=True))

def all_active_assigned_interfaces_count():
    """ Version light seulement pour compter"""
    return all_active_interfaces_count().filter(ipv4__isnull=False)

73 74 75
def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
76
    return render(request, template, c)
77

chirac's avatar
chirac committed
78
@login_required
79 80 81 82 83 84
def new_machine(request, userid):
    try:
        user = User.objects.get(pk=userid)
    except User.DoesNotExist:
        messages.error(request, u"Utilisateur inexistant" )
        return redirect("/machines/")
85 86
    options, created = OptionalMachine.objects.get_or_create()
    max_lambdauser_interfaces = options.max_lambdauser_interfaces
87 88 89 90
    if not request.user.has_perms(('cableur',)):
        if user != request.user:
            messages.error(request, "Vous ne pouvez pas ajouter une machine à un autre user que vous sans droit")
            return redirect("/users/profil/" + str(request.user.id))
91 92
        if user.user_interfaces().count() >= max_lambdauser_interfaces:
            messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_interfaces)
93
            return redirect("/users/profil/" + str(request.user.id))
94
    machine = NewMachineForm(request.POST or None)
95
    interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) 
Dalahro's avatar
Dalahro committed
96
    nb_machine = Interface.objects.filter(machine__user=userid).count()
guimoz's avatar
Typo  
guimoz committed
97
    domain = DomainForm(request.POST or None, name_user=user.pseudo.replace('_','-'), nb_machine=nb_machine)
98 99 100 101 102 103
    if machine.is_valid() and interface.is_valid():
        new_machine = machine.save(commit=False)
        new_machine.user = user
        new_interface = interface.save(commit=False)
        domain.instance.interface_parent = new_interface
        if domain.is_valid():
104
            new_domain = domain.save(commit=False)
105 106 107 108 109 110 111 112 113 114 115 116 117 118
            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")
guimoz's avatar
Typo  
guimoz committed
119
            messages.success(request, "La machine a été créée")
120
            return redirect("/users/profil/" + str(user.id))
chirac's avatar
chirac committed
121
    return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'machines/machine.html', request)
122

chirac's avatar
chirac committed
123
@login_required
124
def edit_interface(request, interfaceid):
125 126 127 128 129
    try:
        interface = Interface.objects.get(pk=interfaceid)
    except Interface.DoesNotExist:
        messages.error(request, u"Interface inexistante" )
        return redirect("/machines")
130
    if not request.user.has_perms(('infra',)):
131
        if not request.user.has_perms(('cableur',)) and interface.machine.user != request.user:
132 133 134
            messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit")
            return redirect("/users/profil/" + str(request.user.id))
        machine_form = BaseEditMachineForm(request.POST or None, instance=interface.machine)
135
        interface_form = BaseEditInterfaceForm(request.POST or None, instance=interface, infra=False)
136 137
    else:
        machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
chirac's avatar
chirac committed
138
        interface_form = EditInterfaceForm(request.POST or None, instance=interface)
139
    domain_form = DomainForm(request.POST or None, instance=interface.domain)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    if machine_form.is_valid() and interface_form.is_valid() and domain_form.is_valid():
        new_machine = machine_form.save(commit=False)
        new_interface = interface_form.save(commit=False)
        new_domain = domain_form.save(commit=False)
        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))
        messages.success(request, "La machine a été modifiée")
        return redirect("/users/profil/" + str(interface.machine.user.id))
chirac's avatar
chirac committed
158
    return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request)
159

160 161 162 163 164 165 166 167 168 169
@login_required
def del_machine(request, machineid):
    try:
        machine = Machine.objects.get(pk=machineid)
    except Machine.DoesNotExist:
        messages.error(request, u"Machine inexistante" )
        return redirect("/machines")
    if not request.user.has_perms(('cableur',)):
        if machine.user != request.user:
            messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit")
170
            return redirect("/users/profil/" + str(machine.user.id))
171
    if request.method == "POST":
172 173 174
        with transaction.atomic(), reversion.create_revision():
            machine.delete()
            reversion.set_user(request.user)
175
        messages.success(request, "La machine a été détruite")
176
        return redirect("/users/profil/" + str(machine.user.id))
177
    return form({'objet': machine, 'objet_name': 'machine'}, 'machines/delete.html', request)
178

chirac's avatar
chirac committed
179
@login_required
180 181 182 183 184 185
def new_interface(request, machineid):
    try:
        machine = Machine.objects.get(pk=machineid)
    except Machine.DoesNotExist:
        messages.error(request, u"Machine inexistante" )
        return redirect("/machines")
186
    if not request.user.has_perms(('cableur',)):
187 188
        options, created = OptionalMachine.objects.get_or_create()
        max_lambdauser_interfaces = options.max_lambdauser_interfaces
189
        if machine.user != request.user:
190 191
            messages.error(request, "Vous ne pouvez pas ajouter une interface à une machine d'un autre user que vous sans droit")
            return redirect("/users/profil/" + str(request.user.id))
192 193
        if machine.user.user_interfaces().count() >= max_lambdauser_interfaces:
            messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_interfaces)
194
            return redirect("/users/profil/" + str(request.user.id))
195
    interface_form = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',)))
196
    domain_form = DomainForm(request.POST or None)
197 198 199 200 201
    if interface_form.is_valid():
        new_interface = interface_form.save(commit=False)
        domain_form.instance.interface_parent = new_interface
        new_interface.machine = machine
        if domain_form.is_valid():
202
            new_domain = domain_form.save(commit=False)
203 204 205 206 207 208 209 210 211 212 213
            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")
            messages.success(request, "L'interface a été ajoutée")
            return redirect("/users/profil/" + str(machine.user.id))
chirac's avatar
chirac committed
214
    return form({'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request)
215

216 217 218 219 220 221 222 223 224 225 226
@login_required
def del_interface(request, interfaceid):
    try:
        interface = Interface.objects.get(pk=interfaceid)
    except Interface.DoesNotExist:
        messages.error(request, u"Interface inexistante" )
        return redirect("/machines")
    if not request.user.has_perms(('cableur',)):
        if interface.machine.user != request.user:
            messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit")
            return redirect("/users/profil/" + str(request.user.id))
227
    if request.method == "POST":
228
        machine = interface.machine
229 230
        with transaction.atomic(), reversion.create_revision():
            interface.delete()
231 232
            if not machine.interface_set.all():
               machine.delete()
233
            reversion.set_user(request.user)
234 235 236
        messages.success(request, "L'interface a été détruite")
        return redirect("/users/profil/" + str(request.user.id))
    return form({'objet': interface, 'objet_name': 'interface'}, 'machines/delete.html', request)
237

chirac's avatar
chirac committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
@login_required
@permission_required('infra')
def add_iptype(request):
    iptype = IpTypeForm(request.POST or None)
    if iptype.is_valid():
        with transaction.atomic(), reversion.create_revision():
            iptype.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        messages.success(request, "Ce type d'ip a été ajouté")
        return redirect("/machines/index_iptype")
    return form({'machineform': iptype, 'interfaceform': None}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def edit_iptype(request, iptypeid):
    try:
        iptype_instance = IpType.objects.get(pk=iptypeid)
    except IpType.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_iptype/")
259
    iptype = EditIpTypeForm(request.POST or None, instance=iptype_instance)
chirac's avatar
chirac committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    if iptype.is_valid():
        with transaction.atomic(), reversion.create_revision():
            iptype.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in iptype.changed_data))
        messages.success(request, "Type d'ip modifié")
        return redirect("/machines/index_iptype/")
    return form({'machineform': iptype}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def del_iptype(request):
    iptype = DelIpTypeForm(request.POST or None)
    if iptype.is_valid():
        iptype_dels = iptype.cleaned_data['iptypes']
        for iptype_del in iptype_dels:
            try:
                with transaction.atomic(), reversion.create_revision():
                    iptype_del.delete()
                    reversion.set_user(request.user)
                messages.success(request, "Le type d'ip a été supprimé")
            except ProtectedError:
                messages.error(request, "Le type d'ip %s est affectée à au moins une machine, vous ne pouvez pas le supprimer" % iptype_del)
        return redirect("/machines/index_iptype")
    return form({'machineform': iptype, 'interfaceform': None}, 'machines/machine.html', request)

chirac's avatar
chirac committed
286
@login_required
287
@permission_required('infra')
chirac's avatar
chirac committed
288 289 290
def add_machinetype(request):
    machinetype = MachineTypeForm(request.POST or None)
    if machinetype.is_valid():
291 292 293 294
        with transaction.atomic(), reversion.create_revision():
            machinetype.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
chirac's avatar
chirac committed
295
        messages.success(request, "Ce type de machine a été ajouté")
296
        return redirect("/machines/index_machinetype")
chirac's avatar
chirac committed
297 298
    return form({'machineform': machinetype, 'interfaceform': None}, 'machines/machine.html', request)

chirac's avatar
chirac committed
299
@login_required
300
@permission_required('infra')
301 302 303 304 305 306 307 308
def edit_machinetype(request, machinetypeid):
    try:
        machinetype_instance = MachineType.objects.get(pk=machinetypeid)
    except MachineType.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_machinetype/")
    machinetype = MachineTypeForm(request.POST or None, instance=machinetype_instance)
    if machinetype.is_valid():
309 310 311 312
        with transaction.atomic(), reversion.create_revision():
            machinetype.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in machinetype.changed_data))
313 314 315 316
        messages.success(request, "Type de machine modifié")
        return redirect("/machines/index_machinetype/")
    return form({'machineform': machinetype}, 'machines/machine.html', request)

chirac's avatar
chirac committed
317
@login_required
318
@permission_required('infra')
chirac's avatar
chirac committed
319 320 321 322 323 324
def del_machinetype(request):
    machinetype = DelMachineTypeForm(request.POST or None)
    if machinetype.is_valid():
        machinetype_dels = machinetype.cleaned_data['machinetypes']
        for machinetype_del in machinetype_dels:
            try:
325 326 327
                with transaction.atomic(), reversion.create_revision():
                    machinetype_del.delete()
                    reversion.set_user(request.user)
chirac's avatar
chirac committed
328 329 330
                messages.success(request, "Le type de machine a été supprimé")
            except ProtectedError:
                messages.error(request, "Le type de machine %s est affectée à au moins une machine, vous ne pouvez pas le supprimer" % machinetype_del)
331
        return redirect("/machines/index_machinetype")
chirac's avatar
chirac committed
332 333
    return form({'machineform': machinetype, 'interfaceform': None}, 'machines/machine.html', request)

334
@login_required
335
@permission_required('infra')
336 337 338
def add_extension(request):
    extension = ExtensionForm(request.POST or None)
    if extension.is_valid():
339 340 341 342
        with transaction.atomic(), reversion.create_revision():
            extension.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
343 344 345 346 347
        messages.success(request, "Cette extension a été ajoutée")
        return redirect("/machines/index_extension")
    return form({'machineform': extension, 'interfaceform': None}, 'machines/machine.html', request)

@login_required
348
@permission_required('infra')
349 350 351 352 353 354 355 356
def edit_extension(request, extensionid):
    try:
        extension_instance = Extension.objects.get(pk=extensionid)
    except Extension.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_extension/")
    extension = ExtensionForm(request.POST or None, instance=extension_instance)
    if extension.is_valid():
357 358 359 360
        with transaction.atomic(), reversion.create_revision():
            extension.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in extension.changed_data))
361 362 363 364 365
        messages.success(request, "Extension modifiée")
        return redirect("/machines/index_extension/")
    return form({'machineform': extension}, 'machines/machine.html', request)

@login_required
366
@permission_required('infra')
367 368 369 370 371 372
def del_extension(request):
    extension = DelExtensionForm(request.POST or None)
    if extension.is_valid():
        extension_dels = extension.cleaned_data['extensions']
        for extension_del in extension_dels:
            try:
373 374 375
                with transaction.atomic(), reversion.create_revision():
                    extension_del.delete()
                    reversion.set_user(request.user)
376 377 378 379 380 381
                messages.success(request, "L'extension a été supprimée")
            except ProtectedError:
                messages.error(request, "L'extension %s est affectée à au moins un type de machine, vous ne pouvez pas la supprimer" % extension_del)
        return redirect("/machines/index_extension")
    return form({'machineform': extension, 'interfaceform': None}, 'machines/machine.html', request)

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
@login_required
@permission_required('infra')
def add_mx(request):
    mx = MxForm(request.POST or None)
    if mx.is_valid():
        with transaction.atomic(), reversion.create_revision():
            mx.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        messages.success(request, "Cet enregistrement mx a été ajouté")
        return redirect("/machines/index_extension")
    return form({'machineform': mx, 'interfaceform': None}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def edit_mx(request, mxid):
    try:
        mx_instance = Mx.objects.get(pk=mxid)
    except Mx.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_extension/")
    mx = MxForm(request.POST or None, instance=mx_instance)
    if mx.is_valid():
        with transaction.atomic(), reversion.create_revision():
            mx.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in mx.changed_data))
        messages.success(request, "Mx modifié")
        return redirect("/machines/index_extension/")
    return form({'machineform': mx}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def del_mx(request):
    mx = DelMxForm(request.POST or None)
    if mx.is_valid():
        mx_dels = mx.cleaned_data['mx']
        for mx_del in mx_dels:
            try:
                with transaction.atomic(), reversion.create_revision():
                    mx_del.delete()
                    reversion.set_user(request.user)
                messages.success(request, "L'mx a été supprimée")
            except ProtectedError:
                messages.error(request, "Erreur le Mx suivant %s ne peut être supprimé" % mx_del)
        return redirect("/machines/index_extension")
    return form({'machineform': mx, 'interfaceform': None}, 'machines/machine.html', request)

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
@login_required
@permission_required('infra')
def add_ns(request):
    ns = NsForm(request.POST or None)
    if ns.is_valid():
        with transaction.atomic(), reversion.create_revision():
            ns.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        messages.success(request, "Cet enregistrement ns a été ajouté")
        return redirect("/machines/index_extension")
    return form({'machineform': ns, 'interfaceform': None}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def edit_ns(request, nsid):
    try:
        ns_instance = Ns.objects.get(pk=nsid)
    except Ns.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_extension/")
    ns = NsForm(request.POST or None, instance=ns_instance)
    if ns.is_valid():
        with transaction.atomic(), reversion.create_revision():
            ns.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in ns.changed_data))
        messages.success(request, "Ns modifié")
        return redirect("/machines/index_extension/")
    return form({'machineform': ns}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def del_ns(request):
    ns = DelNsForm(request.POST or None)
    if ns.is_valid():
        ns_dels = ns.cleaned_data['ns']
        for ns_del in ns_dels:
            try:
                with transaction.atomic(), reversion.create_revision():
                    ns_del.delete()
                    reversion.set_user(request.user)
                messages.success(request, "Le ns a été supprimée")
            except ProtectedError:
                messages.error(request, "Erreur le Ns suivant %s ne peut être supprimé" % ns_del)
        return redirect("/machines/index_extension")
    return form({'machineform': ns, 'interfaceform': None}, 'machines/machine.html', request)

Gabriel Detraz's avatar
Gabriel Detraz committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
@login_required
@permission_required('infra')
def add_text(request):
    text = TextForm(request.POST or None)
    if text.is_valid():
        with transaction.atomic(), reversion.create_revision():
            text.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        messages.success(request, "Cet enregistrement text a été ajouté")
        return redirect("/machines/index_extension")
    return form({'machineform': text, 'interfaceform': None}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def edit_text(request, textid):
    try:
        text_instance = Text.objects.get(pk=textid)
    except Text.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_extension/")
    text = TextForm(request.POST or None, instance=text_instance)
    if text.is_valid():
        with transaction.atomic(), reversion.create_revision():
            text.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in text.changed_data))
        messages.success(request, "Text modifié")
        return redirect("/machines/index_extension/")
    return form({'machineform': text}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def del_text(request):
    text = DelTextForm(request.POST or None)
    if text.is_valid():
        text_dels = text.cleaned_data['text']
        for text_del in text_dels:
            try:
                with transaction.atomic(), reversion.create_revision():
                    text_del.delete()
                    reversion.set_user(request.user)
                messages.success(request, "Le text a été supprimé")
            except ProtectedError:
                messages.error(request, "Erreur le Text suivant %s ne peut être supprimé" % text_del)
        return redirect("/machines/index_extension")
    return form({'machineform': text, 'interfaceform': None}, 'machines/machine.html', request)

526 527 528 529 530 531 532
@login_required
def add_alias(request, interfaceid):
    try:
        interface = Interface.objects.get(pk=interfaceid)
    except Interface.DoesNotExist:
        messages.error(request, u"Interface inexistante" )
        return redirect("/machines")
533
    if not request.user.has_perms(('cableur',)):
534 535
        options, created = OptionalMachine.objects.get_or_create()
        max_lambdauser_aliases = options.max_lambdauser_aliases
536 537 538
        if interface.machine.user != request.user:
            messages.error(request, "Vous ne pouvez pas ajouter un alias à une machine d'un autre user que vous sans droit")
            return redirect("/users/profil/" + str(request.user.id))
539 540
        if Domain.objects.filter(cname__in=Domain.objects.filter(interface_parent__in=interface.machine.user.user_interfaces())).count() >= max_lambdauser_aliases:
            messages.error(request, "Vous avez atteint le maximum d'alias autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_aliases)
541
            return redirect("/users/profil/" + str(request.user.id))
542
    alias = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)))
543 544
    if alias.is_valid():
        alias = alias.save(commit=False)
chirac's avatar
chirac committed
545
        alias.cname = interface.domain
546 547 548 549 550 551 552 553 554 555 556
        with transaction.atomic(), reversion.create_revision():
            alias.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        messages.success(request, "Cet alias a été ajouté")
        return redirect("/machines/index_alias/" + str(interfaceid))
    return form({'machineform': alias, 'interfaceform': None}, 'machines/machine.html', request)

@login_required
def edit_alias(request, aliasid):
    try:
chirac's avatar
chirac committed
557 558
        alias_instance = Domain.objects.get(pk=aliasid)
    except Domain.DoesNotExist:
559 560
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_extension/")
chirac's avatar
chirac committed
561
    if not request.user.has_perms(('cableur',)) and alias_instance.cname.interface_parent.machine.user != request.user:
562 563
        messages.error(request, "Vous ne pouvez pas ajouter un alias à une machine d'un autre user que vous sans droit")
        return redirect("/users/profil/" + str(request.user.id))
564
    alias = AliasForm(request.POST or None, instance=alias_instance, infra=request.user.has_perms(('infra',)))
565 566 567 568 569 570
    if alias.is_valid():
        with transaction.atomic(), reversion.create_revision():
            alias_instance = alias.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in alias.changed_data))
        messages.success(request, "Alias modifié")
chirac's avatar
chirac committed
571
        return redirect("/machines/index_alias/" + str(alias_instance.cname.interface_parent.id))
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    return form({'machineform': alias}, 'machines/machine.html', request)

@login_required
def del_alias(request, interfaceid):
    try:
        interface = Interface.objects.get(pk=interfaceid)
    except Interface.DoesNotExist:
        messages.error(request, u"Interface inexistante" )
        return redirect("/machines")
    if not request.user.has_perms(('cableur',)) and interface.machine.user != request.user:
        messages.error(request, "Vous ne pouvez pas ajouter un alias à une machine d'un autre user que vous sans droit")
        return redirect("/users/profil/" + str(request.user.id))
    alias = DelAliasForm(request.POST or None, interface=interface)
    if alias.is_valid():
        alias_dels = alias.cleaned_data['alias']
        for alias_del in alias_dels:
            try:
                with transaction.atomic(), reversion.create_revision():
                    alias_del.delete()
                    reversion.set_user(request.user)
                messages.success(request, "L'alias %s a été supprimé" % alias_del)
            except ProtectedError:
                messages.error(request, "Erreur l'alias suivant %s ne peut être supprimé" % alias_del)
        return redirect("/machines/index_alias/" + str(interfaceid))
    return form({'machineform': alias, 'interfaceform': None}, 'machines/machine.html', request)

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

@login_required
@permission_required('infra')
def add_service(request):
    service = ServiceForm(request.POST or None)
    if service.is_valid():
        with transaction.atomic(), reversion.create_revision():
            service.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        messages.success(request, "Cet enregistrement service a été ajouté")
        return redirect("/machines/index_service")
    return form({'machineform': service}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def edit_service(request, serviceid):
    try:
        service_instance = Service.objects.get(pk=serviceid)
    except Ns.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_extension/")
    service = ServiceForm(request.POST or None, instance=service_instance)
    if service.is_valid():
        with transaction.atomic(), reversion.create_revision():
            service.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in service.changed_data))
        messages.success(request, "Service modifié")
        return redirect("/machines/index_service/")
    return form({'machineform': service}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def del_service(request):
    service = DelServiceForm(request.POST or None)
    if service.is_valid():
        service_dels = service.cleaned_data['service']
        for service_del in service_dels:
            try:
                with transaction.atomic(), reversion.create_revision():
                    service_del.delete()
                    reversion.set_user(request.user)
                messages.success(request, "Le service a été supprimée")
            except ProtectedError:
                messages.error(request, "Erreur le service suivant %s ne peut être supprimé" % service_del)
        return redirect("/machines/index_service")
    return form({'machineform': service}, 'machines/machine.html', request)

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
@login_required
@permission_required('infra')
def add_vlan(request):
    vlan = VlanForm(request.POST or None)
    if vlan.is_valid():
        with transaction.atomic(), reversion.create_revision():
            vlan.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        messages.success(request, "Cet enregistrement vlan a été ajouté")
        return redirect("/machines/index_vlan")
    return form({'machineform': vlan, 'interfaceform': None}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def edit_vlan(request, vlanid):
    try:
        vlan_instance = Vlan.objects.get(pk=vlanid)
    except Vlan.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_vlan/")
    vlan = VlanForm(request.POST or None, instance=vlan_instance)
    if vlan.is_valid():
        with transaction.atomic(), reversion.create_revision():
            vlan.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in vlan.changed_data))
        messages.success(request, "Vlan modifié")
        return redirect("/machines/index_vlan/")
    return form({'machineform': vlan}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def del_vlan(request):
    vlan = DelVlanForm(request.POST or None)
    if vlan.is_valid():
        vlan_dels = vlan.cleaned_data['vlan']
684
        for vlan_del in vlan_dels:
685 686 687 688 689 690 691 692 693 694
            try:
                with transaction.atomic(), reversion.create_revision():
                    vlan_del.delete()
                    reversion.set_user(request.user)
                messages.success(request, "Le vlan a été supprimée")
            except ProtectedError:
                messages.error(request, "Erreur le Vlan suivant %s ne peut être supprimé" % vlan_del)
        return redirect("/machines/index_vlan")
    return form({'machineform': vlan, 'interfaceform': None}, 'machines/machine.html', request)

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
@login_required
@permission_required('infra')
def add_nas(request):
    nas = NasForm(request.POST or None)
    if nas.is_valid():
        with transaction.atomic(), reversion.create_revision():
            nas.save()
            reversion.set_user(request.user)
            reversion.set_comment("Création")
        messages.success(request, "Cet enregistrement nas a été ajouté")
        return redirect("/machines/index_nas")
    return form({'machineform': nas, 'interfaceform': None}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def edit_nas(request, nasid):
    try:
        nas_instance = Nas.objects.get(pk=nasid)
    except Nas.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/machines/index_nas/")
    nas = NasForm(request.POST or None, instance=nas_instance)
    if nas.is_valid():
        with transaction.atomic(), reversion.create_revision():
            nas.save()
            reversion.set_user(request.user)
            reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in nas.changed_data))
        messages.success(request, "Nas modifié")
        return redirect("/machines/index_nas/")
    return form({'machineform': nas}, 'machines/machine.html', request)

@login_required
@permission_required('infra')
def del_nas(request):
    nas = DelNasForm(request.POST or None)
    if nas.is_valid():
        nas_dels = nas.cleaned_data['nas']
        for nas_del in nas_dels:
            try:
                with transaction.atomic(), reversion.create_revision():
                    nas_del.delete()
                    reversion.set_user(request.user)
                messages.success(request, "Le nas a été supprimé")
            except ProtectedError:
                messages.error(request, "Erreur le Nas suivant %s ne peut être supprimé" % nas_del)
        return redirect("/machines/index_nas")
    return form({'machineform': nas, 'interfaceform': None}, 'machines/machine.html', request)

chirac's avatar
chirac committed
743
@login_required
chirac's avatar
chirac committed
744
@permission_required('cableur')
745
def index(request):
746 747
    options, created = GeneralOption.objects.get_or_create()
    pagination_large_number = options.pagination_large_number
748
    machines_list = Machine.objects.select_related('user').prefetch_related('interface_set__domain__extension').prefetch_related('interface_set__ipv4__ip_type').prefetch_related('interface_set__type__ip_type__extension').prefetch_related('interface_set__domain__related_domain__extension').order_by('pk')
749
    paginator = Paginator(machines_list, pagination_large_number)
750 751 752 753 754 755 756 757 758
    page = request.GET.get('page')
    try:
        machines_list = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        machines_list = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        machines_list = paginator.page(paginator.num_pages)
759
    return render(request, 'machines/index.html', {'machines_list': machines_list})
760

chirac's avatar
chirac committed
761 762 763
@login_required
@permission_required('cableur')
def index_iptype(request):
Gabriel Detraz's avatar
Gabriel Detraz committed
764
    iptype_list = IpType.objects.select_related('extension').order_by('type')
chirac's avatar
chirac committed
765 766
    return render(request, 'machines/index_iptype.html', {'iptype_list':iptype_list})

767 768 769 770 771 772
@login_required
@permission_required('cableur')
def index_vlan(request):
    vlan_list = Vlan.objects.order_by('vlan_id')
    return render(request, 'machines/index_vlan.html', {'vlan_list':vlan_list})

chirac's avatar
chirac committed
773
@login_required
chirac's avatar
chirac committed
774
@permission_required('cableur')
775
def index_machinetype(request):
Gabriel Detraz's avatar
Gabriel Detraz committed
776
    machinetype_list = MachineType.objects.select_related('ip_type').order_by('type')
777
    return render(request, 'machines/index_machinetype.html', {'machinetype_list':machinetype_list})
778

779 780 781 782 783 784
@login_required
@permission_required('cableur')
def index_nas(request):
    nas_list = Nas.objects.select_related('machine_type').order_by('name')
    return render(request, 'machines/index_nas.html', {'nas_list':nas_list})

785
@login_required
chirac's avatar
chirac committed
786
@permission_required('cableur')
787
def index_extension(request):
788 789 790
    extension_list = Extension.objects.select_related('origin').order_by('name')
    mx_list = Mx.objects.order_by('zone').select_related('zone').select_related('name__extension')
    ns_list = Ns.objects.order_by('zone').select_related('zone').select_related('ns__extension')
Gabriel Detraz's avatar
Gabriel Detraz committed
791 792
    text_list = Text.objects.all().select_related('zone')
    return render(request, 'machines/index_extension.html', {'extension_list':extension_list, 'mx_list': mx_list, 'ns_list': ns_list, 'text_list' : text_list})
Dalahro's avatar
Dalahro committed
793

794 795 796 797 798 799 800 801 802 803
@login_required
def index_alias(request, interfaceid):
    try:
        interface = Interface.objects.get(pk=interfaceid)
    except Interface.DoesNotExist:
        messages.error(request, u"Interface inexistante" )
        return redirect("/machines")
    if not request.user.has_perms(('cableur',)) and interface.machine.user != request.user:
        messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit")
        return redirect("/users/profil/" + str(request.user.id))
chirac's avatar
chirac committed
804
    alias_list = Domain.objects.filter(cname=Domain.objects.filter(interface_parent=interface)).order_by('name')
805 806
    return render(request, 'machines/index_alias.html', {'alias_list':alias_list, 'interface_id': interfaceid})

807 808 809 810 811 812 813
@login_required
@permission_required('cableur')
def index_service(request):
    service_list = Service.objects.all()
    servers_list = Service_link.objects.all()
    return render(request, 'machines/index_service.html', {'service_list':service_list, 'servers_list':servers_list})

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
@login_required
def history(request, object, id):
    if object == 'machine':
        try:
             object_instance = Machine.objects.get(pk=id)
        except Machine.DoesNotExist:
             messages.error(request, "Machine inexistante")
             return redirect("/machines/")
        if not request.user.has_perms(('cableur',)) and object_instance.user != request.user:
             messages.error(request, "Vous ne pouvez pas afficher l'historique d'une machine d'un autre user que vous sans droit cableur")
             return redirect("/users/profil/" + str(request.user.id))
    elif object == 'interface':
        try:
             object_instance = Interface.objects.get(pk=id)
        except Interface.DoesNotExist:
             messages.error(request, "Interface inexistante")
             return redirect("/machines/")
        if not request.user.has_perms(('cableur',)) and object_instance.machine.user != request.user:
             messages.error(request, "Vous ne pouvez pas afficher l'historique d'une interface d'un autre user que vous sans droit cableur")
             return redirect("/users/profil/" + str(request.user.id))
834 835
    elif object == 'alias':
        try:
chirac's avatar
chirac committed
836 837
             object_instance = Domain.objects.get(pk=id)
        except Domain.DoesNotExist:
838 839
             messages.error(request, "Alias inexistant")
             return redirect("/machines/")
chirac's avatar
chirac committed
840
        if not request.user.has_perms(('cableur',)) and object_instance.cname.interface_parent.machine.user != request.user:
841 842
             messages.error(request, "Vous ne pouvez pas afficher l'historique d'un alias d'un autre user que vous sans droit cableur")
             return redirect("/users/profil/" + str(request.user.id))
843 844 845 846 847 848
    elif object == 'machinetype' and request.user.has_perms(('cableur',)):
        try:
             object_instance = MachineType.objects.get(pk=id)
        except MachineType.DoesNotExist:
             messages.error(request, "Type de machine inexistant")
             return redirect("/machines/")
chirac's avatar
chirac committed
849 850 851 852 853 854
    elif object == 'iptype' and request.user.has_perms(('cableur',)):
        try:
             object_instance = IpType.objects.get(pk=id)
        except IpType.DoesNotExist:
             messages.error(request, "Type d'ip inexistant")
             return redirect("/machines/")
855 856 857 858 859 860
    elif object == 'extension' and request.user.has_perms(('cableur',)):
        try:
             object_instance = Extension.objects.get(pk=id)
        except Extension.DoesNotExist:
             messages.error(request, "Extension inexistante")
             return redirect("/machines/")
861 862 863 864 865 866
    elif object == 'mx' and request.user.has_perms(('cableur',)):
        try:
             object_instance = Mx.objects.get(pk=id)
        except Mx.DoesNotExist:
             messages.error(request, "Mx inexistant")
             return redirect("/machines/")
Gabriel Detraz's avatar
Gabriel Detraz committed
867 868 869 870 871 872
    elif object == 'text' and request.user.has_perms(('cableur',)):
        try:
             object_instance = Text.objects.get(pk=id)
        except Text.DoesNotExist:
             messages.error(request, "Text inexistant")
             return redirect("/machines/")   
873 874 875 876 877 878
    elif object == 'ns' and request.user.has_perms(('cableur',)):
        try:
             object_instance = Ns.objects.get(pk=id)
        except Ns.DoesNotExist:
             messages.error(request, "Ns inexistant")
             return redirect("/machines/")
879 880 881 882 883 884
    elif object == 'service' and request.user.has_perms(('cableur',)):
        try:
             object_instance = Service.objects.get(pk=id)
        except Service.DoesNotExist:
             messages.error(request, "Service inexistant")
             return redirect("/machines/")
885 886 887 888 889 890
    elif object == 'vlan' and request.user.has_perms(('cableur',)):
        try:
             object_instance = Vlan.objects.get(pk=id)
        except Vlan.DoesNotExist:
             messages.error(request, "Vlan inexistant")
             return redirect("/machines/")
891 892 893 894 895 896
    elif object == 'nas' and request.user.has_perms(('cableur',)):
        try:
             object_instance = Nas.objects.get(pk=id)
        except Nas.DoesNotExist:
             messages.error(request, "Nas inexistant")
             return redirect("/machines/")
897 898 899
    else:
        messages.error(request, "Objet  inconnu")
        return redirect("/machines/")
900 901
    options, created = GeneralOption.objects.get_or_create()
    pagination_number = options.pagination_number
root's avatar
root committed
902
    reversions = Version.objects.get_for_object(object_instance)
903
    paginator = Paginator(reversions, pagination_number)
chirac's avatar
chirac committed
904 905 906 907 908 909 910 911 912
    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)
913 914 915
    return render(request, 're2o/history.html', {'reversions': reversions, 'object': object_instance})


916
@login_required
917
@permission_required('cableur')
918 919 920 921
def index_portlist(request):
    port_list = PortList.objects.all().order_by('name') 
    return render(request, "machines/index_portlist.html", {'port_list':port_list})

922 923 924 925 926 927 928 929 930
@login_required
@permission_required('bureau')
def edit_portlist(request, pk):
    port_list_instance = get_object_or_404(PortList, pk=pk)
    port_list = EditPortListForm(request.POST or None, instance=port_list_instance)
    if port_list.is_valid():
        return redirect("/machines/index_portlist/")
    return form({'machineform' : port_list}, 'machines/machine.html', request)

Dalahro's avatar
Dalahro committed
931 932 933 934
""" Framework Rest """

class JSONResponse(HttpResponse):
    def __init__(self, data, **kwargs):
Dalahro's avatar
Dalahro committed
935
        content = JSONRenderer().render(data)
Dalahro's avatar
Dalahro committed
936 937 938
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

Dalahro's avatar
Dalahro committed
939 940 941
@csrf_exempt
@login_required
@permission_required('serveur')
Gabriel Detraz's avatar
Gabriel Detraz committed
942
def mac_ip_list(request):
943
    interfaces = all_active_assigned_interfaces()
Gabriel Detraz's avatar
Gabriel Detraz committed
944
    seria = InterfaceSerializer(interfaces, many=True)
Dalahro's avatar
Dalahro committed
945
    return seria.data
Dalahro's avatar
Dalahro committed
946

Dalahro's avatar
Dalahro committed
947 948 949
@csrf_exempt
@login_required
@permission_required('serveur')
950
def alias(request):
Gabriel Detraz's avatar
Gabriel Detraz committed
951
    alias = Domain.objects.filter(interface_parent=None).filter(cname__in=Domain.objects.filter(interface_parent__in=Interface.objects.exclude(ipv4=None))).select_related('extension').select_related('cname__extension')
chirac's avatar
chirac committed
952
    seria = DomainSerializer(alias, many=True)
953 954 955 956 957 958
    return JSONResponse(seria.data)

@csrf_exempt
@login_required
@permission_required('serveur')
def corresp(request):
959
    type = IpType.objects.all().select_related('extension')
Dalahro's avatar
Dalahro committed
960
    seria = TypeSerializer(type, many=True)
961
    return JSONResponse(seria.data)
Dalahro's avatar
Dalahro committed
962

963 964 965 966
@csrf_exempt
@login_required
@permission_required('serveur')
def mx(request):
967
    mx = Mx.objects.all().select_related('zone').select_related('name__extension')
968 969 970
    seria = MxSerializer(mx, many=True)
    return JSONResponse(seria.data)

Gabriel Detraz's avatar
Gabriel Detraz committed
971 972 973 974 975 976 977 978
@csrf_exempt
@login_required
@permission_required('serveur')
def text(request):
    text = Text.objects.all().select_related('zone')
    seria = TextSerializer(text, many=True)
    return JSONResponse(seria.data)

979 980 981 982
@csrf_exempt
@login_required
@permission_required('serveur')
def ns(request):
983
    ns = Ns.objects.exclude(ns__in=Domain.objects.filter(interface_parent__in=Interface.objects.filter(ipv4=None))).select_related('zone').select_related('ns__extension')
984 985 986
    seria = NsSerializer(ns, many=True)
    return JSONResponse(seria.data)

987 988 989 990
@csrf_exempt
@login_required
@permission_required('serveur')
def zones(request):
991
    zones = Extension.objects.all().select_related('origin')
992 993 994
    seria = ExtensionSerializer(zones, many=True)
    return JSONResponse(seria.data)

Dalahro's avatar
Dalahro committed
995 996 997
@csrf_exempt
@login_required
@permission_required('serveur')
Dalahro's avatar
Dalahro committed
998
def mac_ip(request):
Gabriel Detraz's avatar
Gabriel Detraz committed
999
    seria = mac_ip_list(request)
Dalahro's avatar
Dalahro committed
1000
    return JSONResponse(seria)
Dalahro's avatar
Dalahro committed
1001

Dalahro's avatar
Dalahro committed
1002 1003 1004
@csrf_exempt
@login_required
@permission_required('serveur')
Dalahro's avatar
Dalahro committed
1005
def mac_ip_dns(request):
Gabriel Detraz's avatar
Gabriel Detraz committed
1006
    seria = mac_ip_list(request)
Dalahro's avatar
Dalahro committed
1007
    return JSONResponse(seria)
Dalahro's avatar
Dalahro committed
1008

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
@csrf_exempt
@login_required
@permission_required('serveur')
def service_servers(request):
    service_link = Service_link.objects.all().select_related('server__domain').select_related('service')
    seria = ServiceServersSerializer(service_link, many=True)
    return JSONResponse(seria.data)

@csrf_exempt
@login_required
@permission_required('serveur')
def regen_achieved(request):
1021
    obj = Service_link.objects.filter(service__in=Service.objects.filter(service_type=request.POST['service']), server__in=Interface.objects.filter(domain__in=Domain.objects.filter(name=request.POST['server'])))
1022
    if obj:
1023
        obj.first().done_regen()
Gabriel Detraz's avatar
iTypo  
Gabriel Detraz committed
1024
    return HttpResponse("Ok")
1025