Ticket #6092: 6092.2.diff

File 6092.2.diff, 4.8 KB (added by floguy, 17 years ago)

Updated patch to be more idiomatic Python and to require a compiled regular expression object.

  • django/db/models/fields/__init__.py

     
    710710class EmailField(CharField):
    711711    def __init__(self, *args, **kwargs):
    712712        kwargs['max_length'] = kwargs.get('max_length', 75)
     713        self.regex = kwargs.get('regex')
    713714        CharField.__init__(self, *args, **kwargs)
    714715
    715716    def get_manipulator_field_objs(self):
    716717        return [oldforms.EmailField]
    717718
    718719    def validate(self, field_data, all_data):
    719         validators.isValidEmail(field_data, all_data)
     720        validators.isValidEmail(field_data, all_data, regex=self.regex)
    720721
    721722    def formfield(self, **kwargs):
    722         defaults = {'form_class': forms.EmailField}
     723        defaults = {'form_class': forms.EmailField, 'regex': self.regex}
    723724        defaults.update(kwargs)
    724725        return super(EmailField, self).formfield(**defaults)
    725726
     
    10881089        return super(TimeField, self).formfield(**defaults)
    10891090
    10901091class URLField(CharField):
    1091     def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
     1092    def __init__(self, verbose_name=None, name=None, verify_exists=True, regex=None, **kwargs):
    10921093        kwargs['max_length'] = kwargs.get('max_length', 200)
    10931094        if verify_exists:
    10941095            kwargs.setdefault('validator_list', []).append(validators.isExistingURL)
    10951096        self.verify_exists = verify_exists
     1097        self.regex = regex
    10961098        CharField.__init__(self, verbose_name, name, **kwargs)
    10971099
    10981100    def get_manipulator_field_objs(self):
    10991101        return [oldforms.URLField]
    11001102
    11011103    def formfield(self, **kwargs):
    1102         defaults = {'form_class': forms.URLField, 'verify_exists': self.verify_exists}
     1104        defaults = {'form_class': forms.URLField, 'verify_exists': self.verify_exists,
     1105            'regex': self.regex}
    11031106        defaults.update(kwargs)
    11041107        return super(URLField, self).formfield(**defaults)
    11051108
  • django/core/validators.py

     
    165165        raise ValidationError, _('Enter a valid date/time in YYYY-MM-DD HH:MM format.')
    166166    _isValidDate(field_data.split()[0])
    167167
    168 def isValidEmail(field_data, all_data):
    169     if not email_re.search(field_data):
     168def isValidEmail(field_data, all_data, regex = email_re):
     169    if not regex.search(field_data):
    170170        raise ValidationError, _('Enter a valid e-mail address.')
    171171
    172172def isValidImage(field_data, all_data):
  • django/newforms/fields.py

     
    402402        'invalid': _(u'Enter a valid e-mail address.'),
    403403    }
    404404
    405     def __init__(self, max_length=None, min_length=None, *args, **kwargs):
    406         RegexField.__init__(self, email_re, max_length, min_length, *args,
     405    def __init__(self, max_length=None, min_length=None, regex=email_re, *args, **kwargs):
     406        RegexField.__init__(self, regex, max_length, min_length, *args,
    407407                            **kwargs)
    408408
    409409try:
     
    498498    }
    499499
    500500    def __init__(self, max_length=None, min_length=None, verify_exists=False,
    501             validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs):
    502         super(URLField, self).__init__(url_re, max_length, min_length, *args,
     501            validator_user_agent=URL_VALIDATOR_USER_AGENT, regex=url_re,
     502            *args, **kwargs):
     503        super(URLField, self).__init__(regex, max_length, min_length, *args,
    503504                                       **kwargs)
    504505        self.verify_exists = verify_exists
    505506        self.user_agent = validator_user_agent
  • tests/regressiontests/forms/fields.py

     
    741741...
    742742ValidationError: [u'Ensure this value has at most 15 characters (it has 20).']
    743743
     744>>> reg = re.compile('^[abc]+$')
     745>>> f = EmailField(regex=reg)
     746>>> f.clean('abc')
     747u'abc'
     748>>> f.clean('abcd')
     749Traceback (most recent call last):
     750...
     751ValidationError: [u'Enter a valid e-mail address.']
     752
    744753# FileField ##################################################################
    745754
    746755>>> f = FileField()
     
    923932>>> f.clean('https://example.com')
    924933u'https://example.com'
    925934
     935>>> reg = re.compile('^http://[abc]+$')
     936>>> f = URLField(regex=reg)
     937>>> f.clean('http://abc')
     938u'http://abc'
     939>>> f.clean('http://abcd')
     940Traceback (most recent call last):
     941...
     942ValidationError: [u'Enter a valid URL.']
     943
    926944# BooleanField ################################################################
    927945
    928946>>> f = BooleanField()
Back to Top