Ticket #15775: ticket_15775.3.diff

File ticket_15775.3.diff, 4.8 KB (added by Bruno Gola, 13 years ago)
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index dd23b12..6f8d1a3 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']
     320                                                          % self.decimal_places)
     321            if (self.max_digits is not None)     and (
     322                self.decimal_places is not None) and (
     323                whole_digits > (self.max_digits - self.decimal_places)):
     324                raise ValidationError(self.error_messages['max_whole_digits']
     325                                     % (self.max_digits - self.decimal_places))
     326        else:
     327            digits = len(digittuple) + exponent
     328            if self.max_digits is not None and digits > self.max_digits:
     329                raise ValidationError(self.error_messages['max_digits'] % self.max_digits)
     330
    321331        return value
    322332
    323333class BaseTemporalField(Field):
  • tests/regressiontests/forms/tests/__init__.py

    diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py
    index 39db39f..5543fac 100644
    a b from media import *  
    88from models import *
    99from regressions import *
    1010from util import *
    11 from validators import TestFieldWithValidators
     11from validators import *
    1212from widgets import *
    1313
    1414from regressiontests.forms.localflavortests import (
  • tests/regressiontests/forms/tests/validators.py

    diff --git a/tests/regressiontests/forms/tests/validators.py b/tests/regressiontests/forms/tests/validators.py
    index cadf660..bbabba4 100644
    a b from django.core import validators  
    33from django.core.exceptions import ValidationError
    44from django.utils.unittest import TestCase
    55
     6from decimal import Decimal
    67
    78class TestFieldWithValidators(TestCase):
    89    def test_all_errors_get_reported(self):
    class TestFieldWithValidators(TestCase):  
    1415            field.clean('not int nor mail')
    1516        except ValidationError, e:
    1617            self.assertEqual(2, len(e.messages))
     18
     19
     20class DecimalFieldTests(TestCase):
     21    def test_validate(self):
     22        f = forms.DecimalField(max_digits=10, decimal_places=1)
     23        try:
     24            # regression test for #15775: scientific notation with positive exponent is valid
     25            f.validate(Decimal('1E+2'))
     26            f.validate(Decimal('1E-1'))
     27            f.validate(Decimal('100'))
     28        except ValidationError:
     29            self.fail("Validation is not working properly")
     30        self.assertRaises(ValidationError, f.validate, Decimal('1E-2'))
Back to Top