Commit 27d68235 authored by Gabriel Detraz's avatar Gabriel Detraz Committed by root
Browse files

Utilise des ranges en lieu et place des slash

parent 81087672
......@@ -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)
# -*- 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 = [
field=models.GenericIPAddressField(default='', protocol='IPv4'),
......@@ -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)
def network(self):
return str(self.domaine_ip) + '/' + str(self.domaine_range)
def ip_network(self):
return IPNetwork(
def ip_range(self):
return IPRange(self.domaine_ip_start, end=self.domaine_ip_stop)
def ip_set(self):
return IPSet(self.ip_network)
return IPSet(self.ip_range)
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]
......@@ -105,6 +104,11 @@ class IpType(models.Model):
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(
if not self.ip_set.isdisjoint(element.ip_set):
......@@ -28,8 +28,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<th>Type d'ip</th>
<th>Nécessite l'autorisation infra</th>
<th>Domaine d'ip</th>
<th>Sur vlan</th>
......@@ -40,8 +40,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<td>{{ type.type }}</td>
<td>{{ type.extension }}</td>
<td>{{ type.need_infra }}</td>
<td>{{ type.domaine_ip }}</td>
<td>{{ type.domaine_range }}</td>
<td>{{ type.domaine_ip_start }}</td>
<td>{{ type.domaine_ip_stop }}</td>
<td>{{ type.vlan }}</td>
<td class="text-right">
{% if is_infra %}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment