models.py 1.2 KB

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