[MERGE] [FORWARD] Forward port of addons 7.0 until revision 9008
[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 osv, fields
26
27 class decimal_precision(osv.osv):
28     _name = 'decimal.precision'
29     _columns = {
30         'name': fields.char('Usage', size=50, select=True, required=True),
31         'digits': fields.integer('Digits', required=True),
32     }
33     _defaults = {
34         'digits': 2,
35     }
36
37     _sql_constraints = [
38         ('name_uniq', 'unique (name)', """Only one value can be defined for each given usage!"""),
39     ]
40
41     @tools.ormcache(skiparg=3)
42     def precision_get(self, cr, uid, application):
43         cr.execute('select digits from decimal_precision where name=%s', (application,))
44         res = cr.fetchone()
45         return res[0] if res else 2
46
47     def create(self, cr, uid, data, context=None):
48         res = super(decimal_precision, self).create(cr, uid, data, context=context)
49         self.precision_get.clear_cache(self)
50         return res
51
52     def unlink(self, cr, uid, ids, context=None):
53         res = super(decimal_precision, self).unlink(cr, uid, ids, context=context)
54         self.precision_get.clear_cache(self)
55         return res
56
57     def write(self, cr, uid, ids, data, *args, **argv):
58         res = super(decimal_precision, self).write(cr, uid, ids, data, *args, **argv)
59         self.precision_get.clear_cache(self)
60         for obj in self.pool.obj_list():
61             for colname, col in self.pool[obj]._columns.items():
62                 if isinstance(col, (fields.float, fields.function)):
63                     col.digits_change(cr)
64         return res
65
66 decimal_precision()
67
68 def get_precision(application):
69     def change_digit(cr):
70         decimal_precision = openerp.registry(cr.dbname)['decimal.precision']
71         res = decimal_precision.precision_get(cr, SUPERUSER_ID, application)
72         return (16, res)
73     return change_digit
74
75 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: