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:
parent
7da5cfe406
commit
7c9509c159
3 changed files with 23 additions and 1 deletions
|
@ -4,6 +4,8 @@ This module defines form classes for domain editing.
|
|||
"""
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import re
|
||||
|
||||
from django import forms
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.translation import ugettext as _
|
||||
|
@ -17,6 +19,18 @@ from crispy_forms.layout import (
|
|||
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):
|
||||
"""
|
||||
This form is used to create new HostingDomain instances.
|
||||
|
@ -29,6 +43,7 @@ class CreateHostingDomainForm(forms.ModelForm):
|
|||
def __init__(self, instance, *args, **kwargs):
|
||||
self.hosting_package = kwargs.pop('hostingpackage')
|
||||
super(CreateHostingDomainForm, self).__init__(*args, **kwargs)
|
||||
self.fields['domain'].validators.append(relative_domain_validator)
|
||||
self.helper = FormHelper()
|
||||
self.helper.form_action = reverse(
|
||||
'create_hosting_domain', kwargs={
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue