Ticket #6092: 6092.2.diff
File 6092.2.diff, 4.8 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
710 710 class EmailField(CharField): 711 711 def __init__(self, *args, **kwargs): 712 712 kwargs['max_length'] = kwargs.get('max_length', 75) 713 self.regex = kwargs.get('regex') 713 714 CharField.__init__(self, *args, **kwargs) 714 715 715 716 def get_manipulator_field_objs(self): 716 717 return [oldforms.EmailField] 717 718 718 719 def validate(self, field_data, all_data): 719 validators.isValidEmail(field_data, all_data )720 validators.isValidEmail(field_data, all_data, regex=self.regex) 720 721 721 722 def formfield(self, **kwargs): 722 defaults = {'form_class': forms.EmailField }723 defaults = {'form_class': forms.EmailField, 'regex': self.regex} 723 724 defaults.update(kwargs) 724 725 return super(EmailField, self).formfield(**defaults) 725 726 … … 1088 1089 return super(TimeField, self).formfield(**defaults) 1089 1090 1090 1091 class 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): 1092 1093 kwargs['max_length'] = kwargs.get('max_length', 200) 1093 1094 if verify_exists: 1094 1095 kwargs.setdefault('validator_list', []).append(validators.isExistingURL) 1095 1096 self.verify_exists = verify_exists 1097 self.regex = regex 1096 1098 CharField.__init__(self, verbose_name, name, **kwargs) 1097 1099 1098 1100 def get_manipulator_field_objs(self): 1099 1101 return [oldforms.URLField] 1100 1102 1101 1103 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} 1103 1106 defaults.update(kwargs) 1104 1107 return super(URLField, self).formfield(**defaults) 1105 1108 -
django/core/validators.py
165 165 raise ValidationError, _('Enter a valid date/time in YYYY-MM-DD HH:MM format.') 166 166 _isValidDate(field_data.split()[0]) 167 167 168 def isValidEmail(field_data, all_data ):169 if not email_re.search(field_data):168 def isValidEmail(field_data, all_data, regex = email_re): 169 if not regex.search(field_data): 170 170 raise ValidationError, _('Enter a valid e-mail address.') 171 171 172 172 def isValidImage(field_data, all_data): -
django/newforms/fields.py
402 402 'invalid': _(u'Enter a valid e-mail address.'), 403 403 } 404 404 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, 407 407 **kwargs) 408 408 409 409 try: … … 498 498 } 499 499 500 500 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, 503 504 **kwargs) 504 505 self.verify_exists = verify_exists 505 506 self.user_agent = validator_user_agent -
tests/regressiontests/forms/fields.py
741 741 ... 742 742 ValidationError: [u'Ensure this value has at most 15 characters (it has 20).'] 743 743 744 >>> reg = re.compile('^[abc]+$') 745 >>> f = EmailField(regex=reg) 746 >>> f.clean('abc') 747 u'abc' 748 >>> f.clean('abcd') 749 Traceback (most recent call last): 750 ... 751 ValidationError: [u'Enter a valid e-mail address.'] 752 744 753 # FileField ################################################################## 745 754 746 755 >>> f = FileField() … … 923 932 >>> f.clean('https://example.com') 924 933 u'https://example.com' 925 934 935 >>> reg = re.compile('^http://[abc]+$') 936 >>> f = URLField(regex=reg) 937 >>> f.clean('http://abc') 938 u'http://abc' 939 >>> f.clean('http://abcd') 940 Traceback (most recent call last): 941 ... 942 ValidationError: [u'Enter a valid URL.'] 943 926 944 # BooleanField ################################################################ 927 945 928 946 >>> f = BooleanField()