From 1e0625fd5899ea8cb5a96e59047f49561dd7d4ea Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Fri, 18 Nov 2016 17:36:30 +0100 Subject: [PATCH] Ajoute le support des alias dns --- machines/admin.py | 2 +- .../migrations/0030_auto_20161118_1730.py | 30 +++++++++++++++++++ machines/models.py | 13 +++++++- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 machines/migrations/0030_auto_20161118_1730.py diff --git a/machines/admin.py b/machines/admin.py index d44e2248..658d8f85 100644 --- a/machines/admin.py +++ b/machines/admin.py @@ -23,7 +23,7 @@ class InterfaceAdmin(VersionAdmin): list_display = ('machine','type','dns','mac_address','ipv4','details') class AliasAdmin(VersionAdmin): - list_display = ('interface_parent', 'alias') + list_display = ('interface_parent', 'alias', 'extension') admin.site.register(Machine, MachineAdmin) admin.site.register(MachineType, MachineTypeAdmin) diff --git a/machines/migrations/0030_auto_20161118_1730.py b/machines/migrations/0030_auto_20161118_1730.py new file mode 100644 index 00000000..64f4a525 --- /dev/null +++ b/machines/migrations/0030_auto_20161118_1730.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0029_iptype_domaine_range'), + ] + + operations = [ + migrations.AddField( + model_name='alias', + name='extension', + field=models.ForeignKey(to='machines.Extension', default=1, on_delete=django.db.models.deletion.PROTECT), + preserve_default=False, + ), + migrations.AlterField( + model_name='alias', + name='alias', + field=models.CharField(max_length=255, help_text='Obligatoire et unique, ne doit pas comporter de points'), + ), + migrations.AlterUniqueTogether( + name='alias', + unique_together=set([('alias', 'extension')]), + ), + ] diff --git a/machines/models.py b/machines/models.py index 926dfb51..aaf07606 100644 --- a/machines/models.py +++ b/machines/models.py @@ -67,6 +67,9 @@ class Interface(models.Model): def clean(self, *args, **kwargs): self.mac_address = str(EUI(self.mac_address)) or None + alias = Alias.objects.filter(alias=self.dns).filter(extension=self.ipv4.ip_type.extension) + if alias: + raise ValidationError("Impossible, le dns est déjà utilisé par un alias (%s)" % alias[0]) def __str__(self): return self.dns @@ -75,7 +78,15 @@ class Alias(models.Model): PRETTY_NAME = "Alias dns" interface_parent = models.ForeignKey('Interface', on_delete=models.CASCADE) - alias = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", max_length=255, unique=True) + alias = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", max_length=255) + extension = models.ForeignKey('Extension', on_delete=models.PROTECT) + + class Meta: + unique_together = ("alias", "extension") + + def clean(self, *args, **kwargs): + if Interface.objects.filter(dns=self.alias).filter(ipv4=IpList.objects.filter(ip_type=IpType.objects.filter(extension=self.extension))): + raise ValidationError("Impossible d'ajouter l'alias, déjà utilisé par une machine") def __str__(self): return self.alias