Ticket #15775: ticket_15775.diff

File ticket_15775.diff, 2.9 KB (added by samufuentes, 4 years ago)
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index a5ea81d..02fc7ef 100644
    a b class DecimalField(Field): 
    301301        if value != value or value == Decimal("Inf") or value == Decimal("-Inf"):
    302302            raise ValidationError(self.error_messages['invalid'])
    303303        sign, digittuple, exponent = value.as_tuple()
    304         decimals = abs(exponent)
    305         # digittuple doesn't include any leading zeros.
    306         digits = len(digittuple)
    307         if decimals > digits:
    308             # We have leading zeros up to or past the decimal point.  Count
    309             # everything past the decimal point as a digit.  We do not count
    310             # 0 before the decimal point as a digit since that would mean
    311             # we would not allow max_digits = decimal_places.
    312             digits = decimals
    313         whole_digits = digits - decimals
    314 
    315         if self.max_digits is not None and digits > self.max_digits:
    316             raise ValidationError(self.error_messages['max_digits'] % self.max_digits)
    317         if self.decimal_places is not None and decimals > self.decimal_places:
    318             raise ValidationError(self.error_messages['max_decimal_places'] % self.decimal_places)
    319         if self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places):
    320             raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places))
     304        if exponent < 0:
     305            decimals = abs(exponent)
     306            # digittuple doesn't include any leading zeros.
     307            digits = len(digittuple)
     308            if decimals > digits:
     309                # We have leading zeros up to or past the decimal point.  Count
     310                # everything past the decimal point as a digit.  We do not count
     311                # 0 before the decimal point as a digit since that would mean
     312                # we would not allow max_digits = decimal_places.
     313                digits = decimals
     314            whole_digits = digits - decimals
     315
     316            if self.max_digits is not None and digits > self.max_digits:
     317                raise ValidationError(self.error_messages['max_digits'] % self.max_digits)
     318            if self.decimal_places is not None and decimals > self.decimal_places:
     319                raise ValidationError(self.error_messages['max_decimal_places'] % self.decimal_places)
     320            if self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places):
     321                raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places))
     322        else:
     323            digits = len(digittuple) + exponent
     324            if self.max_digits is not None and digits > self.max_digits:
     325                raise ValidationError(self.error_messages['max_digits'] % self.max_digits)
     326
     327
    321328        return value
    322329
    323330class BaseTemporalField(Field):
Back to Top