views.py 4.1 KB
Newer Older
1 2 3 4 5 6 7 8
# App de gestion des machines pour re2o
# Gabriel Détraz
# Gplv2
from django.shortcuts import render, redirect
from django.shortcuts import render_to_response, get_object_or_404
from django.core.context_processors import csrf
from django.template import Context, RequestContext, loader
from django.contrib import messages
9

10
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm
11
from .models import Machine, Interface, IpList
12 13
from users.models import User

14 15 16
def unassign_ips(user):
    machines = Interface.objects.filter(machine=Machine.objects.filter(user=user))
    for machine in machines:
17 18 19 20 21 22 23 24 25 26
        unassign_ipv4(machine)
    return

def assign_ips(user):
    """ Assign une ipv4 aux machines d'un user """
    machines = Interface.objects.filter(machine=Machine.objects.filter(user=user))
    for machine in machines:
        if not machine.ipv4:
            interface = assign_ipv4(machine)
            interface.save()
27 28 29 30 31 32 33 34 35 36 37 38 39
    return

def free_ip():
    """ Renvoie la liste des ip disponibles """
    return IpList.objects.filter(interface__isnull=True)

def assign_ipv4(interface):
    """ Assigne une ip à l'interface """
    free_ips = free_ip()
    if free_ips:
        interface.ipv4 = free_ips[0]
    return interface

40 41 42 43
def unassign_ipv4(interface):
    interface.ipv4 = None
    interface.save()

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
    return render_to_response(template, c, context_instance=RequestContext(request))

def new_machine(request, userid):
    try:
        user = User.objects.get(pk=userid)
    except User.DoesNotExist:
        messages.error(request, u"Utilisateur inexistant" )
        return redirect("/machines/")
    machine = NewMachineForm(request.POST or None)
    interface = NewInterfaceForm(request.POST or None) 
    if machine.is_valid() and interface.is_valid():
        new_machine = machine.save(commit=False)
        new_machine.user = user
        new_machine.save()
        new_interface = interface.save(commit=False)
        new_interface.machine = new_machine
63 64 65 66
        if free_ip():
            new_interface = assign_ipv4(new_interface)
        else:
            messages.error(request, u"Il n'y a plus d'ip disponibles")
67 68
        new_interface.save()
        messages.success(request, "La machine a été crée")
69
        return redirect("/users/profil/" + userid)
70 71 72 73 74 75 76 77 78 79 80 81 82 83
    return form({'machineform': machine, 'interfaceform': interface}, 'machines/machine.html', request)

def edit_machine(request, interfaceid):
    try:
        interface = Interface.objects.get(pk=interfaceid)
    except Interface.DoesNotExist:
        messages.error(request, u"Interface inexistante" )
        return redirect("/machines")
    machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
    interface_form = EditInterfaceForm(request.POST or None, instance=interface)
    if machine_form.is_valid() and interface_form.is_valid():
        machine_form.save()
        interface_form.save()
        messages.success(request, "La machine a été modifiée")
chirac's avatar
chirac committed
84
        return redirect("/machines/")
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)

def new_interface(request, machineid):
    try:
        machine = Machine.objects.get(pk=machineid)
    except Machine.DoesNotExist:
        messages.error(request, u"Machine inexistante" )
        return redirect("/machines")
    interface_form = AddInterfaceForm(request.POST or None)
    machine_form = EditMachineForm(request.POST or None, instance=machine)
    if interface_form.is_valid() and machine_form.is_valid():
        machine_form.save()
        new_interface = interface_form.save(commit=False)
        new_interface.machine = machine
        new_interface.save()
        messages.success(request, "L'interface a été ajoutée")
chirac's avatar
chirac committed
101
        return redirect("/machines/")
102 103 104 105 106
    return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)

def index(request):
    machine_list = Interface.objects.order_by('pk')
    return render(request, 'machines/index.html', {'machine_list': machine_list})