diff --git a/templates/whoisdb/overview.html b/templates/whoisdb/overview.html index 3bd5375..93be762 100644 --- a/templates/whoisdb/overview.html +++ b/templates/whoisdb/overview.html @@ -9,9 +9,44 @@
Header
{% if mnts %} + - {% else %} - You don't have a maintainer! Start by creating a Maintainer and a Person Object! + You have the following Maintainers associated with you + + + + + + + + + + + + {% for mnt in mnts %} + + + + + + + {% endfor %} + +
HandleName?ContactActions
{{ mnt.handle }}{{ mnt.description }}{% for contact in mnt.admin_c.all %}{{ contact }}{% endfor %}
+ + {% else %} + You don't have a maintainer! Start by creating a Maintainer and a Person Object!
{% csrf_token %} diff --git a/whoisdb/models.py b/whoisdb/models.py index f40b513..8e1c2a1 100644 --- a/whoisdb/models.py +++ b/whoisdb/models.py @@ -1,42 +1,88 @@ from django.db import models from dncore.models import User -from .validators import HandleValidator +from .validators import HandleValidator, HandleValidatorWithSuffix class WhoisObject(models.Model): - handle_suffix = "" + 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) - class Meta: - abstract = 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") -class MntdObject(WhoisObject): - mnt_by = models.ManyToManyField(Maintainer) + # 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) @@ -44,10 +90,10 @@ class ASNumber(MntdObject): mnt_lower = models.ManyToManyField(Maintainer, related_name='lower_asnumber_set') -#class Route(MntObject): -# class InetNum(WhoisObject): + handleSuffix = "NET" + PROTO = (('ipv4', 'ipv4'), ('ipv6', 'ipv6')) protocol = models.CharField(max_length=4, choices=PROTO) netmask = models.PositiveIntegerField() diff --git a/whoisdb/validators.py b/whoisdb/validators.py index 0a6fd24..e3a5479 100644 --- a/whoisdb/validators.py +++ b/whoisdb/validators.py @@ -5,11 +5,25 @@ from django.utils.translation import ugettext_lazy as _ import re + @deconstructible class HandleValidator(validators.RegexValidator): - regex = r'^[A-Z]+[0-9]+(-[A-Z]+)' - message = _( - 'Enter a valid handle (all uppercase)' - ) - flags = re.ASCII if six.PY3 else 0 + regex = r'^(?:[A-Z]+[0-9]+(-[A-Z]+)|AUTO)' + message = _( + 'Enter a valid handle (all uppercase)' + ) + flags = re.ASCII if six.PY3 else 0 + + +@deconstructible +class HandleValidatorWithSuffix(validators.RegexValidator): + flags = re.ASCII if six.PY3 else 0 + + def __init__(self, suffix): + self.regex = r'^(?:[A-Z]+[0-9]+-%s|AUTO)' % re.escape(suffix) + print(self.regex) + self.message = _( + 'Enter a valid handle with suffix %s (all uppercase)' % suffix + ) + super(HandleValidatorWithSuffix, self).__init__() diff --git a/whoisdb/views.py b/whoisdb/views.py index c8fc5f3..ed73731 100644 --- a/whoisdb/views.py +++ b/whoisdb/views.py @@ -5,27 +5,42 @@ from django.urls import reverse from .forms import MntForm, ContactForm + @login_required def dbDashboard(request): mnts = request.user.maintainer_set.all() - mntForm = contactForm = None - if request.method == "POST": - mntForm = MntForm(data=request.POST, prefix="mnt_") - contactForm = ContactForm(person=True, data=request.POST, prefix="contact") - if mntForm.is_valid() and contactForm.is_valid(): - print("NOOT") - - #return HttpResponseRedirect(reverse("whoisdb:dashboard")) + if mnts.count() == 0: + mntForm = contactForm = None + if request.method == "POST": + mntForm = MntForm(data=request.POST, prefix="mnt") + contactForm = ContactForm(person=True, data=request.POST, prefix="contact") + if mntForm.is_valid() and contactForm.is_valid(): + mnt = mntForm.save(commit=False) + mnt.handleAuto(request.user.username) + mnt.save() + + contact = contactForm.save(commit=False) + contact.handleAuto() + contact.save() + contact.mnt_by.add(mnt.id) + contact.save() + + mnt.auth.add(request.user.id) + mnt.admin_c.add(contact.id) + mnt.save() + + return HttpResponseRedirect(reverse("whoisdb:dashboard")) + else: + mntForm = MntForm(prefix="mnt", initial={'handle': 'AUTO', 'description': 'Primary maintainer of %s' % request.user.username}) + contactForm = ContactForm(person=True, initial={'handle': 'AUTO', 'name': request.user.username.capitalize()}, prefix='contact') - mntForm = MntForm(prefix="mnt", initial={'handle': 'AUTO', 'description': 'Main maintainer of %s' % request.user.username}) - contactForm = ContactForm(person=True, initial={'handle': 'AUTO'}, prefix='contact_') return render(request, "whoisdb/overview.html", {"mnts": mnts, "mntForm": mntForm, "contactForm": contactForm}) + def manageMnt(request, mnt=None): if mnt: # object or 404 pass return render(request, "whoisdb/overview.html", {}) -