mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Fixed #811 -- Added support for IPv6 to forms and model fields. Many thanks to Erik Romijn.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16366 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -5,6 +5,7 @@ import urlparse
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.encoding import smart_unicode
|
||||
from django.utils.ipv6 import is_valid_ipv6_address
|
||||
|
||||
# These values, if given to validate(), will trigger the self.required check.
|
||||
EMPTY_VALUES = (None, '', [], (), {})
|
||||
@@ -145,6 +146,41 @@ validate_slug = RegexValidator(slug_re, _(u"Enter a valid 'slug' consisting of l
|
||||
ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
|
||||
validate_ipv4_address = RegexValidator(ipv4_re, _(u'Enter a valid IPv4 address.'), 'invalid')
|
||||
|
||||
def validate_ipv6_address(value):
|
||||
if not is_valid_ipv6_address(value):
|
||||
raise ValidationError(_(u'Enter a valid IPv6 address.'), code='invalid')
|
||||
|
||||
def validate_ipv46_address(value):
|
||||
try:
|
||||
validate_ipv4_address(value)
|
||||
except ValidationError:
|
||||
try:
|
||||
validate_ipv6_address(value)
|
||||
except ValidationError:
|
||||
raise ValidationError(_(u'Enter a valid IPv4 or IPv6 address.'), code='invalid')
|
||||
|
||||
ip_address_validator_map = {
|
||||
'both': ([validate_ipv46_address], _('Enter a valid IPv4 or IPv6 address.')),
|
||||
'ipv4': ([validate_ipv4_address], _('Enter a valid IPv4 address.')),
|
||||
'ipv6': ([validate_ipv6_address], _('Enter a valid IPv6 address.')),
|
||||
}
|
||||
|
||||
def ip_address_validators(protocol, unpack_ipv4):
|
||||
"""
|
||||
Depending on the given parameters returns the appropriate validators for
|
||||
the GenericIPAddressField.
|
||||
|
||||
This code is here, because it is exactly the same for the model and the form field.
|
||||
"""
|
||||
if protocol != 'both' and unpack_ipv4:
|
||||
raise ValueError(
|
||||
"You can only use `unpack_ipv4` if `protocol` is set to 'both'")
|
||||
try:
|
||||
return ip_address_validator_map[protocol.lower()]
|
||||
except KeyError:
|
||||
raise ValueError("The protocol '%s' is unknown. Supported: %s"
|
||||
% (protocol, ip_address_validator_map.keys()))
|
||||
|
||||
comma_separated_int_list_re = re.compile('^[\d,]+$')
|
||||
validate_comma_separated_integer_list = RegexValidator(comma_separated_int_list_re, _(u'Enter only digits separated by commas.'), 'invalid')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user