Opened 19 years ago

Closed 18 years ago

#1088 closed defect (fixed)

[patch] isValidFloat has logic errors

Reported by: Yango Owned by: Adrian Holovaty
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: no UI/UX: no

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

Download all attachments as: .zip

Change History (5)

by Yango, 19 years ago

Attachment: django.po added

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

by Yango, 19 years ago

Attachment: isValidFloat.diff added

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

by Yango, 19 years ago

Attachment: django.2.po added

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

comment:1 by Adrian Holovaty, 18 years ago

Summary: isValidFloat has logic errors[patch] isValidFloat has logic errors

comment:2 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: newclosed

(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