mirror of
https://github.com/django/django.git
synced 2025-07-07 11:19:12 +00:00
[soc2009/model-validation] SlugField and IPAddressField now use validators.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11191 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
cb7d3f2a0e
commit
c1093ca87f
@ -22,6 +22,24 @@ def validate_email(value):
|
||||
if not email_re.search(smart_unicode(value)):
|
||||
raise ValidationError(_(u'Enter a valid e-mail address.'), code='invalid')
|
||||
|
||||
slug_re = re.compile(r'^[-\w]+$')
|
||||
|
||||
def validate_slug(value):
|
||||
if not slug_re.search(smart_unicode(value)):
|
||||
raise ValidationError(
|
||||
_(u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."),
|
||||
code='invalid'
|
||||
)
|
||||
|
||||
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}$')
|
||||
|
||||
def validate_ipv4_address(value):
|
||||
if not ipv4_re.search(smart_unicode(value)):
|
||||
raise ValidationError(
|
||||
_(u'Enter a valid IPv4 address.'),
|
||||
code="invalid"
|
||||
)
|
||||
|
||||
class MaxValueValidator(object):
|
||||
def __init__(self, max_value):
|
||||
self.max_value = max_value
|
||||
|
@ -914,23 +914,17 @@ class SplitDateTimeField(MultiValueField):
|
||||
return datetime.datetime.combine(*data_list)
|
||||
return None
|
||||
|
||||
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}$')
|
||||
|
||||
class IPAddressField(RegexField):
|
||||
class IPAddressField(CharField):
|
||||
default_error_messages = {
|
||||
'invalid': _(u'Enter a valid IPv4 address.'),
|
||||
}
|
||||
default_validators = [validators.validate_ipv4_address]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(IPAddressField, self).__init__(ipv4_re, *args, **kwargs)
|
||||
|
||||
slug_re = re.compile(r'^[-\w]+$')
|
||||
|
||||
class SlugField(RegexField):
|
||||
class SlugField(CharField):
|
||||
default_error_messages = {
|
||||
'invalid': _(u"Enter a valid 'slug' consisting of letters, numbers,"
|
||||
u" underscores or hyphens."),
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SlugField, self).__init__(slug_re, *args, **kwargs)
|
||||
default_validators = [validators.validate_slug]
|
||||
|
@ -1,7 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from unittest import TestCase
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import validate_integer, validate_email, RequiredIfOtherFieldBlank
|
||||
from django.core.validators import (
|
||||
validate_integer, validate_email, RequiredIfOtherFieldBlank,
|
||||
validate_slug, validate_ipv4_address
|
||||
)
|
||||
|
||||
class TestSimpleValidators(TestCase):
|
||||
pass
|
||||
@ -12,15 +16,42 @@ SIMPLE_VALIDATORS_VALUES = (
|
||||
(validate_integer, '-42', None),
|
||||
(validate_integer, -42, None),
|
||||
(validate_integer, -42.5, None),
|
||||
|
||||
(validate_integer, None, ValidationError),
|
||||
(validate_integer, 'a', ValidationError),
|
||||
|
||||
|
||||
(validate_email, 'email@here.com', None),
|
||||
(validate_email, 'weirder-email@here.and.there.com', None),
|
||||
|
||||
(validate_email, None, ValidationError),
|
||||
(validate_email, '', ValidationError),
|
||||
(validate_email, 'abc', ValidationError),
|
||||
(validate_email, 'a @x.cz', ValidationError),
|
||||
(validate_email, 'something@@somewhere.com', ValidationError),
|
||||
|
||||
|
||||
(validate_slug, 'slug-ok', None),
|
||||
(validate_slug, 'longer-slug-still-ok', None),
|
||||
(validate_slug, '--------', None),
|
||||
(validate_slug, 'nohyphensoranything', None),
|
||||
|
||||
(validate_slug, '', ValidationError),
|
||||
(validate_slug, ' text ', ValidationError),
|
||||
(validate_slug, ' ', ValidationError),
|
||||
(validate_slug, 'some@mail.com', ValidationError),
|
||||
(validate_slug, '你好', ValidationError),
|
||||
(validate_slug, '\n', ValidationError),
|
||||
|
||||
|
||||
(validate_ipv4_address, '1.1.1.1', None),
|
||||
(validate_ipv4_address, '255.0.0.0', None),
|
||||
(validate_ipv4_address, '0.0.0.0', None),
|
||||
|
||||
(validate_ipv4_address, '256.1.1.1', ValidationError),
|
||||
(validate_ipv4_address, '25.1.1.', ValidationError),
|
||||
(validate_ipv4_address, '25,1,1,1', ValidationError),
|
||||
(validate_ipv4_address, '25.1 .1.1', ValidationError),
|
||||
)
|
||||
|
||||
def get_simple_test_func(validator, expected, value, num):
|
||||
|
Loading…
x
Reference in New Issue
Block a user