mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-23 23:43:47 +00:00
Utilise des ranges en lieu et place des slash
This commit is contained in:
parent
8108767260
commit
27d6823532
4 changed files with 51 additions and 16 deletions
|
@ -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)
|
||||
|
|
30
machines/migrations/0052_auto_20170828_2322.py
Normal file
30
machines/migrations/0052_auto_20170828_2322.py
Normal file
|
@ -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,
|
||||
),
|
||||
]
|
|
@ -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):
|
||||
|
|
|
@ -28,8 +28,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
<th>Type d'ip</th>
|
||||
<th>Extension</th>
|
||||
<th>Nécessite l'autorisation infra</th>
|
||||
<th>Domaine d'ip</th>
|
||||
<th>Range</th>
|
||||
<th>Début</th>
|
||||
<th>Fin</th>
|
||||
<th>Sur vlan</th>
|
||||
<th></th>
|
||||
<th></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 %}
|
||||
|
|
Loading…
Reference in a new issue