dnmgmt/domains/forms.py

90 lines
2.2 KiB
Python
Raw Normal View History

2017-03-14 20:18:57 +01:00
from django import forms
from whoisdb.forms import MntFormMixin
from .models import Domain, Nameserver
import re
class DomainForm(MntFormMixin, forms.ModelForm):
class Meta:
model = Domain
fields = ['name', 'nameservers', 'mnt_by', 'admin_c']
def __init__(self, user, *args, **kwargs):
self._user = user
super(DomainForm, self).__init__(*args, **kwargs)
2017-03-15 02:22:08 +01:00
instance = getattr(self, "instance", None)
self._create = not (instance and instance.pk)
if not self._create:
self.fields['name'].disabled = True
2017-03-14 20:18:57 +01:00
def clean_name(self):
name = self.cleaned_data['name'].lower()
2017-03-15 02:22:08 +01:00
if self._create:
if not name.endswith("."):
name += "."
2017-03-14 20:18:57 +01:00
2017-03-15 02:22:08 +01:00
if not name.endswith("dn."):
raise forms.ValidationError("Only .dn domains can be registered at this point")
2017-03-14 20:18:57 +01:00
2017-03-15 02:22:08 +01:00
if name.count(".") > 2:
raise forms.ValidationError("No subdomains can be registered")
2017-03-14 20:18:57 +01:00
2017-03-15 02:22:08 +01:00
if not re.match("^[a-z0-9.-]+$", name):
raise forms.ValidationError("Only a-z, 0-9 and - are allowed inside the domain name")
2017-03-14 20:18:57 +01:00
2017-03-15 02:22:08 +01:00
try:
Domain.objects.get(name=name)
raise forms.ValidationError("Domain already exists")
except Domain.DoesNotExist:
pass
2017-03-14 20:18:57 +01:00
return name
2017-03-15 02:22:08 +01:00
2017-03-14 20:18:57 +01:00
class NameserverForm(MntFormMixin, forms.ModelForm):
class Meta:
model = Nameserver
fields = ['name', 'glueIPv4', 'glueIPv6', 'mnt_by', 'admin_c']
def __init__(self, user, *args, **kwargs):
self._user = user
super(NameserverForm, self).__init__(*args, **kwargs)
2017-03-15 02:22:08 +01:00
instance = getattr(self, "instance", None)
self._create = not (instance and instance.pk)
2017-03-14 20:18:57 +01:00
def clean_name(self):
name = self.cleaned_data['name'].lower().strip()
if not name.endswith("."):
name += "."
if name.count(".") <= 2:
raise forms.ValidationError("Nameserver must be inside a domain (e.g. ns1.noot.dn.)")
zone = ".".join(name.split(".")[-3:])
mnts = self._user.maintainer_set.all()
domains = Domain.objects.filter(mnt_by__in=mnts)
2017-03-15 02:22:08 +01:00
found = False
2017-03-14 20:18:57 +01:00
for domain in domains:
2017-03-15 02:22:08 +01:00
if domain.name == zone:
found = True
break
if not found:
raise forms.ValidationError("This nameserver is not under a domain you control.")
2017-03-14 20:18:57 +01:00
return name
def clean(self):
cleaned_data = super(NameserverForm, self).clean()
return cleaned_data