Ticket #6092: 6092.diff

File 6092.diff, 6.2 KB (added by floguy, 17 years ago)

Initial patch to add pattern keyword argument to several model and form fields.

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

     
    691691class EmailField(CharField):
    692692    def __init__(self, *args, **kwargs):
    693693        kwargs['max_length'] = kwargs.get('max_length', 75)
     694        self.pattern = kwargs.get('pattern', None)
     695        if self.pattern != None and not hasattr(self.pattern, 'match'):
     696            self.pattern = re.compile(self.pattern, re.IGNORECASE)
    694697        CharField.__init__(self, *args, **kwargs)
    695698
    696699    def get_internal_type(self):
     
    700703        return [oldforms.EmailField]
    701704
    702705    def validate(self, field_data, all_data):
    703         validators.isValidEmail(field_data, all_data)
     706        validators.isValidEmail(field_data, all_data, pattern=self.pattern)
    704707
    705708    def formfield(self, **kwargs):
    706         defaults = {'form_class': forms.EmailField}
     709        defaults = {'form_class': forms.EmailField, 'pattern': self.pattern}
    707710        defaults.update(kwargs)
    708711        return super(EmailField, self).formfield(**defaults)
    709712
     
    10211024        return super(TimeField, self).formfield(**defaults)
    10221025
    10231026class URLField(CharField):
    1024     def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
     1027    def __init__(self, verbose_name=None, name=None, verify_exists=True, pattern=None, **kwargs):
    10251028        kwargs['max_length'] = kwargs.get('max_length', 200)
    10261029        if verify_exists:
    10271030            kwargs.setdefault('validator_list', []).append(validators.isExistingURL)
    10281031        self.verify_exists = verify_exists
     1032        self.pattern = pattern
     1033        if pattern != None and not hasattr(pattern, 'match'):
     1034            self.pattern = re.compile(pattern, re.IGNORECASE)
    10291035        CharField.__init__(self, verbose_name, name, **kwargs)
    10301036
    10311037    def get_manipulator_field_objs(self):
     
    10351041        return "CharField"
    10361042
    10371043    def formfield(self, **kwargs):
    1038         defaults = {'form_class': forms.URLField, 'verify_exists': self.verify_exists}
     1044        defaults = {'form_class': forms.URLField, 'verify_exists': self.verify_exists,
     1045            'pattern': self.pattern}
    10391046        defaults.update(kwargs)
    10401047        return super(URLField, self).formfield(**defaults)
    10411048
  • 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, pattern = None):
     169    re_to_use = email_re
     170    if pattern != None:
     171        if hasattr(pattern, 'match'):
     172            re_to_use = pattern
     173        else:
     174            re_to_use = re.compile(pattern, re.IGNORECASE)
     175    if not re_to_use.search(field_data):
    170176        raise ValidationError, _('Enter a valid e-mail address.')
    171177
    172178def 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, pattern=None, *args, **kwargs):
     406        if pattern != None:
     407            if hasattr(pattern, 'match'):
     408                re_to_use = pattern
     409            else:
     410                re_to_use = re.compile(pattern, re.IGNORECASE)
     411        else:
     412            re_to_use = email_re
     413        RegexField.__init__(self, re_to_use, max_length, min_length, *args,
    407414                            **kwargs)
    408415
    409416try:
     
    494501    }
    495502
    496503    def __init__(self, max_length=None, min_length=None, verify_exists=False,
    497             validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs):
    498         super(URLField, self).__init__(url_re, max_length, min_length, *args,
     504            validator_user_agent=URL_VALIDATOR_USER_AGENT, pattern=None,
     505            *args, **kwargs):
     506        if pattern != None:
     507            if hasattr(pattern, 'match'):
     508                re_to_use = pattern
     509            else:
     510                re_to_use = re.compile(pattern, re.IGNORECASE)
     511        else:
     512            re_to_use = url_re
     513        super(URLField, self).__init__(re_to_use, max_length, min_length, *args,
    499514                                       **kwargs)
    500515        self.verify_exists = verify_exists
    501516        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>>> f = EmailField(pattern=r'^[abc]+$')
     745>>> f.clean('abc')
     746u'abc'
     747>>> f.clean('abcd')
     748Traceback (most recent call last):
     749...
     750ValidationError: [u'Enter a valid e-mail address.']
     751
     752>>> reg = re.compile('^[abc]+$')
     753>>> f = EmailField(pattern=reg)
     754>>> f.clean('abc')
     755u'abc'
     756>>> f.clean('abcd')
     757Traceback (most recent call last):
     758...
     759ValidationError: [u'Enter a valid e-mail address.']
     760
    744761# FileField ##################################################################
    745762
    746763>>> f = FileField()
     
    896913>>> f.clean('https://example.com')
    897914u'https://example.com'
    898915
     916>>> f = URLField(pattern=r'^http://[abc]+$')
     917>>> f.clean('http://abc')
     918u'http://abc'
     919>>> f.clean('http://abcd')
     920Traceback (most recent call last):
     921...
     922ValidationError: [u'Enter a valid URL.']
     923
     924>>> reg = re.compile('^http://[abc]+$')
     925>>> f = URLField(pattern=reg)
     926>>> f.clean('http://abc')
     927u'http://abc'
     928>>> f.clean('http://abcd')
     929Traceback (most recent call last):
     930...
     931ValidationError: [u'Enter a valid URL.']
     932
    899933# BooleanField ################################################################
    900934
    901935>>> f = BooleanField()
Back to Top