Opened 20 years ago
Closed 19 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)
Change History (5)
by , 20 years ago
by , 20 years ago
| Attachment: | isValidFloat.diff added |
|---|
A diff for the validator, don't pay attention to the pasted code as it's buggy
by , 20 years ago
| Attachment: | django.2.po added |
|---|
Final django.po version (Trac doesn't let me replace existing attachment)
comment:1 by , 19 years ago
| Summary: | isValidFloat has logic errors → [patch] isValidFloat has logic errors |
|---|
comment:2 by , 19 years ago
| 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