From efc360f862ce57d2e7d1d04d1bb5b7ba4a1c0389 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Thu, 13 Oct 2016 02:11:23 +0200 Subject: [PATCH] Lie le type de machine avec les acl infra, lie les / d'ip et type de machines --- machines/admin.py | 4 ++-- machines/forms.py | 14 ++++++++++--- machines/migrations/0023_iplist_ip_type.py | 21 +++++++++++++++++++ .../migrations/0024_machinetype_need_infra.py | 19 +++++++++++++++++ machines/models.py | 2 ++ .../templates/machines/aff_machinetype.html | 2 ++ machines/views.py | 20 +++++++++--------- 7 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 machines/migrations/0023_iplist_ip_type.py create mode 100644 machines/migrations/0024_machinetype_need_infra.py diff --git a/machines/admin.py b/machines/admin.py index b4446277..0ee9d483 100644 --- a/machines/admin.py +++ b/machines/admin.py @@ -7,13 +7,13 @@ class MachineAdmin(VersionAdmin): list_display = ('user','name','active') class MachineTypeAdmin(VersionAdmin): - list_display = ('type','extension') + list_display = ('type','extension','need_infra') class ExtensionAdmin(VersionAdmin): list_display = ('name',) class IpListAdmin(VersionAdmin): - list_display = ('ipv4',) + list_display = ('ipv4','ip_type') class InterfaceAdmin(VersionAdmin): list_display = ('machine','type','dns','mac_address','ipv4','details') diff --git a/machines/forms.py b/machines/forms.py index 7ba3909a..8a0b744e 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -1,6 +1,6 @@ from django.forms import ModelForm, Form, ValidationError from django import forms -from .models import Machine, Interface, MachineType, Extension +from .models import Machine, Interface, IpList, MachineType, Extension class EditMachineForm(ModelForm): class Meta: @@ -36,8 +36,12 @@ class AddInterfaceForm(EditInterfaceForm): fields = ['ipv4','mac_address','dns','type','details'] def __init__(self, *args, **kwargs): + infra = kwargs.pop('infra') super(AddInterfaceForm, self).__init__(*args, **kwargs) self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" + if not infra: + self.fields['type'].queryset = MachineType.objects.filter(need_infra=False) + self.fields['ipv4'].queryset = IpList.objects.filter(ip_type=MachineType.objects.filter(need_infra=False)) class NewInterfaceForm(EditInterfaceForm): class Meta(EditInterfaceForm.Meta): @@ -47,14 +51,18 @@ class BaseEditInterfaceForm(EditInterfaceForm): class Meta(EditInterfaceForm.Meta): fields = ['ipv4','mac_address','dns','type','details'] - def __init__(self, *args, **kwargs): + def __init__(self, infra=False, *args, **kwargs): + infra = kwargs.pop('infra') super(BaseEditInterfaceForm, self).__init__(*args, **kwargs) self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" + if not infra: + self.fields['type'].queryset = MachineType.objects.filter(need_infra=False) + self.fields['ipv4'].queryset = IpList.objects.filter(ip_type=MachineType.objects.filter(need_infra=False)) class MachineTypeForm(ModelForm): class Meta: model = MachineType - fields = ['type','extension'] + fields = ['type','extension','need_infra'] def __init__(self, *args, **kwargs): super(MachineTypeForm, self).__init__(*args, **kwargs) diff --git a/machines/migrations/0023_iplist_ip_type.py b/machines/migrations/0023_iplist_ip_type.py new file mode 100644 index 00000000..cd40aec5 --- /dev/null +++ b/machines/migrations/0023_iplist_ip_type.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0022_auto_20161011_1829'), + ] + + operations = [ + migrations.AddField( + model_name='iplist', + name='ip_type', + field=models.ForeignKey(to='machines.MachineType', on_delete=django.db.models.deletion.PROTECT, default=1), + preserve_default=False, + ), + ] diff --git a/machines/migrations/0024_machinetype_need_infra.py b/machines/migrations/0024_machinetype_need_infra.py new file mode 100644 index 00000000..8bc985d4 --- /dev/null +++ b/machines/migrations/0024_machinetype_need_infra.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', '0023_iplist_ip_type'), + ] + + operations = [ + migrations.AddField( + model_name='machinetype', + name='need_infra', + field=models.BooleanField(default=False), + ), + ] diff --git a/machines/models.py b/machines/models.py index a4c38aec..8c62ace0 100644 --- a/machines/models.py +++ b/machines/models.py @@ -18,6 +18,7 @@ class Machine(models.Model): class MachineType(models.Model): type = models.CharField(max_length=255) extension = models.ForeignKey('Extension', on_delete=models.PROTECT) + need_infra = models.BooleanField(default=False) def __str__(self): return self.type @@ -48,6 +49,7 @@ class Interface(models.Model): class IpList(models.Model): ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True) + ip_type = models.ForeignKey('MachineType', on_delete=models.PROTECT) def __str__(self): return self.ipv4 diff --git a/machines/templates/machines/aff_machinetype.html b/machines/templates/machines/aff_machinetype.html index 255c94ee..ea5d8691 100644 --- a/machines/templates/machines/aff_machinetype.html +++ b/machines/templates/machines/aff_machinetype.html @@ -3,6 +3,7 @@ Type de machine Extension + Nécessite l'autorisation infra @@ -11,6 +12,7 @@ {{ type.type }} {{ type.extension }} + {{ type.need_infra }} {% if is_infra %} Editer{% endif %} Historique diff --git a/machines/views.py b/machines/views.py index 1eacebfe..0c9cafaf 100644 --- a/machines/views.py +++ b/machines/views.py @@ -61,13 +61,13 @@ def assign_ips(user): interface.save() return -def free_ip(): +def free_ip(type): """ Renvoie la liste des ip disponibles """ - return IpList.objects.filter(interface__isnull=True) + return IpList.objects.filter(interface__isnull=True).filter(ip_type=type) def assign_ipv4(interface): """ Assigne une ip à l'interface """ - free_ips = free_ip() + free_ips = free_ip(interface.type) if free_ips: interface.ipv4 = free_ips[0] return interface @@ -94,7 +94,7 @@ def new_machine(request, userid): messages.error(request, "Vous ne pouvez pas ajouter une machine à un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) machine = NewMachineForm(request.POST or None) - interface = AddInterfaceForm(request.POST or None) + interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) if machine.is_valid() and interface.is_valid(): new_machine = machine.save(commit=False) new_machine.user = user @@ -105,7 +105,7 @@ def new_machine(request, userid): reversion.set_user(request.user) reversion.set_comment("Création") new_interface.machine = new_machine - if free_ip() and not new_interface.ipv4: + if free_ip(new_interface.type) and not new_interface.ipv4: new_interface = assign_ipv4(new_interface) elif not new_interface.ipv4: messages.error(request, u"Il n'y a plus d'ip disponibles") @@ -124,15 +124,15 @@ def edit_interface(request, interfaceid): except Interface.DoesNotExist: messages.error(request, u"Interface inexistante" ) return redirect("/machines") - if not request.user.has_perms(('cableur',)): + if not request.user.has_perms(('infra',)): if interface.machine.user != request.user: messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) machine_form = BaseEditMachineForm(request.POST or None, instance=interface.machine) - interface_form = BaseEditInterfaceForm(request.POST or None, instance=interface) + interface_form = BaseEditInterfaceForm(request.POST or None, instance=interface, infra=False) else: machine_form = EditMachineForm(request.POST or None, instance=interface.machine) - interface_form = EditInterfaceForm(request.POST or None, instance=interface) + interface_form = EditInterfaceForm(request.POST or None, instance=interface, infra=True) if machine_form.is_valid() and interface_form.is_valid(): new_interface = interface_form.save(commit=False) new_machine = machine_form.save(commit=False) @@ -179,12 +179,12 @@ def new_interface(request, machineid): if machine.user != request.user: messages.error(request, "Vous ne pouvez pas ajouter une interface à une machine d'un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) - interface_form = AddInterfaceForm(request.POST or None) + interface_form = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) if interface_form.is_valid(): new_interface = interface_form.save(commit=False) new_interface.machine = machine if full_domain_validator(request, new_interface): - if free_ip() and not new_interface.ipv4: + if free_ip(new_interface.type) and not new_interface.ipv4: new_interface = assign_ipv4(new_interface) elif not new_interface.ipv4: messages.error(request, u"Il n'y a plus d'ip disponibles")