implement domain name validation

- implement domains.forms.relative_domain_validator
- use the validator for domain field validation in
  domains.forms.CreateHostingDomainForm
- use the validator for subdomain field validation in
  websites.forms.AddWebsiteForm
This commit is contained in:
Jan Dittberner 2015-01-27 16:41:44 +01:00
parent 7da5cfe406
commit 7c9509c159
3 changed files with 23 additions and 1 deletions

View file

@ -1,6 +1,7 @@
Changelog Changelog
========= =========
* :support:`-` implement domain name validation
* :feature:`-` implement deletion of websites * :feature:`-` implement deletion of websites
* :feature:`-` implement setup of websites * :feature:`-` implement setup of websites
* :support:`-` add webtasks interface * :support:`-` add webtasks interface

View file

@ -4,6 +4,8 @@ This module defines form classes for domain editing.
""" """
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
import re
from django import forms from django import forms
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -17,6 +19,18 @@ from crispy_forms.layout import (
from .models import HostingDomain from .models import HostingDomain
def relative_domain_validator(value):
"""
This validator ensures that the given value is a valid lowercase
"""
if len(value) > 254:
raise forms.ValidationError(
_('host name too long'), code='too-long')
allowed = re.compile(r"(?!-)[a-z\d-]{1,63}(?<!-)$")
if not all(allowed.match(x) for x in value.split('.')):
raise forms.ValidationError(_('invalid domain name'))
class CreateHostingDomainForm(forms.ModelForm): class CreateHostingDomainForm(forms.ModelForm):
""" """
This form is used to create new HostingDomain instances. This form is used to create new HostingDomain instances.
@ -29,6 +43,7 @@ class CreateHostingDomainForm(forms.ModelForm):
def __init__(self, instance, *args, **kwargs): def __init__(self, instance, *args, **kwargs):
self.hosting_package = kwargs.pop('hostingpackage') self.hosting_package = kwargs.pop('hostingpackage')
super(CreateHostingDomainForm, self).__init__(*args, **kwargs) super(CreateHostingDomainForm, self).__init__(*args, **kwargs)
self.fields['domain'].validators.append(relative_domain_validator)
self.helper = FormHelper() self.helper = FormHelper()
self.helper.form_action = reverse( self.helper.form_action = reverse(
'create_hosting_domain', kwargs={ 'create_hosting_domain', kwargs={

View file

@ -15,6 +15,7 @@ from crispy_forms.layout import (
Submit, Submit,
) )
from domains.forms import relative_domain_validator
from .models import Website from .models import Website
@ -31,7 +32,10 @@ class AddWebsiteForm(forms.ModelForm):
self.hosting_package = kwargs.pop('hostingpackage') self.hosting_package = kwargs.pop('hostingpackage')
self.hosting_domain = kwargs.pop('domain') self.hosting_domain = kwargs.pop('domain')
super(AddWebsiteForm, self).__init__(*args, **kwargs) super(AddWebsiteForm, self).__init__(*args, **kwargs)
if Website.objects.filter(wildcard=True).exists(): self.fields['subdomain'].validators.append(relative_domain_validator)
if Website.objects.filter(
wildcard=True, domain=self.hosting_domain
).exists():
self.fields['wildcard'].widget = forms.HiddenInput() self.fields['wildcard'].widget = forms.HiddenInput()
self.helper = FormHelper() self.helper = FormHelper()
@ -54,6 +58,8 @@ class AddWebsiteForm(forms.ModelForm):
).exists(): ).exists():
raise forms.ValidationError( raise forms.ValidationError(
_('There is already a website for this subdomain')) _('There is already a website for this subdomain'))
relative_domain_validator(
"{0}.{1}".format(data, self.hosting_domain.domain))
return data return data
def save(self, commit=True): def save(self, commit=True):