diff --git a/machines/migrations/0012_auto_20160704_0118.py b/machines/migrations/0012_auto_20160704_0118.py new file mode 100644 index 00000000..e33b5fe1 --- /dev/null +++ b/machines/migrations/0012_auto_20160704_0118.py @@ -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), + ), + ] diff --git a/machines/views.py b/machines/views.py index b89e9e71..50cbe197 100644 --- a/machines/views.py +++ b/machines/views.py @@ -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/") diff --git a/users/models.py b/users/models.py index 27c7963e..c30e4a68 100644 --- a/users/models.py +++ b/users/models.py @@ -110,7 +110,6 @@ class StateForm(ModelForm): model = User fields = ['state'] - class SchoolForm(ModelForm): class Meta: model = School diff --git a/users/views.py b/users/views.py index 813f1fdc..1d51006a 100644 --- a/users/views.py +++ b/users/views.py @@ -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: