From cc539095b36b8510a76b40fca77d54476c8a2a99 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 30 Jan 2018 02:01:06 +0000 Subject: [PATCH] =?UTF-8?q?Fix,=20nouvelle=20validation=20pour=20v=C3=A9ri?= =?UTF-8?q?fier=20l'unicit=C3=A9=20de=20l'ip=20slaac?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0075_auto_20180130_0052.py | 19 +++++++++++++++++++ machines/models.py | 11 ++++++++++- machines/views.py | 5 ++--- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 machines/migrations/0075_auto_20180130_0052.py diff --git a/machines/migrations/0075_auto_20180130_0052.py b/machines/migrations/0075_auto_20180130_0052.py new file mode 100644 index 00000000..473824b8 --- /dev/null +++ b/machines/migrations/0075_auto_20180130_0052.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-01-29 23:52 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0074_auto_20180129_0352'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='ipv6list', + unique_together=set([]), + ), + ] diff --git a/machines/models.py b/machines/models.py index 0b6e2171..721d6199 100644 --- a/machines/models.py +++ b/machines/models.py @@ -1395,7 +1395,6 @@ class Ipv6List(FieldPermissionModelMixin, models.Model): slaac_ip = models.BooleanField(default=False) class Meta: - unique_together = (("interface", "slaac_ip"),) permissions = ( ("view_ipv6list", "Peut voir un objet ipv6"), ("change_ipv6list_slaac_ip", "Peut changer la valeur slaac sur une ipv6"), @@ -1478,6 +1477,16 @@ class Ipv6List(FieldPermissionModelMixin, models.Model): 'slaac_ip' : self.can_change_slaac_ip, } + def clean(self, *args, **kwargs): + if self.slaac_ip and Ipv6List.objects.filter(interface=self.interface, slaac_ip=True).exclude(id=self.id): + raise ValidationError("Une ip slaac est déjà enregistrée") + super(Ipv6List, self).clean(*args, **kwargs) + + def save(self, *args, **kwargs): + """Force à avoir appellé clean avant""" + self.full_clean() + super(Ipv6List, self).save(*args, **kwargs) + def __str__(self): return str(self.ipv6) diff --git a/machines/views.py b/machines/views.py index dff298ef..eb42bb9b 100644 --- a/machines/views.py +++ b/machines/views.py @@ -375,10 +375,9 @@ def del_interface(request, interface, interfaceid): @can_edit(Interface) def new_ipv6list(request, interface, interfaceid): """Nouvelle ipv6""" - ipv6 = Ipv6ListForm(request.POST or None, user=request.user) + ipv6list_instance = Ipv6List(interface=interface) + ipv6 = Ipv6ListForm(request.POST or None, instance=ipv6list_instance, user=request.user) if ipv6.is_valid(): - ipv6 = ipv6.save(commit=False) - ipv6.interface = interface with transaction.atomic(), reversion.create_revision(): ipv6.save() reversion.set_user(request.user)