models.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # -*- coding: utf-8 -*-
  2. from django.db import models
  3. from django.core.exceptions import ValidationError
  4. from django.core.validators import MinValueValidator
  5. from netfields import InetAddressField, NetManager
  6. from netaddr import IPAddress
  7. import ldapdb.models
  8. from ldapdb.models.fields import CharField, IntegerField, ListField
  9. # TODO: validate DNS names with this regex
  10. REGEX = r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}|[A-Z0-9-]{2,})\.?$'
  11. class NameServer(models.Model):
  12. # TODO: signal to IPSubnet when we are modified, so that is saves the
  13. # result into LDAP. Actually, better: build a custom M2M relation
  14. # between NameServer and IPSubnet (see Capslock), and save in LDAP
  15. # there.
  16. dns_name = models.CharField(max_length=255,
  17. help_text="Example: ns1.example.com")
  18. description = models.CharField(max_length=255, blank=True,
  19. verbose_name="human-readable name of the nameserver",
  20. help_text="Example: My first name server")
  21. # TODO: don't allow NULL owner (this is only here to allow migrations)
  22. owner = models.ForeignKey("members.Member", null=True)
  23. def __unicode__(self):
  24. return "{} ({})".format(self.description, self.dns_name) if self.description else self.dns_name
  25. class ReverseDNSEntry(models.Model):
  26. ip = InetAddressField(unique=True)
  27. reverse = models.CharField(max_length=255,
  28. verbose_name="hostname to associate to the IP")
  29. ttl = models.IntegerField(default=3600, verbose_name="TTL",
  30. help_text="in seconds",
  31. validators=[MinValueValidator(60)])
  32. ip_subnet = models.ForeignKey('resources.IPSubnet')
  33. objects = NetManager()
  34. def clean(self):
  35. if self.reverse:
  36. # Check that the reverse ends with a "." (add it if necessary)
  37. if not self.reverse.endswith('.'):
  38. self.reverse += '.'
  39. if self.ip:
  40. if not self.ip in self.ip_subnet.inet:
  41. raise ValidationError('IP address must be included in the IP subnet.')
  42. def __unicode__(self):
  43. return u"{} → {}".format(self.ip, self.reverse)