diff --git a/machines/views.py b/machines/views.py index 50cbe197..f06eb5ff 100644 --- a/machines/views.py +++ b/machines/views.py @@ -11,14 +11,19 @@ from .models import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInter 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) + 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() return def free_ip(): @@ -32,6 +37,10 @@ def assign_ipv4(interface): interface.ipv4 = free_ips[0] return interface +def unassign_ipv4(interface): + interface.ipv4 = None + interface.save() + def form(ctx, template, request): c = ctx c.update(csrf(request)) diff --git a/users/views.py b/users/views.py index 1d51006a..2e1f1f59 100644 --- a/users/views.py +++ b/users/views.py @@ -15,7 +15,7 @@ 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 machines.views import unassign_ips, assign_ips from re2o.login import makeSecret, hashNT @@ -24,6 +24,11 @@ def archive(user): unassign_ips(user) return +def unarchive(user): + """ Triger actions au desarchivage d'un user """ + assign_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'] @@ -83,6 +88,8 @@ def state(request, userid): if state.has_changed(): if state.cleaned_data['state'] == User.STATE_ARCHIVED: archive(user) + else: + unarchive(user) state.save() messages.success(request, "Etat changé avec succès") return redirect("/users/")