Ticket #4833: 4833-2.diff

File 4833-2.diff, 3.5 KB (added by Claude Paroz, 13 years ago)

Validate email addresses with localhost as domain

  • django/core/validators.py

    diff --git a/django/core/validators.py b/django/core/validators.py
    index 458f419..0658971 100644
    a b def validate_integer(value):  
    116116        raise ValidationError('')
    117117
    118118class 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']
    119124
    120125    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
    128134                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)
    130138                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)
    135143
    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)
     144email_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)
    141156validate_email = EmailValidator(email_re, _(u'Enter a valid e-mail address.'), 'invalid')
    142157
    143158slug_re = re.compile(r'^[-\w]+$')
  • tests/modeltests/validators/tests.py

    diff --git a/tests/modeltests/validators/tests.py b/tests/modeltests/validators/tests.py
    index 9e254b9..ed8bdfb 100644
    a b TEST_DATA = (  
    2323    (validate_email, 'email@here.com', None),
    2424    (validate_email, 'weirder-email@here.and.there.com', None),
    2525    (validate_email, 'email@[127.0.0.1]', None),
     26    (validate_email, 'email@localhost', None),
    2627
    2728    (validate_email, None, ValidationError),
    2829    (validate_email, '', ValidationError),
Back to Top