From 327407906267190edf0a6bdebbd332fb87d57e5c Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 6 Sep 2017 11:07:36 +0200 Subject: [PATCH] =?UTF-8?q?Generation=20des=20entr=C3=A9es=20dns=20dans=20?= =?UTF-8?q?models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machines/models.py | 16 ++++++++++++++++ machines/serializers.py | 36 ++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/machines/models.py b/machines/models.py index 4cf3941c..615507dc 100644 --- a/machines/models.py +++ b/machines/models.py @@ -145,6 +145,10 @@ class Extension(models.Model): need_infra = models.BooleanField(default=False) origin = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True) + @cached_property + def dns_entry(self): + return "@ IN A " + str(self.origin) + def __str__(self): return self.name @@ -155,6 +159,10 @@ class Mx(models.Model): priority = models.IntegerField(unique=True) name = models.OneToOneField('Domain', on_delete=models.PROTECT) + @cached_property + def dns_entry(self): + return "@ IN MX " + str(self.priority) + " " + str(self.name) + def __str__(self): return str(self.zone) + ' ' + str(self.priority) + ' ' + str(self.name) @@ -164,6 +172,10 @@ class Ns(models.Model): zone = models.ForeignKey('Extension', on_delete=models.PROTECT) ns = models.OneToOneField('Domain', on_delete=models.PROTECT) + @cached_property + def dns_entry(self): + return "@ IN NS " + str(self.ns) + def __str__(self): return str(self.zone) + ' ' + str(self.ns) @@ -274,6 +286,10 @@ class Domain(models.Model): self.validate_unique() super(Domain, self).clean() + @cached_property + def dns_entry(self): + if self.cname: + return str(self.name) + " IN CNAME " + str(self.cname) def save(self, *args, **kwargs): if not self.get_extension(): diff --git a/machines/serializers.py b/machines/serializers.py index 329bffa7..bfe5f295 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -68,35 +68,43 @@ class TypeSerializer(serializers.ModelSerializer): class ExtensionSerializer(serializers.ModelSerializer): origin = serializers.SerializerMethodField('get_origin_ip') + zone_entry = serializers.SerializerMethodField('get_zone_name') class Meta: model = Extension - fields = ('name', 'origin') + fields = ('name', 'origin', 'zone_entry') def get_origin_ip(self, obj): return obj.origin.ipv4 + def get_zone_name(self, obj): + return str(obj.dns_entry) + class MxSerializer(serializers.ModelSerializer): - name = serializers.SerializerMethodField('get_mx_name') + name = serializers.SerializerMethodField('get_entry_name') zone = serializers.SerializerMethodField('get_zone_name') + mx_entry = serializers.SerializerMethodField('get_mx_name') class Meta: model = Mx - fields = ('zone', 'priority', 'name') + fields = ('zone', 'priority', 'name', 'mx_entry') - def get_mx_name(self, obj): + def get_entry_name(self, obj): return str(obj.name) def get_zone_name(self, obj): return obj.zone.name + def get_mx_name(self, obj): + return str(obj.dns_entry) + class TextSerializer(serializers.ModelSerializer): zone = serializers.SerializerMethodField('get_zone_name') - text = serializers.SerializerMethodField('get_text_name') + text_entry = serializers.SerializerMethodField('get_text_name') class Meta: model = Text - fields = ('zone','text') + fields = ('zone','text_entry','field1', 'field2') def get_zone_name(self, obj): return str(obj.zone.name) @@ -107,10 +115,11 @@ class TextSerializer(serializers.ModelSerializer): class NsSerializer(serializers.ModelSerializer): zone = serializers.SerializerMethodField('get_zone_name') ns = serializers.SerializerMethodField('get_domain_name') + ns_entry = serializers.SerializerMethodField('get_text_name') class Meta: model = Ns - fields = ('zone', 'ns') + fields = ('zone', 'ns', 'ns_entry') def get_zone_name(self, obj): return obj.zone.name @@ -118,20 +127,27 @@ class NsSerializer(serializers.ModelSerializer): def get_domain_name(self, obj): return str(obj.ns) + def get_text_name(self, obj): + return str(obj.dns_entry) + class DomainSerializer(serializers.ModelSerializer): extension = serializers.SerializerMethodField('get_zone_name') - cname = serializers.SerializerMethodField('get_cname_name') + cname = serializers.SerializerMethodField('get_alias_name') + cname_entry = serializers.SerializerMethodField('get_cname_name') class Meta: model = Domain - fields = ('name', 'extension', 'cname') + fields = ('name', 'extension', 'cname', 'cname_entry') def get_zone_name(self, obj): return obj.extension.name - def get_cname_name(self, obj): + def get_alias_name(self, obj): return str(obj.cname) + def get_cname_name(self, obj): + return str(obj.dns_entry) + class ServiceServersSerializer(serializers.ModelSerializer): server = serializers.SerializerMethodField('get_server_name') service = serializers.SerializerMethodField('get_service_name')