diff --git a/machines/forms.py b/machines/forms.py index 49150349..c0af0bd6 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -217,10 +217,7 @@ class IpTypeForm(FormRevMixin, ModelForm): stop après creation""" class Meta: model = IpType - fields = ['type', 'extension', 'need_infra', 'domaine_ip_start', - 'domaine_ip_stop', 'dnssec_reverse_v4', 'prefix_v6', - 'prefix_v6_length','dnssec_reverse_v6', 'vlan', - 'ouverture_ports'] + fields = '__all__' def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) @@ -232,7 +229,8 @@ class EditIpTypeForm(IpTypeForm): """Edition d'un iptype. Pas d'edition du rangev4 possible, car il faudrait synchroniser les objets iplist""" class Meta(IpTypeForm.Meta): - fields = ['extension', 'type', 'need_infra', 'prefix_v6', 'prefix_v6_length', + fields = ['extension', 'type', 'need_infra', 'domaine_ip_network', 'domaine_ip_netmask', + 'prefix_v6', 'prefix_v6_length', 'vlan', 'dnssec_reverse_v4', 'dnssec_reverse_v6', 'ouverture_ports'] diff --git a/machines/migrations/0091_auto_20180806_2310.py b/machines/migrations/0091_auto_20180806_2310.py new file mode 100644 index 00000000..cd756cad --- /dev/null +++ b/machines/migrations/0091_auto_20180806_2310.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-08-06 21:10 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0090_auto_20180805_1459'), + ] + + operations = [ + migrations.AddField( + model_name='iptype', + name='domaine_ip_netmask', + field=models.IntegerField(default=24, help_text='Netmask for the ipv4 range domain', validators=[django.core.validators.MaxValueValidator(31), django.core.validators.MinValueValidator(8)]), + ), + migrations.AddField( + model_name='iptype', + name='domaine_ip_network', + field=models.GenericIPAddressField(blank=True, help_text='Network containing the ipv4 range domain ip start/stop. Optional', null=True, protocol='IPv4'), + ), + ] diff --git a/machines/models.py b/machines/models.py index 1f8cbc0b..34f91adf 100644 --- a/machines/models.py +++ b/machines/models.py @@ -278,6 +278,20 @@ class IpType(RevMixin, AclMixin, models.Model): need_infra = models.BooleanField(default=False) domaine_ip_start = models.GenericIPAddressField(protocol='IPv4') domaine_ip_stop = models.GenericIPAddressField(protocol='IPv4') + domaine_ip_network = models.GenericIPAddressField( + protocol='IPv4', + null=True, + blank=True, + help_text="Network containing the ipv4 range domain ip start/stop. Optional" + ) + domaine_ip_netmask = models.IntegerField( + default=24, + validators=[ + MaxValueValidator(31), + MinValueValidator(8) + ], + help_text="Netmask for the ipv4 range domain" + ) dnssec_reverse_v4 = models.BooleanField( default=False, help_text="Activer DNSSEC sur le reverse DNS IPv4", @@ -331,6 +345,11 @@ class IpType(RevMixin, AclMixin, models.Model): """ Renvoie une liste des ip en string""" return [str(x) for x in self.ip_set] + @cached_property + def ip_set_cidrs_as_str(self): + """Renvoie la liste des cidrs du range en str""" + return [str(ip_range) for ip_range in self.ip_set.iter_cidrs()] + @cached_property def ip_set_full_info(self): """Iter sur les range cidr, et renvoie network, broacast , etc""" @@ -358,6 +377,24 @@ class IpType(RevMixin, AclMixin, models.Model): else: return None + @cached_property + def ip_network(self): + """Renvoie le network parent du range start-stop, si spécifié + Différent de ip_set_cidrs ou iP_set, car lui est supérieur ou égal""" + if self.domaine_ip_network: + return IPNetwork(str(self.domaine_ip_network) + '/' + str(self.domaine_ip_netmask)) + return None + + @cached_property + def ip_net_full_info(self): + """Renvoie les infos du network contenant du range""" + return { + 'network' : str(self.ip_network.network), + 'netmask' : str(self.ip_network.netmask), + 'broadcast' : str(self.ip_network.broadcast), + 'netmask_cidr' : str(self.ip_network.prefixlen), + } + @cached_property def complete_prefixv6(self): """Return the complete prefix v6 as cidr""" @@ -442,6 +479,11 @@ class IpType(RevMixin, AclMixin, models.Model): # On formate le prefix v6 if self.prefix_v6: self.prefix_v6 = str(IPNetwork(self.prefix_v6 + '/64').network) + # On vérifie qu'un domaine network/netmask contiens bien le domaine ip start-stop + if self.domaine_ip_network: + if not self.domaine_ip_start in self.ip_network or not self.domaine_ip_stop in self.ip_network: + raise ValidationError("If you specify a domaine ip network/netmask, it\ + must contain domaine ipstart-stop range") return def save(self, *args, **kwargs): diff --git a/machines/templates/machines/aff_iptype.html b/machines/templates/machines/aff_iptype.html index 7d4de6c5..ccb70a14 100644 --- a/machines/templates/machines/aff_iptype.html +++ b/machines/templates/machines/aff_iptype.html @@ -46,7 +46,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,