diff --git a/django/core/validators.py b/django/core/validators.py
index 458f419..0658971 100644
a
|
b
|
def validate_integer(value):
|
116 | 116 | raise ValidationError('') |
117 | 117 | |
118 | 118 | class EmailValidator(RegexValidator): |
| 119 | """ |
| 120 | EmailValidator takes a tuple of regexes on init, one for user part and one |
| 121 | for domain part |
| 122 | """ |
| 123 | domain_whitelist = ['localhost'] |
119 | 124 | |
120 | 125 | def __call__(self, value): |
121 | | try: |
122 | | super(EmailValidator, self).__call__(value) |
123 | | except ValidationError, e: |
124 | | # Trivial case failed. Try for possible IDN domain-part |
125 | | if value and u'@' in value: |
126 | | parts = value.split(u'@') |
127 | | domain_part = parts[-1] |
| 126 | if value and u'@' in value: |
| 127 | user_part, domain_part = value.split(u'@', 1) |
| 128 | res = self.regex[0].search(smart_unicode(user_part)) |
| 129 | if not self.regex[0].search(smart_unicode(user_part)): |
| 130 | raise ValidationError(self.message, code=self.code) |
| 131 | if (not smart_unicode(domain_part) in self.domain_whitelist and |
| 132 | not self.regex[1].search(smart_unicode(domain_part))): |
| 133 | # Try for possible IDN domain-part |
128 | 134 | try: |
129 | | parts[-1] = parts[-1].encode('idna') |
| 135 | domain_part = domain_part.encode('idna') |
| 136 | if not self.regex[1].search(smart_unicode(domain_part)): |
| 137 | raise ValidationError(self.message, code=self.code) |
130 | 138 | except UnicodeError: |
131 | | raise e |
132 | | super(EmailValidator, self).__call__(u'@'.join(parts)) |
133 | | else: |
134 | | raise |
| 139 | pass |
| 140 | raise ValidationError(self.message, code=self.code) |
| 141 | else: |
| 142 | raise ValidationError(self.message, code=self.code) |
135 | 143 | |
136 | | email_re = re.compile( |
137 | | r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom |
138 | | r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string |
139 | | r')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)' # domain |
140 | | 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) |
| 144 | email_re = ( |
| 145 | re.compile( |
| 146 | r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$" # dot-atom |
| 147 | r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"$)', # quoted-string |
| 148 | re.IGNORECASE |
| 149 | ), |
| 150 | re.compile( |
| 151 | r'(^(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)' # domain |
| 152 | r'|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$', # literal form, ipv4 address (SMTP 4.1.3) |
| 153 | re.IGNORECASE |
| 154 | ) |
| 155 | ) |
141 | 156 | validate_email = EmailValidator(email_re, _(u'Enter a valid e-mail address.'), 'invalid') |
142 | 157 | |
143 | 158 | slug_re = re.compile(r'^[-\w]+$') |
diff --git a/tests/modeltests/validators/tests.py b/tests/modeltests/validators/tests.py
index 9e254b9..ed8bdfb 100644
a
|
b
|
TEST_DATA = (
|
23 | 23 | (validate_email, 'email@here.com', None), |
24 | 24 | (validate_email, 'weirder-email@here.and.there.com', None), |
25 | 25 | (validate_email, 'email@[127.0.0.1]', None), |
| 26 | (validate_email, 'email@localhost', None), |
26 | 27 | |
27 | 28 | (validate_email, None, ValidationError), |
28 | 29 | (validate_email, '', ValidationError), |