Opened 12 years ago
Closed 12 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: | 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)
Change History (5)
Changed 12 years ago by
Changed 12 years ago by
Attachment: | isValidFloat.diff added |
---|
A diff for the validator, don't pay attention to the pasted code as it's buggy
Changed 12 years ago by
Attachment: | django.2.po added |
---|
Final django.po version (Trac doesn't let me replace existing attachment)
comment:1 Changed 12 years ago by
Summary: | isValidFloat has logic errors → [patch] isValidFloat has logic errors |
---|
comment:2 Changed 12 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
Note: See
TracTickets for help on using
tickets.
conf/locale/es/LC_MESSAGES/django.po for the new string added in isValidFloat