Opened 9 years ago

Closed 9 years ago

#1088 closed defect (fixed)

[patch] isValidFloat has logic errors

Reported by: Yango Owned by: adrian
Component: Validators Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 (3)

django.po (57.3 KB) - added by Yango 9 years ago.
conf/locale/es/LC_MESSAGES/django.po for the new string added in isValidFloat
isValidFloat.diff (1.3 KB) - added by Yango 9 years ago.
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 9 years ago.
Final django.po version (Trac doesn't let me replace existing attachment)

Download all attachments as: .zip

Change History (5)

Changed 9 years ago by Yango

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

Changed 9 years ago by Yango

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

Changed 9 years ago by Yango

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

comment:1 Changed 9 years ago by adrian

  • Summary changed from isValidFloat has logic errors to [patch] isValidFloat has logic errors

comment:2 Changed 9 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

Note: See TracTickets for help on using tickets.
Back to Top