-
Notifications
You must be signed in to change notification settings - Fork 23k
/
decimal_precision.py
71 lines (53 loc) · 2.21 KB
/
decimal_precision.py
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
# -*- encoding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, tools
import odoo.addons
import logging
import sys
_logger = logging.getLogger(__name__)
def get_precision(application):
_logger.warning("Deprecated call to decimal_precision.get_precision(<application>), use digits=<application> instead")
return application
class DecimalPrecision(models.Model):
_name = 'decimal.precision'
_description = 'Decimal Precision'
name = fields.Char('Usage', index=True, required=True)
digits = fields.Integer('Digits', required=True, default=2)
_sql_constraints = [
('name_uniq', 'unique (name)', """Only one value can be defined for each given usage!"""),
]
@api.model
@tools.ormcache('application')
def precision_get(self, application):
self.env.cr.execute('select digits from decimal_precision where name=%s', (application,))
res = self.env.cr.fetchone()
return res[0] if res else 2
@api.model_create_multi
def create(self, vals_list):
res = super(DecimalPrecision, self).create(vals_list)
self.clear_caches()
return res
def write(self, data):
res = super(DecimalPrecision, self).write(data)
self.clear_caches()
return res
def unlink(self):
res = super(DecimalPrecision, self).unlink()
self.clear_caches()
return res
class DecimalPrecisionFloat(models.AbstractModel):
""" Override qweb.field.float to add a `decimal_precision` domain option
and use that instead of the column's own value if it is specified
"""
_inherit = 'ir.qweb.field.float'
@api.model
def precision(self, field, options=None):
dp = options and options.get('decimal_precision')
if dp:
return self.env['decimal.precision'].precision_get(dp)
return super().precision(field, options=options)
# compatibility for decimal_precision.get_precision(): expose the module in addons namespace
dp = sys.modules['odoo.addons.base.models.decimal_precision']
odoo.addons.decimal_precision = dp
sys.modules['odoo.addons.decimal_precision'] = dp
sys.modules['openerp.addons.decimal_precision'] = dp