[IMP] use model._fields instead of model._all_columns to cover all fields
[odoo/odoo.git] / addons / decimal_precision / decimal_precision.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import openerp
23 from openerp import SUPERUSER_ID
24 from openerp import tools
25 from openerp.osv import orm, fields
26 from openerp.modules.registry import RegistryManager
27
28 class decimal_precision(orm.Model):
29     _name = 'decimal.precision'
30     _columns = {
31         'name': fields.char('Usage', select=True, required=True),
32         'digits': fields.integer('Digits', required=True),
33     }
34     _defaults = {
35         'digits': 2,
36     }
37
38     _sql_constraints = [
39         ('name_uniq', 'unique (name)', """Only one value can be defined for each given usage!"""),
40     ]
41
42     @tools.ormcache(skiparg=3)
43     def precision_get(self, cr, uid, application):
44         cr.execute('select digits from decimal_precision where name=%s', (application,))
45         res = cr.fetchone()
46         return res[0] if res else 2
47
48     def clear_cache(self, cr):
49         """clear cache and update models. Notify other workers to restart their registry."""
50         self.precision_get.clear_cache(self)
51         env = openerp.api.Environment(cr, SUPERUSER_ID, {})
52         for model in self.pool.values():
53             for field in model._fields.values():
54                 if field.type == 'float':
55                     field._setup_digits(env)
56         RegistryManager.signal_caches_change(cr.dbname)
57
58     def create(self, cr, uid, data, context=None):
59         res = super(decimal_precision, self).create(cr, uid, data, context=context)
60         self.clear_cache(cr)
61         return res
62
63     def unlink(self, cr, uid, ids, context=None):
64         res = super(decimal_precision, self).unlink(cr, uid, ids, context=context)
65         self.clear_cache(cr)
66         return res
67
68     def write(self, cr, uid, ids, data, *args, **argv):
69         res = super(decimal_precision, self).write(cr, uid, ids, data, *args, **argv)
70         self.clear_cache(cr)
71         return res
72
73
74 def get_precision(application):
75     def change_digit(cr):
76         decimal_precision = openerp.registry(cr.dbname)['decimal.precision']
77         res = decimal_precision.precision_get(cr, SUPERUSER_ID, application)
78         return (16, res)
79     return change_digit
80
81 class DecimalPrecisionFloat(orm.AbstractModel):
82     """ Override qweb.field.float to add a `decimal_precision` domain option
83     and use that instead of the column's own value if it is specified
84     """
85     _inherit = 'ir.qweb.field.float'
86
87
88     def precision(self, cr, uid, field, options=None, context=None):
89         dp = options and options.get('decimal_precision')
90         if dp:
91             return self.pool['decimal.precision'].precision_get(
92                 cr, uid, dp)
93
94         return super(DecimalPrecisionFloat, self).precision(
95             cr, uid, field, options=options, context=context)
96
97 class DecimalPrecisionTestModel(orm.Model):
98     _name = 'decimal.precision.test'
99
100     _columns = {
101         'float': fields.float(),
102         'float_2': fields.float(digits=(16, 2)),
103         'float_4': fields.float(digits=(16, 4)),
104     }
105
106 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: