[go: up one dir, main page]

Menu

[599fc9]: / sqlutil / formlocale.py  Maximize  Restore  History

Download this file

36 lines (29 with data), 1.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from formal import converters, validation
import locale
try:
import decimal
haveDecimal = True
except ImportError:
haveDecimal = False
class LocNumberToStringConverter (converters.NumberToStringConverter):
def fromType( self, value ):
if value is None: return None
return locale.str( value )
def toType(self, value):
if value is not None: value = value.strip()
if not value: return None
# "Cast" the value to the correcl type. For some strange reason,
# Python's decimal.Decimal type raises an ArithmeticError when it's
# given a dodgy value.
try:
value = locale.atof( value, self.cast )
except (ValueError, ArithmeticError):
raise validation.FieldValidationError("Not a valid number")
return value
#class LocIntegerToStringConverter (LocNumberToStringConverter):
# cast = int
class LocFloatToStringConverter (LocNumberToStringConverter):
cast = float
if haveDecimal:
class LocDecimalToStringConverter (LocNumberToStringConverter):
cast = decimal.Decimal