File: float.py

package info (click to toggle)
tryton-server 7.0.40-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,748 kB
  • sloc: python: 53,502; xml: 5,194; sh: 803; sql: 217; makefile: 28
file content (79 lines) | stat: -rw-r--r-- 2,603 bytes parent folder | download | duplicates (3)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.pyson import PYSON, PYSONEncoder
from trytond.tools import cached_property

from .field import Field, get_eval_fields


def digits_validate(value):
    if value:
        assert isinstance(value, (tuple, str)), \
                'digits must be a tuple or a string'
        if isinstance(value, tuple):
            for i in value:
                assert isinstance(i, (int, PYSON)), \
                    'digits must be tuple of integers or PYSON'
                if isinstance(i, PYSON):
                    assert i.types() == {int}, \
                        'PYSON digits must return an integer'


def _get_digits_depends(field):
    if isinstance(field.digits, str):
        return {field.digits}
    else:
        return get_eval_fields(field.digits)


class Float(Field):
    '''
    Define a float field (``float``).
    '''
    _type = 'float'
    _sql_type = 'FLOAT'
    _py_type = float

    def __init__(self, string='', digits=None, help='', required=False,
            readonly=False, domain=None, states=None,
            on_change=None, on_change_with=None, depends=None,
            context=None, loading='eager'):
        '''
        :param digits: a list of two integers defining the total
            of digits and the number of decimals of the float.
        '''
        super(Float, self).__init__(string=string, help=help,
            required=required, readonly=readonly, domain=domain, states=states,
            on_change=on_change, on_change_with=on_change_with,
            depends=depends, context=context, loading=loading)
        self.__digits = None
        self.digits = digits

    __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)

    def definition(self, model, language):
        encoder = PYSONEncoder()
        definition = super().definition(model, language)
        definition['digits'] = encoder.encode(self.digits)
        return definition

    @cached_property
    def display_depends(self):
        return super().display_depends | _get_digits_depends(self)

    @cached_property
    def edition_depends(self):
        return super().edition_depends | _get_digits_depends(self)

    @cached_property
    def validation_depends(self):
        return super().validation_depends | _get_digits_depends(self)