Changeset 5680
- Timestamp:
- 07/13/07 04:09:59 (1 year ago)
- Files:
-
- django/trunk/django/core/validators.py (modified) (3 diffs)
- django/trunk/django/newforms/fields.py (modified) (3 diffs)
- django/trunk/tests/regressiontests/forms/tests.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/validators.py
r5640 r5680 15 15 from django.utils.encoding import force_unicode 16 16 import re 17 try: 18 from decimal import Decimal, DecimalException 19 except ImportError: 20 from django.utils._decimal import Decimal, DecimalException # Python 2.3 17 21 18 22 _datere = r'\d{4}-\d{1,2}-\d{1,2}' … … 27 31 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"' # quoted-string 28 32 r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain 29 decimal_re = re.compile(r'^-?(?P<digits>\d+)(\.(?P<decimals>\d+))?$')30 33 integer_re = re.compile(r'^-?\d+$') 31 34 ip4_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}$') … … 416 419 417 420 def __call__(self, field_data, all_data): 418 match = decimal_re.search(str(field_data)) 419 if not match: 421 try: 422 val = Decimal(field_data) 423 except DecimalException: 420 424 raise ValidationError, _("Please enter a valid decimal number.") 421 422 digits = len(match.group('digits') or '') 423 decimals = len(match.group('decimals') or '') 424 425 426 pieces = str(val).split('.') 427 decimals = (len(pieces) == 2) and len(pieces[1]) or 0 428 digits = len(pieces[0]) 429 425 430 if digits + decimals > self.max_digits: 426 431 raise ValidationError, ungettext("Please enter a valid decimal number with at most %s total digit.", django/trunk/django/newforms/fields.py
r5669 r5680 12 12 from util import ErrorList, ValidationError 13 13 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple 14 15 try: 16 from decimal import Decimal, DecimalException 17 except ImportError: 18 from django.utils._decimal import Decimal, DecimalException 14 19 15 20 __all__ = ( … … 163 168 return value 164 169 165 decimal_re = re.compile(r'^-?(?P<digits>\d+)(\.(?P<decimals>\d+))?$')166 167 170 class DecimalField(Field): 168 171 def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): … … 182 185 return None 183 186 value = value.strip() 184 match = decimal_re.search(value) 185 if not match: 187 try: 188 value = Decimal(value) 189 except DecimalException: 186 190 raise ValidationError(ugettext('Enter a number.')) 187 else: 188 value = Decimal(value) 189 digits = len(match.group('digits') or '') 190 decimals = len(match.group('decimals') or '') 191 pieces = str(value).split('.') 192 decimals = (len(pieces) == 2) and len(pieces[1]) or 0 193 digits = len(pieces[0]) 191 194 if self.max_value is not None and value > self.max_value: 192 195 raise ValidationError(ugettext('Ensure this value is less than or equal to %s.') % self.max_value) django/trunk/tests/regressiontests/forms/tests.py
r5609 r5680 1177 1177 >>> f.clean('0.5') 1178 1178 Decimal("0.5") 1179 >>> f.clean('.5') 1180 Decimal("0.5") 1181 >>> f.clean('00.50') 1182 Decimal("0.50") 1179 1183 1180 1184 # DateField ###################################################################
