[MIG] decimal_precision: migrate to new API
This commit is contained in:
committed by
Jérome Maes
parent
8f7b233bc1
commit
df4508635c
@@ -1,5 +1,13 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
from odoo import api, SUPERUSER_ID
|
||||
|
||||
#import decimal_precision
|
||||
from .models.decimal_precision import get_precision
|
||||
from . import models
|
||||
|
||||
def get_precision(application):
|
||||
def change_digit(cr):
|
||||
env = api.Environment(cr, SUPERUSER_ID, {})
|
||||
precision = env['decimal.precision'].precision_get(application)
|
||||
return 16, precision
|
||||
|
||||
return change_digit
|
||||
|
||||
@@ -1,80 +1,66 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import openerp
|
||||
from openerp import SUPERUSER_ID
|
||||
from openerp import tools
|
||||
from openerp.osv import orm, fields
|
||||
from openerp.modules.registry import RegistryManager
|
||||
from odoo import api, fields, models, tools
|
||||
|
||||
class decimal_precision(orm.Model):
|
||||
class DecimalPrecision(models.Model):
|
||||
_name = 'decimal.precision'
|
||||
_columns = {
|
||||
'name': fields.char('Usage', select=True, required=True),
|
||||
'digits': fields.integer('Digits', required=True),
|
||||
}
|
||||
_defaults = {
|
||||
'digits': 2,
|
||||
}
|
||||
|
||||
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, cr, uid, application):
|
||||
cr.execute('select digits from decimal_precision where name=%s', (application,))
|
||||
res = cr.fetchone()
|
||||
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
|
||||
|
||||
def clear_cache(self, cr):
|
||||
""" Deprecated, use `clear_caches` instead. """
|
||||
self.clear_caches()
|
||||
|
||||
def create(self, cr, uid, data, context=None):
|
||||
res = super(decimal_precision, self).create(cr, uid, data, context=context)
|
||||
@api.model
|
||||
def create(self, data):
|
||||
res = super(DecimalPrecision, self).create(data)
|
||||
self.clear_caches()
|
||||
return res
|
||||
|
||||
def unlink(self, cr, uid, ids, context=None):
|
||||
res = super(decimal_precision, self).unlink(cr, uid, ids, context=context)
|
||||
@api.multi
|
||||
def write(self, data):
|
||||
res = super(DecimalPrecision, self).write(data)
|
||||
self.clear_caches()
|
||||
return res
|
||||
|
||||
def write(self, cr, uid, ids, data, *args, **argv):
|
||||
res = super(decimal_precision, self).write(cr, uid, ids, data, *args, **argv)
|
||||
@api.multi
|
||||
def unlink(self):
|
||||
res = super(DecimalPrecision, self).unlink()
|
||||
self.clear_caches()
|
||||
return res
|
||||
|
||||
|
||||
def get_precision(application):
|
||||
def change_digit(cr):
|
||||
decimal_precision = openerp.registry(cr.dbname)['decimal.precision']
|
||||
res = decimal_precision.precision_get(cr, SUPERUSER_ID, application)
|
||||
return (16, res)
|
||||
return change_digit
|
||||
|
||||
class DecimalPrecisionFloat(orm.AbstractModel):
|
||||
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'
|
||||
|
||||
|
||||
def precision(self, cr, uid, field, options=None, context=None):
|
||||
@api.model
|
||||
def precision(self, field, options=None):
|
||||
dp = options and options.get('decimal_precision')
|
||||
if dp:
|
||||
return self.pool['decimal.precision'].precision_get(
|
||||
cr, uid, dp)
|
||||
return self.env['decimal.precision'].precision_get(dp)
|
||||
|
||||
return super(DecimalPrecisionFloat, self).precision(
|
||||
cr, uid, field, options=options, context=context)
|
||||
return super(DecimalPrecisionFloat, self).precision(field, options=options)
|
||||
|
||||
class DecimalPrecisionTestModel(orm.Model):
|
||||
class DecimalPrecisionTestModel(models.Model):
|
||||
_name = 'decimal.precision.test'
|
||||
|
||||
_columns = {
|
||||
'float': fields.float(),
|
||||
'float_2': fields.float(digits=(16, 2)),
|
||||
'float_4': fields.float(digits=(16, 4)),
|
||||
}
|
||||
float = fields.Float()
|
||||
float_2 = fields.Float(digits=(16, 2))
|
||||
float_4 = fields.Float(digits=(16, 4))
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from openerp.tests import common
|
||||
|
||||
|
||||
class TestFloatExport(common.TransactionCase):
|
||||
def setUp(self):
|
||||
super(TestFloatExport, self).setUp()
|
||||
self.Model = self.registry('decimal.precision.test')
|
||||
|
||||
def get_converter(self, name):
|
||||
float_obj = self.registry('ir.qweb.field.float')
|
||||
_, precision = self.Model._fields[name].digits or (None, None)
|
||||
FloatField = self.env['ir.qweb.field.float']
|
||||
_, precision = self.env['decimal.precision.test']._fields[name].digits or (None, None)
|
||||
|
||||
def converter(value, options=None):
|
||||
record = self.Model.new(self.cr, self.uid, {name: value}, context=None)
|
||||
return float_obj.record_to_html(
|
||||
self.cr, self.uid, record, name, options or {}, context=None)
|
||||
record = self.env['decimal.precision.test'].new({name: value})
|
||||
return FloatField.record_to_html(record, name, options or {})
|
||||
return converter
|
||||
|
||||
def test_basic_float(self):
|
||||
@@ -42,12 +39,11 @@ class TestFloatExport(common.TransactionCase):
|
||||
'42.1235')
|
||||
|
||||
def test_precision_domain(self):
|
||||
DP = self.registry('decimal.precision')
|
||||
DP.create(self.cr, self.uid, {
|
||||
self.env['decimal.precision'].create({
|
||||
'name': 'A',
|
||||
'digits': 2,
|
||||
})
|
||||
DP.create(self.cr, self.uid, {
|
||||
self.env['decimal.precision'].create({
|
||||
'name': 'B',
|
||||
'digits': 6,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user