diff --git a/django/core/validators.py b/django/core/validators.py
index 51ea82f..ec6ace8 100644
a
|
b
|
def validate_integer(value):
|
138 | 138 | raise ValidationError('') |
139 | 139 | |
140 | 140 | class EmailValidator(RegexValidator): |
| 141 | """ |
| 142 | EmailValidator takes a tuple of regexes on init, one for user part and one |
| 143 | for domain part |
| 144 | """ |
| 145 | domain_whitelist = ['localhost'] |
141 | 146 | |
142 | 147 | def __call__(self, value): |
143 | | try: |
144 | | super(EmailValidator, self).__call__(value) |
145 | | except ValidationError, e: |
146 | | # Trivial case failed. Try for possible IDN domain-part |
147 | | if value and u'@' in value: |
148 | | parts = value.split(u'@') |
| 148 | if value and u'@' in value: |
| 149 | user_part, domain_part = value.split(u'@', 1) |
| 150 | res = self.regex[0].search(smart_unicode(user_part)) |
| 151 | if not self.regex[0].search(smart_unicode(user_part)): |
| 152 | raise ValidationError(self.message, code=self.code) |
| 153 | if (not smart_unicode(domain_part) in self.domain_whitelist and |
| 154 | not self.regex[1].search(smart_unicode(domain_part))): |
| 155 | # Try for possible IDN domain-part |
149 | 156 | try: |
150 | | parts[-1] = parts[-1].encode('idna') |
| 157 | domain_part = domain_part.encode('idna') |
| 158 | if not self.regex[1].search(smart_unicode(domain_part)): |
| 159 | raise ValidationError(self.message, code=self.code) |
| 160 | else: |
| 161 | return |
151 | 162 | except UnicodeError: |
152 | | raise e |
153 | | super(EmailValidator, self).__call__(u'@'.join(parts)) |
154 | | else: |
155 | | raise |
| 163 | pass |
| 164 | raise ValidationError(self.message, code=self.code) |
| 165 | else: |
| 166 | raise ValidationError(self.message, code=self.code) |
156 | 167 | |
157 | | email_re = re.compile( |
158 | | r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom |
159 | | r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string |
160 | | r')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)' # domain |
161 | | 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) |
| 168 | email_re = ( |
| 169 | re.compile( |
| 170 | r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$" # dot-atom |
| 171 | r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"$)', # quoted-string |
| 172 | re.IGNORECASE |
| 173 | ), |
| 174 | re.compile( |
| 175 | r'(^(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$)' # domain |
| 176 | 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) |
| 177 | re.IGNORECASE |
| 178 | ) |
| 179 | ) |
162 | 180 | validate_email = EmailValidator(email_re, _(u'Enter a valid e-mail address.'), 'invalid') |
163 | 181 | |
164 | 182 | 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), |