From acd082b2f8328eaeed0894942c4df24ebef508a4 Mon Sep 17 00:00:00 2001 From: Nikolai Kristiansen Date: Tue, 6 Oct 2015 23:08:49 +0200 Subject: [PATCH] Starts zone file template --- mdb/models.py | 11 +++++++++++ mdb/templates/zone_file.txt | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 mdb/templates/zone_file.txt diff --git a/mdb/models.py b/mdb/models.py index 969b530..f0a5ae0 100644 --- a/mdb/models.py +++ b/mdb/models.py @@ -1,5 +1,6 @@ from django.db import models from django.db.models import Count +from django.template.loader import render_to_string from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ from mdb.utils import host_as_pxe_files @@ -50,6 +51,9 @@ class Domain(models.Model): def __str__(self): return self.domain_name + def domain_admin_formatted(self): + return self.domain_admin.replace("@", ".") + def num_records(self): host_a_records = Host.objects.filter(interface__domain=self).count() size = { @@ -136,6 +140,13 @@ class Domain(models.Model): return content + def zone_file_contents_alt(self): + context = { + 'domain': self, + 'now': datetime.datetime.now() + } + return render_to_string('zone_file.txt', context=context) + @python_2_unicode_compatible class DomainSrvRecord(models.Model): diff --git a/mdb/templates/zone_file.txt b/mdb/templates/zone_file.txt new file mode 100644 index 0000000..268d1ed --- /dev/null +++ b/mdb/templates/zone_file.txt @@ -0,0 +1,31 @@ +; serial: {{ domain.domain_serial }} +; zone file for {{ domain.domain_name }} +; {{ now|date:"c" }} +; filename: {{ domain.domain_filename }} +$TTL {{ domain.domain_ttl }} +@ IN SOA {{ domain.domain_soa }}. {{ domain.domain_admin_formatted }}. ( + {{ domain.domain_serial|stringformat:"-10s" }} ; serial + {{ domain.domain_refresh|stringformat:"-10s" }} ; refresh + {{ domain.domain_retry|stringformat:"-10s" }} ; retry + {{ domain.domain_expire|stringformat:"-10s" }} ; expire + {{ domain.domain_minimum_ttl }} ) ; minimum ttl +; +{% for nameserver in domain.domain_nameservers.all %}@ IN NS {{ nameserver.hostname }}. +{% endfor %}{% for mx in domain.domain_mailexchanges.all %}@ MX {{ mx.priority }} {{ mx.hostname }}.{% endfor %} +{% if domain.domain_ipaddr %}@ IN A {{ domain.domain_ipaddr }}{% endif %} +{% if domain.domain_ip6addr %}@ IN AAAA {{ domain.domain_ip6addr }}{% endif %} +; SRV records +{% for srv in domain.domainsrvrecord_set.all %}{{ srv.as_record }}{% endfor %} +; A records +{% for a in domain.domainarecord_set.all %}{{ a.as_record }}{% endfor %} +; AAAA records +{% for aaaa in domain.domainaaaarecord_set.all %}{{ aaaa.as_record }}{% endfor %} +; CNAME records +{% for cname in domain.domaincnamerecord_set.all %}{{ cname.name|stringformat:"-20s" }} IN CNAME {{ cname.target }}{% endfor %} +; TXT records +{% for txt in domain.domaintxtrecord_set.all %}{{ txt.as_record }}{% endfor %} +; HOST records +{% for interface in domain.interface_set.all %} +{% if interface.ip4address %}{{ interface.host.hostname|stringformat:"-20s" }} IN A {{ interface.ip4address.address }}{% endif %} +{% for ipv6addr in interface.ip6address_set.all %}{{ interface.host.hostname|stringformat:"-20s" }} IN AAAA {{ ipv6addr.full_address }}{% endfor %} +{% endfor %} \ No newline at end of file