Author: Honza_Kral
Date: 2009-06-02 21:39:02 -0500 (Tue, 02 Jun 2009)
New Revision: 10910
Modified:
django/branches/soc2009/model-validation/django/core/validators.py
django/branches/soc2009/model-validation/tests/modeltests/validators/tests.py
Log:
[soc2009/model-validation] added validate_email validator
Modified: django/branches/soc2009/model-validation/django/core/validators.py
===================================================================
--- django/branches/soc2009/model-validation/django/core/validators.py
2009-06-03 02:38:39 UTC (rev 10909)
+++ django/branches/soc2009/model-validation/django/core/validators.py
2009-06-03 02:39:02 UTC (rev 10910)
@@ -1,4 +1,8 @@
+import re
+
from django.core.exceptions import ValidationError
+from django.utils.translation import ugettext_lazy as _
+from django.utils.encoding import smart_unicode
# These values, if given to to_python(), will trigger the self.required check.
EMPTY_VALUES = (None, '')
@@ -8,3 +12,12 @@
int(value)
except (ValueError, TypeError), e:
raise ValidationError('')
+
+email_re = re.compile(
+ r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" #
dot-atom
+
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"'
# quoted-string
+ r')@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain
+
+def validate_email(value, all_values={}, model_instance=None):
+ if not email_re.search(smart_unicode(value)):
+ raise ValidationError(_(u'Enter a valid e-mail address.'))
Modified:
django/branches/soc2009/model-validation/tests/modeltests/validators/tests.py
===================================================================
---
django/branches/soc2009/model-validation/tests/modeltests/validators/tests.py
2009-06-03 02:38:39 UTC (rev 10909)
+++
django/branches/soc2009/model-validation/tests/modeltests/validators/tests.py
2009-06-03 02:39:02 UTC (rev 10910)
@@ -1,7 +1,7 @@
from unittest import TestCase
from django.core.exceptions import ValidationError
-from django.core.validators import validate_integer
+from django.core.validators import validate_integer, validate_email
class TestSimpleValidators(TestCase):
pass
@@ -13,9 +13,16 @@
(validate_integer, -42.5, None),
(validate_integer, None, ValidationError),
(validate_integer, 'a', ValidationError),
+ (validate_email, '[email protected]', None),
+ (validate_email, '[email protected]', None),
+ (validate_email, None, ValidationError),
+ (validate_email, '', ValidationError),
+ (validate_email, 'abc', ValidationError),
+ (validate_email, 'a @x.cz', ValidationError),
+ (validate_email, 'something@@somewhere.com', ValidationError),
)
-def get_simple_test_func(expected, value, num):
+def get_simple_test_func(validator, expected, value, num):
if isinstance(expected, type) and issubclass(expected, ValidationError):
test_mask = 'test_%s_raises_error_%d'
def test_func(self):
@@ -30,4 +37,4 @@
test_counter = {}
for validator, value, expected in SIMPLE_VALIDATORS_VALUES:
num = test_counter[validator] = test_counter.setdefault(validator, 0) + 1
- setattr(TestSimpleValidators, *get_simple_test_func(expected, value, num))
+ setattr(TestSimpleValidators, *get_simple_test_func(validator, expected,
value, num))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---