From e2661a668537a1aff743dc0f00defc30f56b98f4 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 24 Dec 2016 18:07:09 +0100 Subject: [PATCH] Creation de domain, transfert --- machines/admin.py | 8 ++--- .../migrations/0035_auto_20161224_1201.py | 15 +++++++++ .../migrations/0036_auto_20161224_1204.py | 28 ++++++++++++++++ machines/migrations/0037_domain_cname.py | 19 +++++++++++ .../migrations/0038_auto_20161224_1721.py | 19 +++++++++++ .../migrations/0039_auto_20161224_1732.py | 19 +++++++++++ machines/models.py | 32 ++++++++----------- 7 files changed, 118 insertions(+), 22 deletions(-) create mode 100644 machines/migrations/0035_auto_20161224_1201.py create mode 100644 machines/migrations/0036_auto_20161224_1204.py create mode 100644 machines/migrations/0037_domain_cname.py create mode 100644 machines/migrations/0038_auto_20161224_1721.py create mode 100644 machines/migrations/0039_auto_20161224_1732.py diff --git a/machines/admin.py b/machines/admin.py index aa7d6bc5..23fd068e 100644 --- a/machines/admin.py +++ b/machines/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from reversion.admin import VersionAdmin -from .models import IpType, Machine, MachineType, Alias, IpList, Interface, Extension, Mx, Ns +from .models import IpType, Machine, MachineType, Domain, IpList, Interface, Extension, Mx, Ns class MachineAdmin(VersionAdmin): list_display = ('user','name','active') @@ -28,8 +28,8 @@ class IpListAdmin(VersionAdmin): class InterfaceAdmin(VersionAdmin): list_display = ('machine','type','dns','mac_address','ipv4','details') -class AliasAdmin(VersionAdmin): - list_display = ('interface_parent', 'alias', 'extension') +class DomainAdmin(VersionAdmin): + list_display = ('interface_parent', 'name', 'extension', 'cname') admin.site.register(Machine, MachineAdmin) admin.site.register(MachineType, MachineTypeAdmin) @@ -39,4 +39,4 @@ admin.site.register(Mx, MxAdmin) admin.site.register(Ns, NsAdmin) admin.site.register(IpList, IpListAdmin) admin.site.register(Interface, InterfaceAdmin) -admin.site.register(Alias, AliasAdmin) +admin.site.register(Domain, DomainAdmin) diff --git a/machines/migrations/0035_auto_20161224_1201.py b/machines/migrations/0035_auto_20161224_1201.py new file mode 100644 index 00000000..bda8e9b9 --- /dev/null +++ b/machines/migrations/0035_auto_20161224_1201.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0034_iplist_need_infra'), + ] + + operations = [ + migrations.RenameModel('Alias', 'Domain') + ] diff --git a/machines/migrations/0036_auto_20161224_1204.py b/machines/migrations/0036_auto_20161224_1204.py new file mode 100644 index 00000000..3eb97a6c --- /dev/null +++ b/machines/migrations/0036_auto_20161224_1204.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0035_auto_20161224_1201'), + ] + + operations = [ + migrations.RenameField( + model_name='domain', + old_name='alias', + new_name='name', + ), + migrations.AlterField( + model_name='domain', + name='interface_parent', + field=models.ForeignKey(to='machines.Interface', null=True, blank=True), + ), + migrations.AlterUniqueTogether( + name='domain', + unique_together=set([('name', 'extension')]), + ), + ] diff --git a/machines/migrations/0037_domain_cname.py b/machines/migrations/0037_domain_cname.py new file mode 100644 index 00000000..ee901817 --- /dev/null +++ b/machines/migrations/0037_domain_cname.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0036_auto_20161224_1204'), + ] + + operations = [ + migrations.AddField( + model_name='domain', + name='cname', + field=models.OneToOneField(related_name='related_domain', null=True, to='machines.Domain', blank=True), + ), + ] diff --git a/machines/migrations/0038_auto_20161224_1721.py b/machines/migrations/0038_auto_20161224_1721.py new file mode 100644 index 00000000..36f00f71 --- /dev/null +++ b/machines/migrations/0038_auto_20161224_1721.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0037_domain_cname'), + ] + + operations = [ + migrations.AlterField( + model_name='domain', + name='cname', + field=models.ForeignKey(null=True, to='machines.Domain', related_name='related_domain', blank=True), + ), + ] diff --git a/machines/migrations/0039_auto_20161224_1732.py b/machines/migrations/0039_auto_20161224_1732.py new file mode 100644 index 00000000..48f8a0d6 --- /dev/null +++ b/machines/migrations/0039_auto_20161224_1732.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0038_auto_20161224_1721'), + ] + + operations = [ + migrations.AlterField( + model_name='domain', + name='interface_parent', + field=models.OneToOneField(blank=True, null=True, to='machines.Interface'), + ), + ] diff --git a/machines/models.py b/machines/models.py index 26a3166f..791b4575 100644 --- a/machines/models.py +++ b/machines/models.py @@ -55,7 +55,7 @@ class Mx(models.Model): zone = models.ForeignKey('Extension', on_delete=models.PROTECT) priority = models.IntegerField(unique=True) - name = models.OneToOneField('Alias', on_delete=models.PROTECT) + name = models.OneToOneField('Domain', on_delete=models.PROTECT) def __str__(self): return str(self.zone) + ' ' + str(self.priority) + ' ' + str(self.name) @@ -91,33 +91,29 @@ class Interface(models.Model): def clean(self, *args, **kwargs): self.mac_address = str(EUI(self.mac_address)) or None - if self.ipv4: - alias = Alias.objects.filter(alias=self.dns).filter(extension=self.ipv4.ip_type.extension) - else: - alias = Alias.objects.filter(alias=self.dns) - if alias: - raise ValidationError("Impossible, le dns est déjà utilisé par un alias (%s)" % alias[0]) def __str__(self): - return self.dns + return self.domain_set.all().first() -class Alias(models.Model): - PRETTY_NAME = "Alias dns" +class Domain(models.Model): + PRETTY_NAME = "Domaine 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) + interface_parent = models.OneToOneField('Interface', on_delete=models.CASCADE, blank=True, null=True) + name = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", max_length=255) extension = models.ForeignKey('Extension', on_delete=models.PROTECT) + cname = models.ForeignKey('self', null=True, blank=True, related_name='related_domain') class Meta: - unique_together = ("alias", "extension") + unique_together = ("name", "extension") - def clean(self, *args, **kwargs): - if hasattr(self, 'alias') and hasattr(self, 'extension'): - if Interface.objects.filter(dns=self.alias).filter(ipv4__in=IpList.objects.filter(ip_type__in=IpType.objects.filter(extension=self.extension))): - raise ValidationError("Impossible d'ajouter l'alias, déjà utilisé par une machine") + def clean(self): + if self.interface_parent and self.cname: + raise ValidationError("On ne peut créer à la fois A et CNAME") + if self.related==self: + raise ValidationError("On ne peut créer un cname sur lui même") def __str__(self): - return str(self.alias) + str(self.extension) + return str(self.name) + str(self.extension) class IpList(models.Model): PRETTY_NAME = "Addresses ipv4"