From 2111cb63dd0b38519090074d03c6d977167cff82 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 3 Oct 2017 02:36:39 +0200 Subject: [PATCH] Attribut ipv6 sur les interfaces --- machines/forms.py | 4 ++-- machines/migrations/0059_iptype_prefix_v6.py | 20 +++++++++++++++++++ machines/models.py | 17 +++++++++++++++- machines/templates/machines/aff_iptype.html | 2 ++ machines/templates/machines/aff_machines.html | 6 ++++-- .../migrations/0020_optionalmachine_ipv6.py | 20 +++++++++++++++++++ preferences/models.py | 1 + .../preferences/display_preferences.html | 4 +++- re2o/context_processors.py | 4 +++- 9 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 machines/migrations/0059_iptype_prefix_v6.py create mode 100644 preferences/migrations/0020_optionalmachine_ipv6.py diff --git a/machines/forms.py b/machines/forms.py index c29af9dd..8321db8a 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -142,7 +142,7 @@ class DelMachineTypeForm(Form): class IpTypeForm(ModelForm): class Meta: model = IpType - fields = ['type','extension','need_infra','domaine_ip_start','domaine_ip_stop', 'vlan'] + fields = ['type','extension','need_infra','domaine_ip_start','domaine_ip_stop', 'prefix_v6', 'vlan'] def __init__(self, *args, **kwargs): @@ -151,7 +151,7 @@ class IpTypeForm(ModelForm): class EditIpTypeForm(IpTypeForm): class Meta(IpTypeForm.Meta): - fields = ['extension','type','need_infra', 'vlan'] + fields = ['extension','type','need_infra', 'prefix_v6', 'vlan'] class DelIpTypeForm(Form): iptypes = forms.ModelMultipleChoiceField(queryset=IpType.objects.all(), label="Types d'ip actuelles", widget=forms.CheckboxSelectMultiple) diff --git a/machines/migrations/0059_iptype_prefix_v6.py b/machines/migrations/0059_iptype_prefix_v6.py new file mode 100644 index 00000000..464fc5e6 --- /dev/null +++ b/machines/migrations/0059_iptype_prefix_v6.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-10-02 16:33 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0058_auto_20171002_0350'), + ] + + operations = [ + migrations.AddField( + model_name='iptype', + name='prefix_v6', + field=models.GenericIPAddressField(blank=True, null=True, protocol='IPv6'), + ), + ] diff --git a/machines/models.py b/machines/models.py index cb859141..8e7b1c3c 100644 --- a/machines/models.py +++ b/machines/models.py @@ -70,6 +70,7 @@ class IpType(models.Model): need_infra = models.BooleanField(default=False) domaine_ip_start = models.GenericIPAddressField(protocol='IPv4') domaine_ip_stop = models.GenericIPAddressField(protocol='IPv4') + prefix_v6 = models.GenericIPAddressField(protocol='IPv6', null=True, blank=True) vlan = models.ForeignKey('Vlan', on_delete=models.PROTECT, blank=True, null=True) @cached_property @@ -122,6 +123,9 @@ class IpType(models.Model): for element in IpType.objects.all().exclude(pk=self.pk): if not self.ip_set.isdisjoint(element.ip_set): raise ValidationError("Le range indiqué n'est pas disjoint des ranges existants") + # On formate le prefix v6 + if self.prefix_v6: + self.prefix_v6 = str(IPNetwork(self.prefix_v6 + '/64').network) return def save(self, *args, **kwargs): @@ -218,7 +222,6 @@ class Interface(models.Model): PRETTY_NAME = "Interface" ipv4 = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True) - #ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True) mac_address = MACAddressField(integer=False, unique=True) machine = models.ForeignKey('Machine', on_delete=models.CASCADE) type = models.ForeignKey('MachineType', on_delete=models.PROTECT) @@ -232,6 +235,18 @@ class Interface(models.Model): user = self.machine.user return machine.active and user.has_access() + + @cached_property + def ipv6_object(self): + if self.type.ip_type.prefix_v6: + return EUI(self.mac_address).ipv6(IPNetwork(self.type.ip_type.prefix_v6).network) + else: + return None + + @cached_property + def ipv6(self): + return str(self.ipv6_object) + def mac_bare(self): return str(EUI(self.mac_address, dialect=mac_bare)).lower() diff --git a/machines/templates/machines/aff_iptype.html b/machines/templates/machines/aff_iptype.html index 46318c83..aafc4c1d 100644 --- a/machines/templates/machines/aff_iptype.html +++ b/machines/templates/machines/aff_iptype.html @@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc., Nécessite l'autorisation infra Début Fin + Préfixe v6 Sur vlan @@ -42,6 +43,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ type.need_infra }} {{ type.domaine_ip_start }} {{ type.domaine_ip_stop }} + {{ type.prefix_v6 }} {{ type.vlan }} {% if is_infra %} diff --git a/machines/templates/machines/aff_machines.html b/machines/templates/machines/aff_machines.html index 6cf306f7..b9c8c9e9 100644 --- a/machines/templates/machines/aff_machines.html +++ b/machines/templates/machines/aff_machines.html @@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc., Nom dns Type Mac - Ipv4 + IP @@ -74,7 +74,9 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ interface.type }} {{ interface.mac_address }} - {{ interface.ipv4 }} + {{ interface.ipv4 }} + {{ interface.ipv6 }} +