models.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # -*- coding: utf-8 -*-
  2. from django.db import models
  3. from django.core.exceptions import ValidationError
  4. from netfields import InetAddressField, NetManager
  5. from netaddr import IPAddress
  6. # TODO: validate DNS names with this regex
  7. REGEX = r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}|[A-Z0-9-]{2,})\.?$'
  8. class NameServer(models.Model):
  9. dns_name = models.CharField(max_length=255,
  10. help_text="Example: ns1.example.com")
  11. description = models.CharField(max_length=255,
  12. verbose_name="human-readable name of the nameserver",
  13. help_text="Example: My first name server")
  14. def __unicode__(self):
  15. return "{} ({})".format(self.description, self.dns_name)
  16. class ReverseDNSEntry(models.Model):
  17. ip = InetAddressField(unique=True)
  18. reverse = models.CharField(max_length=255,
  19. verbose_name="hostname to associate to the IP")
  20. ip_subnet = models.ForeignKey('resources.IPSubnet')
  21. objects = NetManager()
  22. def clean(self):
  23. if self.reverse:
  24. # Check that the reverse ends with a "." (add it if necessary)
  25. if not self.reverse.endswith('.'):
  26. self.reverse += '.'
  27. if self.ip:
  28. if not self.ip in self.ip_subnet.inet:
  29. raise ValidationError('IP address must be included in the IP subnet.')
  30. def __unicode__(self):
  31. return u"{} → {}".format(self.ip, self.reverse)