commit 99d66413e937620049cd8a1e4115d3d12f719b91
Author: Claude Paroz <claude@2xlibre.net>
Date:   Sat Jul 9 22:53:42 2011 +0200

    Validate email addresses with localhost as domain

diff --git a/django/core/validators.py b/django/core/validators.py
index de415d6..d90956a 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -140,29 +140,50 @@ def validate_integer(value):
     except (ValueError, TypeError):
         raise ValidationError('')
 
-class EmailValidator(RegexValidator):
+class EmailValidator(object):
+    message = _(u'Enter a valid e-mail address.')
+    code = 'invalid'
+    user_regex = 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
+        re.IGNORECASE)
+    domain_regex = re.compile(
+        r'(^(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)'  # domain
+        # literal form, ipv4 address (SMTP 4.1.3)
+        r'|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$',
+        re.IGNORECASE)
+    domain_whitelist = ['localhost']
+
+    def __init__(self, message=None, code=None, whitelist=None):
+        if message is not None:
+            self.message = message
+        if code is not None:
+            self.code = code
+        if whitelist is not None:
+            self.domain_whitelist = whitelist
 
     def __call__(self, value):
-        try:
-            super(EmailValidator, self).__call__(value)
-        except ValidationError, e:
-            # Trivial case failed. Try for possible IDN domain-part
-            if value and u'@' in value:
-                parts = value.split(u'@')
+        if value and u'@' in value:
+            user_part, domain_part = value.split(u'@', 1)
+            res = self.user_regex.search(smart_unicode(user_part))
+            if not self.user_regex.search(smart_unicode(user_part)):
+                raise ValidationError(self.message, code=self.code)
+            if (not smart_unicode(domain_part) in self.domain_whitelist and
+                    not self.domain_regex.search(smart_unicode(domain_part))):
+                # Try for possible IDN domain-part
                 try:
-                    parts[-1] = parts[-1].encode('idna')
+                    domain_part = domain_part.encode('idna')
+                    if not self.domain_regex.search(smart_unicode(domain_part)):
+                        raise ValidationError(self.message, code=self.code)
+                    else:
+                        return
                 except UnicodeError:
-                    raise e
-                super(EmailValidator, self).__call__(u'@'.join(parts))
-            else:
-                raise
+                    pass
+                raise ValidationError(self.message, code=self.code)
+        else:
+            raise ValidationError(self.message, code=self.code)
 
-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-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)'  # domain
-    r'|\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$', re.IGNORECASE)  # literal form, ipv4 address (SMTP 4.1.3)
-validate_email = EmailValidator(email_re, _(u'Enter a valid e-mail address.'), 'invalid')
+validate_email = EmailValidator()
 
 slug_re = re.compile(r'^[-\w]+$')
 validate_slug = RegexValidator(slug_re, _(u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."), 'invalid')
diff --git a/tests/modeltests/validators/tests.py b/tests/modeltests/validators/tests.py
index 9e254b9..ed8bdfb 100644
--- a/tests/modeltests/validators/tests.py
+++ b/tests/modeltests/validators/tests.py
@@ -23,6 +23,7 @@ TEST_DATA = (
     (validate_email, 'email@here.com', None),
     (validate_email, 'weirder-email@here.and.there.com', None),
     (validate_email, 'email@[127.0.0.1]', None),
+    (validate_email, 'email@localhost', None),
 
     (validate_email, None, ValidationError),
     (validate_email, '', ValidationError),
