Ticket #15775: ticket_15775.2.diff

File ticket_15775.2.diff, 4.7 KB (added by samufuentes, 13 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):
  • 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..eb04b7e 100644
    a b  
     1from decimal import Decimal
     2
    13from django import forms
    24from django.core import validators
    35from django.core.exceptions import ValidationError
    46from django.utils.unittest import TestCase
     7from django import test
    58
    69
    710class TestFieldWithValidators(TestCase):
    class TestFieldWithValidators(TestCase):  
    1417            field.clean('not int nor mail')
    1518        except ValidationError, e:
    1619            self.assertEqual(2, len(e.messages))
     20
     21class DecimalFieldTests(test.TestCase):
     22    def test_validate(self):
     23        f = forms.DecimalField(max_digits=10, decimal_places=1)
     24        try:
     25            f.validate(Decimal('1E+2'))  # Ensure that scientific notation with positive exponent is accepted (#15775)
     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'))
     31 No newline at end of file
Back to Top