simplified logic for number validators
This commit is contained in:
@@ -1 +1 @@
|
|||||||
Version 2.7.2-stable+timestamp.2013.10.11.16.58.14
|
Version 2.7.2-stable+timestamp.2013.10.11.17.06.24
|
||||||
|
|||||||
+14
-24
@@ -696,11 +696,11 @@ class IS_INT_IN_RANGE(Validator):
|
|||||||
>>> IS_INT_IN_RANGE(None,5)('4')
|
>>> IS_INT_IN_RANGE(None,5)('4')
|
||||||
(4, None)
|
(4, None)
|
||||||
>>> IS_INT_IN_RANGE(None,5)('6')
|
>>> IS_INT_IN_RANGE(None,5)('6')
|
||||||
(6, 'enter an integer less than or equal to 4')
|
('6', 'enter an integer less than or equal to 4')
|
||||||
>>> IS_INT_IN_RANGE(1,None)('4')
|
>>> IS_INT_IN_RANGE(1,None)('4')
|
||||||
(4, None)
|
(4, None)
|
||||||
>>> IS_INT_IN_RANGE(1,None)('0')
|
>>> IS_INT_IN_RANGE(1,None)('0')
|
||||||
(0, 'enter an integer greater than or equal to 1')
|
('0', 'enter an integer greater than or equal to 1')
|
||||||
>>> IS_INT_IN_RANGE()(6)
|
>>> IS_INT_IN_RANGE()(6)
|
||||||
(6, None)
|
(6, None)
|
||||||
>>> IS_INT_IN_RANGE()('abc')
|
>>> IS_INT_IN_RANGE()('abc')
|
||||||
@@ -742,10 +742,10 @@ class IS_INT_IN_RANGE(Validator):
|
|||||||
|
|
||||||
def __call__(self, value):
|
def __call__(self, value):
|
||||||
if value and str(value).isdigit():
|
if value and str(value).isdigit():
|
||||||
value = int(value)
|
v = int(value)
|
||||||
if ((self.minimum is None or value >= self.minimum) and
|
if ((self.minimum is None or v >= self.minimum) and
|
||||||
(self.maximum is None or value < self.maximum)):
|
(self.maximum is None or v < self.maximum)):
|
||||||
return (value, None)
|
return (v, None)
|
||||||
return (value, self.error_message)
|
return (value, self.error_message)
|
||||||
|
|
||||||
def str2dec(number):
|
def str2dec(number):
|
||||||
@@ -828,17 +828,12 @@ class IS_FLOAT_IN_RANGE(Validator):
|
|||||||
def __call__(self, value):
|
def __call__(self, value):
|
||||||
try:
|
try:
|
||||||
if self.dot == '.':
|
if self.dot == '.':
|
||||||
fvalue = float(value)
|
v = float(value)
|
||||||
else:
|
else:
|
||||||
fvalue = float(str(value).replace(self.dot, '.'))
|
v = float(str(value).replace(self.dot, '.'))
|
||||||
if self.minimum is None:
|
if ((self.minimum is None or v >= self.minimum) and
|
||||||
if self.maximum is None or fvalue <= self.maximum:
|
(self.maximum is None or v <= self.maximum)):
|
||||||
return (fvalue, None)
|
return (v, None)
|
||||||
elif self.maximum is None:
|
|
||||||
if fvalue >= self.minimum:
|
|
||||||
return (fvalue, None)
|
|
||||||
elif self.minimum <= fvalue <= self.maximum:
|
|
||||||
return (fvalue, None)
|
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
pass
|
pass
|
||||||
return (value, self.error_message)
|
return (value, self.error_message)
|
||||||
@@ -937,14 +932,9 @@ class IS_DECIMAL_IN_RANGE(Validator):
|
|||||||
v = value
|
v = value
|
||||||
else:
|
else:
|
||||||
v = decimal.Decimal(str(value).replace(self.dot, '.'))
|
v = decimal.Decimal(str(value).replace(self.dot, '.'))
|
||||||
if self.minimum is None:
|
if ((self.minimum is None or v >= self.minimum) and
|
||||||
if self.maximum is None or v <= self.maximum:
|
(self.maximum is None or v <= self.maximum)):
|
||||||
return (v, None)
|
return (v, None)
|
||||||
elif self.maximum is None:
|
|
||||||
if v >= self.minimum:
|
|
||||||
return (v, None)
|
|
||||||
elif self.minimum <= v <= self.maximum:
|
|
||||||
return (v, None)
|
|
||||||
except (ValueError, TypeError, decimal.InvalidOperation):
|
except (ValueError, TypeError, decimal.InvalidOperation):
|
||||||
pass
|
pass
|
||||||
return (value, self.error_message)
|
return (value, self.error_message)
|
||||||
|
|||||||
Reference in New Issue
Block a user