Ticket #6092: 6092.diff
File 6092.diff, 6.2 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
691 691 class EmailField(CharField): 692 692 def __init__(self, *args, **kwargs): 693 693 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) 694 697 CharField.__init__(self, *args, **kwargs) 695 698 696 699 def get_internal_type(self): … … 700 703 return [oldforms.EmailField] 701 704 702 705 def validate(self, field_data, all_data): 703 validators.isValidEmail(field_data, all_data )706 validators.isValidEmail(field_data, all_data, pattern=self.pattern) 704 707 705 708 def formfield(self, **kwargs): 706 defaults = {'form_class': forms.EmailField }709 defaults = {'form_class': forms.EmailField, 'pattern': self.pattern} 707 710 defaults.update(kwargs) 708 711 return super(EmailField, self).formfield(**defaults) 709 712 … … 1021 1024 return super(TimeField, self).formfield(**defaults) 1022 1025 1023 1026 class 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): 1025 1028 kwargs['max_length'] = kwargs.get('max_length', 200) 1026 1029 if verify_exists: 1027 1030 kwargs.setdefault('validator_list', []).append(validators.isExistingURL) 1028 1031 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) 1029 1035 CharField.__init__(self, verbose_name, name, **kwargs) 1030 1036 1031 1037 def get_manipulator_field_objs(self): … … 1035 1041 return "CharField" 1036 1042 1037 1043 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} 1039 1046 defaults.update(kwargs) 1040 1047 return super(URLField, self).formfield(**defaults) 1041 1048 -
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, 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): 170 176 raise ValidationError, _('Enter a valid e-mail address.') 171 177 172 178 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, 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, 407 414 **kwargs) 408 415 409 416 try: … … 494 501 } 495 502 496 503 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, 499 514 **kwargs) 500 515 self.verify_exists = verify_exists 501 516 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 >>> f = EmailField(pattern=r'^[abc]+$') 745 >>> f.clean('abc') 746 u'abc' 747 >>> f.clean('abcd') 748 Traceback (most recent call last): 749 ... 750 ValidationError: [u'Enter a valid e-mail address.'] 751 752 >>> reg = re.compile('^[abc]+$') 753 >>> f = EmailField(pattern=reg) 754 >>> f.clean('abc') 755 u'abc' 756 >>> f.clean('abcd') 757 Traceback (most recent call last): 758 ... 759 ValidationError: [u'Enter a valid e-mail address.'] 760 744 761 # FileField ################################################################## 745 762 746 763 >>> f = FileField() … … 896 913 >>> f.clean('https://example.com') 897 914 u'https://example.com' 898 915 916 >>> f = URLField(pattern=r'^http://[abc]+$') 917 >>> f.clean('http://abc') 918 u'http://abc' 919 >>> f.clean('http://abcd') 920 Traceback (most recent call last): 921 ... 922 ValidationError: [u'Enter a valid URL.'] 923 924 >>> reg = re.compile('^http://[abc]+$') 925 >>> f = URLField(pattern=reg) 926 >>> f.clean('http://abc') 927 u'http://abc' 928 >>> f.clean('http://abcd') 929 Traceback (most recent call last): 930 ... 931 ValidationError: [u'Enter a valid URL.'] 932 899 933 # BooleanField ################################################################ 900 934 901 935 >>> f = BooleanField()