Commit 36f95c88 authored by chirac's avatar chirac

Desassigne les ip à l'archivage + assigne une ip à la creation d'une machine

parent 5a011bc0
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('machines', '0011_auto_20160704_0105'),
operations = [
field=models.CharField(max_length=255, help_text='Obligatoire et unique', unique=True),
......@@ -8,9 +8,30 @@ from django.template import Context, RequestContext, loader
from django.contrib import messages
from .models import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm
from .models import Machine, Interface
from .models import Machine, Interface, IpList
from users.models import User
def unassign_ip(machine):
machine.ipv4 = None
def unassign_ips(user):
machines = Interface.objects.filter(machine=Machine.objects.filter(user=user))
for machine in machines:
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
def form(ctx, template, request):
c = ctx
......@@ -30,6 +51,10 @@ def new_machine(request, userid):
new_interface =
new_interface.machine = new_machine
if free_ip():
new_interface = assign_ipv4(new_interface)
messages.error(request, u"Il n'y a plus d'ip disponibles")
messages.success(request, "La machine a été crée")
return redirect("/users/")
......@@ -110,7 +110,6 @@ class StateForm(ModelForm):
model = User
fields = ['state']
class SchoolForm(ModelForm):
class Meta:
model = School
......@@ -15,9 +15,15 @@ from machines.models import Machine, Interface
from users.forms import PassForm
from search.models import SearchForm
from cotisations.views import is_adherent, end_adhesion
from machines.views import unassign_ips
from re2o.login import makeSecret, hashNT
def archive(user):
""" Archive un utilisateur """
def end_ban(user):
""" Renvoie la date de fin de ban d'un user, False sinon """
date_max = Ban.objects.all().filter(user=user).aggregate(Max('date_end'))['date_end__max']
......@@ -72,12 +78,15 @@ def state(request, userid):
except User.DoesNotExist:
messages.error(request, u"Utilisateur inexistant" )
return redirect("/users/")
user = StateForm(request.POST or None, instance=user)
if user.is_valid():
state = StateForm(request.POST or None, instance=user)
if state.is_valid():
if state.has_changed():
if state.cleaned_data['state'] == User.STATE_ARCHIVED:
messages.success(request, "Etat changé avec succès")
return redirect("/users/")
return form({'userform': user}, 'users/user.html', request)
return form({'userform': state}, 'users/user.html', request)
def password(request, userid):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment