Files
tryton/trytond/model/fields/float.py
T

69 lines
2.2 KiB
Python
Raw Normal View History

2009-02-03 18:58:25 +01:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
2013-09-06 22:32:37 +02:00
from sql import Query, Expression
2009-02-03 18:58:25 +01:00
2013-09-06 22:32:37 +02:00
from ...config import CONFIG
from .field import Field, SQLType
from ...pyson import PYSON
2012-04-01 23:08:15 +02:00
def digits_validate(value):
if value:
assert isinstance(value, tuple), 'digits must be a tuple'
for i in value:
assert isinstance(i, (int, long, PYSON)), \
2013-02-27 22:50:31 +01:00
'digits must be tuple of integers or PYSON'
if isinstance(i, PYSON):
assert i.types().issubset(set([int, long])), \
2013-02-27 22:50:31 +01:00
'PYSON digits must return an integer'
2009-02-03 18:58:25 +01:00
class Float(Field):
2009-03-01 13:37:31 +01:00
'''
2009-09-13 15:37:11 +02:00
Define a float field (``float``).
2009-03-01 13:37:31 +01:00
'''
2009-02-03 18:58:25 +01:00
_type = 'float'
2013-02-27 22:50:31 +01:00
def __init__(self, string='', digits=None, help='', required=False,
2011-12-22 22:03:15 +01:00
readonly=False, domain=None, states=None, select=False,
on_change=None, on_change_with=None, depends=None,
2013-09-06 22:32:37 +02:00
context=None, loading='eager'):
2009-03-01 13:37:31 +01:00
'''
2009-09-13 15:37:11 +02:00
:param digits: a list of two integers defining the total
of digits and the number of decimals of the float.
2009-03-01 13:37:31 +01:00
'''
super(Float, self).__init__(string=string, help=help,
2011-12-22 22:03:15 +01:00
required=required, readonly=readonly, domain=domain, states=states,
select=select, on_change=on_change, on_change_with=on_change_with,
2013-09-06 22:32:37 +02:00
depends=depends, context=context, loading=loading)
self.__digits = None
2009-02-03 18:58:25 +01:00
self.digits = digits
2009-03-01 13:37:31 +01:00
__init__.__doc__ += Field.__init__.__doc__
def _get_digits(self):
return self.__digits
def _set_digits(self, value):
digits_validate(value)
self.__digits = value
digits = property(_get_digits, _set_digits)
2013-09-06 22:32:37 +02:00
@staticmethod
def sql_format(value):
if isinstance(value, (Query, Expression)):
return value
if value is None:
return None
return float(value)
def sql_type(self):
db_type = CONFIG['db_type']
if db_type == 'postgresql':
return SQLType('FLOAT8', 'FLOAT8')
elif db_type == 'mysql':
return SQLType('DOUBLE', 'DOUBLE(255, 15)')
else:
return SQLType('FLOAT', 'FLOAT')