8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-23 23:43:47 +00:00

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

This commit is contained in:
chirac 2016-07-04 11:31:20 +02:00
parent 5a011bc0fd
commit 36f95c8864
4 changed files with 58 additions and 6 deletions

View file

@ -0,0 +1,19 @@
# -*- 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 = [
migrations.AlterField(
model_name='interface',
name='dns',
field=models.CharField(max_length=255, help_text='Obligatoire et unique', unique=True),
),
]

View file

@ -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
machine.save()
def unassign_ips(user):
machines = Interface.objects.filter(machine=Machine.objects.filter(user=user))
for machine in machines:
unassign_ip(machine)
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
def form(ctx, template, request):
c = ctx
c.update(csrf(request))
@ -30,6 +51,10 @@ def new_machine(request, userid):
new_machine.save()
new_interface = interface.save(commit=False)
new_interface.machine = new_machine
if free_ip():
new_interface = assign_ipv4(new_interface)
else:
messages.error(request, u"Il n'y a plus d'ip disponibles")
new_interface.save()
messages.success(request, "La machine a été crée")
return redirect("/users/")

View file

@ -110,7 +110,6 @@ class StateForm(ModelForm):
model = User
fields = ['state']
class SchoolForm(ModelForm):
class Meta:
model = School

View file

@ -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 """
unassign_ips(user)
return
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():
user.save()
state = StateForm(request.POST or None, instance=user)
if state.is_valid():
if state.has_changed():
if state.cleaned_data['state'] == User.STATE_ARCHIVED:
archive(user)
state.save()
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):
try: