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

57 lines
1.9 KiB
Python
Raw Normal View History

2014-12-02 12:54:01 +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.
2020-07-21 14:38:36 +02:00
from trytond.pyson import PYSONEncoder, PYSON
from .field import Field
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:
2018-06-26 19:11:19 +02:00
assert isinstance(i, (int, PYSON)), \
2013-02-27 22:50:31 +01:00
'digits must be tuple of integers or PYSON'
if isinstance(i, PYSON):
2018-06-26 19:11:19 +02:00
assert i.types().issubset(set([int, int])), \
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'
_sql_type = 'FLOAT'
2019-06-26 21:56:16 +02:00
_py_type = float
2009-02-03 18:58:25 +01:00
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
2019-02-13 10:37:40 +01:00
def definition(self, model, language):
encoder = PYSONEncoder()
definition = super().definition(model, language)
definition['digits'] = encoder.encode(self.digits)
return definition