Ticket #4807: decimal_strip.patch

File decimal_strip.patch, 4.3 KB (added by Chris Beaven, 17 years ago)
  • django/core/validators.py

     
    2626    r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"  # dot-atom
    2727    r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
    2828    r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE)  # domain
    29 decimal_re = re.compile(r'^-?(?P<digits>\d+)(\.(?P<decimals>\d+))?$')
     29decimal_re = re.compile(r'^-?(?P<digits>\d*)(\.(?P<decimals>\d+))?$')
    3030integer_re = re.compile(r'^-?\d+$')
    3131ip4_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}$')
    3232phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
     
    418418        match = decimal_re.search(str(field_data))
    419419        if not match:
    420420            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
    425425        if digits + decimals > self.max_digits:
    426426            raise ValidationError, ungettext("Please enter a valid decimal number with at most %s total digit.",
    427427                "Please enter a valid decimal number with at most %s total digits.", self.max_digits) % self.max_digits
  • django/newforms/fields.py

     
    162162            raise ValidationError(ugettext('Ensure this value is greater than or equal to %s.') % self.min_value)
    163163        return value
    164164
    165 decimal_re = re.compile(r'^-?(?P<digits>\d+)(\.(?P<decimals>\d+))?$')
     165decimal_re = re.compile(r'-?(?P<digits>\d*)(\.(?P<decimals>\d+))?$')
    166166
    167167class DecimalField(Field):
    168168    def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs):
     
    181181        if not self.required and value in EMPTY_VALUES:
    182182            return None
    183183        value = value.strip()
    184         match = decimal_re.search(value)
     184        match = decimal_re.match(value)
    185185        if not match:
    186186            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'))
    191189        if self.max_value is not None and value > self.max_value:
    192190            raise ValidationError(ugettext('Ensure this value is less than or equal to %s.') % self.max_value)
    193191        if self.min_value is not None and value < self.min_value:
    194192            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'))
    195196        if self.max_digits is not None and (digits + decimals) > self.max_digits:
    196197            raise ValidationError(ugettext('Ensure that there are no more than %s digits in total.') % self.max_digits)
    197198        if self.decimal_places is not None and decimals > self.decimal_places:
  • tests/regressiontests/forms/tests.py

     
    11321132...
    11331133ValidationError: [u'Enter a number.']
    11341134>>> f.clean('1.0 ')
    1135 Decimal("1.0")
     1135Decimal("1")
    11361136>>> f.clean(' 1.0')
    1137 Decimal("1.0")
     1137Decimal("1")
    11381138>>> f.clean(' 1.0 ')
    1139 Decimal("1.0")
     1139Decimal("1")
    11401140>>> f.clean('1.0a')
    11411141Traceback (most recent call last):
    11421142...
     
    11771177>>> f.clean('0.5')
    11781178Decimal("0.5")
    11791179
     1180DecimalField 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')
     1183Decimal("0.1234")
     1184>>> f.clean('00000.12340')
     1185Decimal("0.1234")
     1186
    11801187# DateField ###################################################################
    11811188
    11821189>>> import datetime
Back to Top