Ticket #4807: decimal_strip.patch
File decimal_strip.patch, 4.3 KB (added by , 17 years ago) |
---|
-
django/core/validators.py
26 26 r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom 27 27 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string 28 28 r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain 29 decimal_re = re.compile(r'^-?(?P<digits>\d +)(\.(?P<decimals>\d+))?$')29 decimal_re = re.compile(r'^-?(?P<digits>\d*)(\.(?P<decimals>\d+))?$') 30 30 integer_re = re.compile(r'^-?\d+$') 31 31 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}$') 32 32 phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE) … … 418 418 match = decimal_re.search(str(field_data)) 419 419 if not match: 420 420 raise ValidationError, _("Please enter a valid decimal number.") 421 422 digits = len( match.group('digits') or '')423 decimals = len( match.group('decimals') or '')424 421 422 digits = len((match.group('digits') or '').lstrip('0')) 423 decimals = len((match.group('decimals') or '').rstrip('0')) 424 425 425 if digits + decimals > self.max_digits: 426 426 raise ValidationError, ungettext("Please enter a valid decimal number with at most %s total digit.", 427 427 "Please enter a valid decimal number with at most %s total digits.", self.max_digits) % self.max_digits -
django/newforms/fields.py
162 162 raise ValidationError(ugettext('Ensure this value is greater than or equal to %s.') % self.min_value) 163 163 return value 164 164 165 decimal_re = re.compile(r' ^-?(?P<digits>\d+)(\.(?P<decimals>\d+))?$')165 decimal_re = re.compile(r'-?(?P<digits>\d*)(\.(?P<decimals>\d+))?$') 166 166 167 167 class DecimalField(Field): 168 168 def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): … … 181 181 if not self.required and value in EMPTY_VALUES: 182 182 return None 183 183 value = value.strip() 184 match = decimal_re. search(value)184 match = decimal_re.match(value) 185 185 if not match: 186 186 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 '') 187 188 value = Decimal(value.rstrip('0')) 191 189 if self.max_value is not None and value > self.max_value: 192 190 raise ValidationError(ugettext('Ensure this value is less than or equal to %s.') % self.max_value) 193 191 if self.min_value is not None and value < self.min_value: 194 192 raise ValidationError(ugettext('Ensure this value is greater than or equal to %s.') % self.min_value) 193 194 digits = len((match.group('digits') or '').lstrip('0')) 195 decimals = len((match.group('decimals') or '').rstrip('0')) 195 196 if self.max_digits is not None and (digits + decimals) > self.max_digits: 196 197 raise ValidationError(ugettext('Ensure that there are no more than %s digits in total.') % self.max_digits) 197 198 if self.decimal_places is not None and decimals > self.decimal_places: -
tests/regressiontests/forms/tests.py
1132 1132 ... 1133 1133 ValidationError: [u'Enter a number.'] 1134 1134 >>> f.clean('1.0 ') 1135 Decimal("1 .0")1135 Decimal("1") 1136 1136 >>> f.clean(' 1.0') 1137 Decimal("1 .0")1137 Decimal("1") 1138 1138 >>> f.clean(' 1.0 ') 1139 Decimal("1 .0")1139 Decimal("1") 1140 1140 >>> f.clean('1.0a') 1141 1141 Traceback (most recent call last): 1142 1142 ... … … 1177 1177 >>> f.clean('0.5') 1178 1178 Decimal("0.5") 1179 1179 1180 DecimalField works with values which trim down to max_digits and decimal_places 1181 >>> f = DecimalField(max_digits=4, decimal_places=4) 1182 >>> f.clean('.1234') 1183 Decimal("0.1234") 1184 >>> f.clean('00000.12340') 1185 Decimal("0.1234") 1186 1180 1187 # DateField ################################################################### 1181 1188 1182 1189 >>> import datetime