From b06c7ed6ffeed0becf8743b886bcf3eec99c35a1 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 29 Aug 2017 00:35:03 +0200 Subject: [PATCH] Utilise des ranges en lieu et place des slash --- machines/forms.py | 3 +- .../migrations/0052_auto_20170828_2322.py | 30 +++++++++++++++++++ machines/models.py | 26 +++++++++------- machines/templates/machines/aff_iptype.html | 8 ++--- 4 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 machines/migrations/0052_auto_20170828_2322.py diff --git a/machines/forms.py b/machines/forms.py index 3512e277..dedd8395 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -143,7 +143,8 @@ class DelMachineTypeForm(Form): class IpTypeForm(ModelForm): class Meta: model = IpType - fields = ['type','extension','need_infra','domaine_ip','domaine_range', 'vlan'] + fields = ['type','extension','need_infra','domaine_ip_start','domaine_ip_stop', 'vlan'] + def __init__(self, *args, **kwargs): super(IpTypeForm, self).__init__(*args, **kwargs) diff --git a/machines/migrations/0052_auto_20170828_2322.py b/machines/migrations/0052_auto_20170828_2322.py new file mode 100644 index 00000000..b8ab65ca --- /dev/null +++ b/machines/migrations/0052_auto_20170828_2322.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-08-28 21:22 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0051_iptype_vlan'), + ] + + operations = [ + migrations.RenameField( + model_name='iptype', + old_name='domaine_ip', + new_name='domaine_ip_start', + ), + migrations.RemoveField( + model_name='iptype', + name='domaine_range', + ), + migrations.AddField( + model_name='iptype', + name='domaine_ip_stop', + field=models.GenericIPAddressField(default='255.255.254.254', protocol='IPv4'), + preserve_default=False, + ), + ] diff --git a/machines/models.py b/machines/models.py index 4c236296..0153e569 100644 --- a/machines/models.py +++ b/machines/models.py @@ -27,7 +27,7 @@ from django.forms import ValidationError from django.utils.functional import cached_property from django.utils import timezone from macaddress.fields import MACAddressField -from netaddr import mac_bare, EUI, IPSet, IPNetwork +from netaddr import mac_bare, EUI, IPSet, IPRange, IPNetwork, IPAddress from django.core.validators import MinValueValidator,MaxValueValidator import re from reversion import revisions as reversion @@ -65,21 +65,17 @@ class IpType(models.Model): type = models.CharField(max_length=255) extension = models.ForeignKey('Extension', on_delete=models.PROTECT) need_infra = models.BooleanField(default=False) - domaine_ip = models.GenericIPAddressField(protocol='IPv4') - domaine_range = models.IntegerField(validators=[MinValueValidator(16), MaxValueValidator(32)]) + domaine_ip_start = models.GenericIPAddressField(protocol='IPv4') + domaine_ip_stop = models.GenericIPAddressField(protocol='IPv4') vlan = models.ForeignKey('Vlan', on_delete=models.PROTECT, blank=True, null=True) @cached_property - def network(self): - return str(self.domaine_ip) + '/' + str(self.domaine_range) - - @cached_property - def ip_network(self): - return IPNetwork(self.network) + def ip_range(self): + return IPRange(self.domaine_ip_start, end=self.domaine_ip_stop) @cached_property def ip_set(self): - return IPSet(self.ip_network) + return IPSet(self.ip_range) @cached_property def ip_set_as_str(self): @@ -93,7 +89,10 @@ class IpType(models.Model): def gen_ip_range(self): # Creation du range d'ip dans les objets iplist - ip_obj = [IpList(ip_type=self, ipv4=str(ip)) for ip in self.ip_network.iter_hosts()] + networks = [] + for net in self.ip_range.cidrs(): + networks += net.iter_hosts() + ip_obj = [IpList(ip_type=self, ipv4=str(ip)) for ip in networks] IpList.objects.bulk_create(ip_obj) return @@ -105,6 +104,11 @@ class IpType(models.Model): ip.delete() def clean(self): + if IPAddress(self.domaine_ip_start) > IPAddress(self.domaine_ip_stop): + raise ValidationError("Domaine end doit être après start...") + # On ne crée pas plus grand qu'un /16 + if self.ip_range.size > 65536: + raise ValidationError("Le range est trop gros, vous ne devez pas créer plus grand qu'un /16") # On check que les / ne se recoupent pas for element in IpType.objects.all().exclude(pk=self.pk): if not self.ip_set.isdisjoint(element.ip_set): diff --git a/machines/templates/machines/aff_iptype.html b/machines/templates/machines/aff_iptype.html index c0f6b880..46318c83 100644 --- a/machines/templates/machines/aff_iptype.html +++ b/machines/templates/machines/aff_iptype.html @@ -28,8 +28,8 @@ with this program; if not, write to the Free Software Foundation, Inc., Type d'ip Extension Nécessite l'autorisation infra - Domaine d'ip - Range + Début + Fin Sur vlan @@ -40,8 +40,8 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ type.type }} {{ type.extension }} {{ type.need_infra }} - {{ type.domaine_ip }} - {{ type.domaine_range }} + {{ type.domaine_ip_start }} + {{ type.domaine_ip_stop }} {{ type.vlan }} {% if is_infra %}