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) instance = getattr(self, "instance", None) self._create = not (instance and instance.pk) if not self._create: self.fields['name'].disabled = True def clean_name(self): name = self.cleaned_data['name'].lower() if self._create: if not name.endswith("."): name += "." if not name.endswith("dn."): raise forms.ValidationError("Only .dn domains can be registered at this point") if name.count(".") > 2: raise forms.ValidationError("No subdomains can be registered") if not re.match("^[a-z0-9.-]+$", name): raise forms.ValidationError("Only a-z, 0-9 and - are allowed inside the domain name") try: Domain.objects.get(name=name) raise forms.ValidationError("Domain already exists") except Domain.DoesNotExist: pass return name 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) instance = getattr(self, "instance", None) self._create = not (instance and instance.pk) 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) found = False for domain in domains: if domain.name == zone: found = True break if not found: raise forms.ValidationError("This nameserver is not under a domain you control.") return name def clean(self): cleaned_data = super(NameserverForm, self).clean() return cleaned_data