Django

Code

Ticket #1088 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

[patch] isValidFloat has logic errors

Reported by: Yango Assigned to: adrian
Milestone: Component: Validators
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

It's missing the case when the whole part is greater than max_digits-decimal_places, which leads to ProgrammingError? raised when you add such a number

Corrected version:

class IsValidFloat:
    def __init__(self, max_digits, decimal_places):
        self.max_digits, self.decimal_places = max_digits, decimal_places

    def __call__(self, field_data, all_data):
        data = str(field_data)
        try:
            float(data)
        except ValueError:
            raise ValidationError, _("Please enter a valid decimal number.")
        if len(data) > (self.max_digits + 1):
            raise ValidationError, ngettext( "Please enter a valid decimal number with at most %s total digit.",
                "Please enter a valid decimal number with at most %s total digits.", self.max_digits) % self.max_digits
        if (not '.' in data and len(data) > (self.max_digits - self.decimal_places)) or ('.' in data and len(data.split('.')[1]) == 0 and len(data) > (self.max_digits - self.decimal_places + 1)):
            raise ValidationError, ngettext( "Please enter a valid decimal number with a whole part of at most %s digit.",
                "Please enter a valid decimal number with a whole part of at most %s digits.", str(int(self.max_digits)-int(self.decimal_places))) % str(int(self.max_digits)-int(self.decimal_places))
        if '.' in data and len(data.split('.')[1]) > self.decimal_places:
            raise ValidationError, ngettext("Please enter a valid decimal number with at most %s decimal place.",
                "Please enter a valid decimal number with at most %s decimal places.", self.decimal_places) % self.decimal_places

Attachments

django.po (57.3 kB) - added by Yango on 12/19/05 10:47:03.
conf/locale/es/LC_MESSAGES/django.po for the new string added in isValidFloat
isValidFloat.diff (1.3 kB) - added by Yango on 12/19/05 11:38:23.
A diff for the validator, don't pay attention to the pasted code as it's buggy
django.2.po (57.3 kB) - added by Yango on 12/19/05 11:40:02.
Final django.po version (Trac doesn't let me replace existing attachment)

Change History

12/19/05 10:47:03 changed by Yango

  • attachment django.po added.

conf/locale/es/LC_MESSAGES/django.po for the new string added in isValidFloat

12/19/05 11:38:23 changed by Yango

  • attachment isValidFloat.diff added.

A diff for the validator, don't pay attention to the pasted code as it's buggy

12/19/05 11:40:02 changed by Yango

  • attachment django.2.po added.

Final django.po version (Trac doesn't let me replace existing attachment)

06/02/06 15:54:37 changed by adrian

  • summary changed from isValidFloat has logic errors to [patch] isValidFloat has logic errors.

06/18/06 20:09:03 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [3142]) Fixed #1088 - Correctly detect when a float with too many digits before the decimal point is passed in.


Add/Change #1088 ([patch] isValidFloat has logic errors)




Change Properties
Action