from django.db import models from dncore.models import User from .validators import HandleValidator, HandleValidatorWithSuffix class WhoisObject(models.Model): class Meta: abstract = True handleSuffix = "" handle = models.SlugField(max_length=32, unique=True, verbose_name='handle', validators=[HandleValidator()]) created = models.DateTimeField(auto_now_add=True) last_modified = models.DateTimeField(auto_now_add=True) def __str__(self): return self.handle def genHandle(self, main=None): if not main: main = self.name prefix = "" if " " in main: parts = main.split(" ") prefix = "".join(map(lambda _x: _x[0], parts)) if len(prefix) < 3 and len(parts[-1]) > 1: prefix += parts[-1][1:4 - len(prefix)] else: prefix = main[0:3] prefix = prefix.upper() i = 1 handle = "%s%%d-%s" % (prefix, self.handleSuffix) while True: try: prefix self.__class__.objects.get(handle=handle % i) i += 1 except self.DoesNotExist: break return handle % i def handleAuto(self, name=None): if self.handle == "AUTO": self.handle = self.genHandle(name) class Maintainer(WhoisObject): handleSuffix = "MNT" auth = models.ManyToManyField(User) handle = models.SlugField(max_length=32, unique=True, verbose_name='handle', validators=[HandleValidatorWithSuffix('MNT')]) description = models.CharField(max_length=64, blank=True) admin_c = models.ManyToManyField("Contact") # autoInclude = models.BooleanField(default=True) class MntdObject(WhoisObject): class Meta: abstract = True mnt_by = models.ManyToManyField(Maintainer) class Contact(MntdObject): handleSuffix = "DN" TYPE = (('person', 'person'), ('role', 'role')) name = models.CharField(max_length=128) class ASBlock(MntdObject): handleSuffix = "ASB" parent_block = models.ForeignKey("ASBlock", models.CASCADE, null=True, blank=True, default=None) description = models.CharField(max_length=64, blank=True) class ASNumber(MntdObject): handleSuffix = "AS" number = models.PositiveIntegerField(unique=True, db_index=True) volatile = models.BooleanField(default=False) asblock = models.ForeignKey(ASBlock, models.CASCADE) description = models.CharField(max_length=64, blank=True) mnt_lower = models.ManyToManyField(Maintainer, related_name='lower_asnumber_set') class InetNum(WhoisObject): handleSuffix = "NET" PROTO = (('ipv4', 'ipv4'), ('ipv6', 'ipv6')) protocol = models.CharField(max_length=4, choices=PROTO) netmask = models.PositiveIntegerField() parent_range = models.ForeignKey("InetNum", models.CASCADE, null=True, blank=True, default=None) description = models.CharField(max_length=64, blank=True) mnt_lower = models.ManyToManyField(Maintainer)