Ticket #3989: email_re.2.diff

File email_re.2.diff, 4.4 KB (added by Vinay Sajip <vinay_sajip@…>, 17 years ago)

Updated patch with additions to newforms/fields.py and docs/model_api.txt

  • django/core/validators.py

     
    2121ansi_date_re = re.compile('^%s$' % _datere)
    2222ansi_time_re = re.compile('^%s$' % _timere)
    2323ansi_datetime_re = re.compile('^%s %s$' % (_datere, _timere))
     24
     25ADDR_SPEC = """
     26((?:
     27    [-!#$%&'*+/=?^_`{}|~0-9A-Z]+(?:\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)* # dot-atom
     28)|(?:
     29    "(?:[\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*" # quoted-string
     30))@(
     31    (?:[A-Z0-9-]+\.)+[A-Z]{2,6} #domain
     32)
     33"""
     34
    2435email_re = re.compile(
    25     r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"  # dot-atom
    26     r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
    27     r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE)  # domain
     36    "^(?:" + ADDR_SPEC + ")|(?:\w[\w ]*)<" + ADDR_SPEC + ">$", re.VERBOSE | re.IGNORECASE)
     37
    2838integer_re = re.compile(r'^-?\d+$')
    2939ip4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
    3040phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
     
    141151        date(year, month, day)
    142152    except ValueError, e:
    143153        msg = gettext('Invalid date: %s') % gettext(str(e))
    144         raise ValidationError, msg   
     154        raise ValidationError, msg
    145155
    146156def isValidANSIDate(field_data, all_data):
    147157    if not ansi_date_re.search(field_data):
     
    244254            raise ValidationError, _("The URL %s is a broken link.") % field_data
    245255    except: # urllib2.URLError, httplib.InvalidURL, etc.
    246256        raise ValidationError, _("The URL %s is a broken link.") % field_data
    247        
     257
    248258def isValidUSState(field_data, all_data):
    249259    "Checks that the given string is a valid two-letter U.S. state abbreviation"
    250260    states = ['AA', 'AE', 'AK', 'AL', 'AP', 'AR', 'AS', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'FM', 'GA', 'GU', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MH', 'MI', 'MN', 'MO', 'MP', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'PW', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VI', 'VT', 'WA', 'WI', 'WV', 'WY']
     
    373383            self.error_message = error_message
    374384
    375385    def __call__(self, field_data, all_data):
    376         # Try to make the value numeric. If this fails, we assume another 
     386        # Try to make the value numeric. If this fails, we assume another
    377387        # validator will catch the problem.
    378388        try:
    379389            val = float(field_data)
    380390        except ValueError:
    381391            return
    382            
     392
    383393        # Now validate
    384394        if self.lower and self.upper and (val < self.lower or val > self.upper):
    385395            raise ValidationError(self.error_message)
  • django/newforms/fields.py

     
    260260            raise ValidationError(self.error_message)
    261261        return value
    262262
     263ADDR_SPEC = """
     264((?:
     265    [-!#$%&'*+/=?^_`{}|~0-9A-Z]+(?:\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)* # dot-atom
     266)|(?:
     267    "(?:[\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*" # quoted-string
     268))@(
     269    (?:[A-Z0-9-]+\.)+[A-Z]{2,6} #domain
     270)
     271"""
     272
    263273email_re = re.compile(
    264     r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"  # dot-atom
    265     r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
    266     r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE)  # domain
     274    "^(?:" + ADDR_SPEC + ")|(?:\w[\w ]*)<" + ADDR_SPEC + ">$", re.VERBOSE | re.IGNORECASE)
    267275
    268276class EmailField(RegexField):
    269277    def __init__(self, max_length=None, min_length=None, *args, **kwargs):
  • docs/model-api.txt

     
    189189
    190190A ``CharField`` that checks that the value is a valid e-mail address.
    191191This doesn't accept ``maxlength``; its ``maxlength`` is automatically set to
    192 75.
     19275. In addition to the expected ``joe.bloggs@some.domain.com`` address format,
     193the field also accepts email addresses with display names, such as
     194``Joe Bloggs <joe.bloggs@some.domain.com>``.
    193195
    194196``FileField``
    195197~~~~~~~~~~~~~
Back to Top