@ -51,7 +51,7 @@ from openerp.tools.translate import _
from openerp . tools import float_round , float_repr
from openerp . tools import html_sanitize
import simplejson
from openerp import SUPERUSER_ID
from openerp import SUPERUSER_ID , registry
_logger = logging . getLogger ( __name__ )
@ -334,36 +334,42 @@ class html(text):
import __builtin__
def _symbol_set_float ( self , x ) :
result = __builtin__ . float ( x or 0.0 )
digits = self . digits
if digits :
precision , scale = digits
result = float_repr ( float_round ( result , precision_digits = scale ) , precision_digits = scale )
return result
class float ( _column ) :
_type = ' float '
_symbol_c = ' %s '
_symbol_f = lambda x : __builtin__ . float ( x or 0.0 )
_symbol_set = ( _symbol_c , _symbol_f )
_symbol_get = lambda self , x : x or 0.0
@property
def digits ( self ) :
if self . _digits_compute :
with registry ( ) . cursor ( ) as cr :
return self . _digits_compute ( cr )
else :
return self . _digits
def __init__ ( self , string = ' unknown ' , digits = None , digits_compute = None , required = False , * * args ) :
_column . __init__ ( self , string = string , required = required , * * args )
self . digits = digits
# synopsis: digits_compute(cr) -> (precision, scale)
self . digits_compute = digits_compute
def new ( self , _computed_field = False , * * args ) :
# float columns are database-dependent, so always recreate them
return type ( self ) ( * * args )
self . _digits = digits
self . _digits_compute = digits_compute
self . _symbol_f = lambda x : _symbol_set_float ( self , x )
self . _symbol_set = ( self . _symbol_c , self . _symbol_f )
def to_field_args ( self ) :
args = super ( float , self ) . to_field_args ( )
args [ ' digits ' ] = self . digits_compute or self . digits
args [ ' digits ' ] = self . _ digits_compute or self . _ digits
return args
def digits_change ( self , cr ) :
if self . digits_compute :
self . digits = self . digits_compute ( cr )
if self . digits :
precision , scale = self . digits
self . _symbol_set = ( ' %s ' , lambda x : float_repr ( float_round ( __builtin__ . float ( x or 0.0 ) ,
precision_digits = scale ) ,
precision_digits = scale ) )
pass
class date ( _column ) :
_type = ' date '
@ -1240,10 +1246,22 @@ class function(_column):
# function fields are not copied by default
copy = False
@property
def digits ( self ) :
if self . _digits_compute :
with registry ( ) . cursor ( ) as cr :
return self . _digits_compute ( cr )
else :
return self . _digits
#
# multi: compute several fields in one call
#
def __init__ ( self , fnct , arg = None , fnct_inv = None , fnct_inv_arg = None , type = ' float ' , fnct_search = None , obj = None , store = False , multi = False , * * args ) :
# pop attributes that should not be assigned to self
self . _digits = args . pop ( ' digits ' , ( 16 , 2 ) )
self . _digits_compute = args . pop ( ' digits_compute ' , None )
_column . __init__ ( self , * * args )
self . _obj = obj
self . _fnct = fnct
@ -1253,8 +1271,6 @@ class function(_column):
if ' relation ' in args :
self . _obj = args [ ' relation ' ]
self . digits = args . get ( ' digits ' , ( 16 , 2 ) )
self . digits_compute = args . get ( ' digits_compute ' , None )
if callable ( args . get ( ' selection ' ) ) :
from openerp import api
self . selection = api . expected ( api . cr_uid_context , args [ ' selection ' ] )
@ -1283,6 +1299,10 @@ class function(_column):
self . _symbol_c = char . _symbol_c
self . _symbol_f = lambda x : _symbol_set_char ( self , x )
self . _symbol_set = ( self . _symbol_c , self . _symbol_f )
elif type == ' float ' :
self . _symbol_c = float . _symbol_c
self . _symbol_f = lambda x : _symbol_set_float ( self , x )
self . _symbol_set = ( self . _symbol_c , self . _symbol_f )
else :
type_class = globals ( ) . get ( type )
if type_class is not None :
@ -1304,7 +1324,7 @@ class function(_column):
args = super ( function , self ) . to_field_args ( )
args [ ' store ' ] = bool ( self . store )
if self . _type in ( ' float ' , ) :
args [ ' digits ' ] = self . digits_compute or self . digits
args [ ' digits ' ] = self . _ digits_compute or self . _ digits
elif self . _type in ( ' selection ' , ' reference ' ) :
args [ ' selection ' ] = self . selection
elif self . _type in ( ' many2one ' , ' one2many ' , ' many2many ' ) :
@ -1312,14 +1332,7 @@ class function(_column):
return args
def digits_change ( self , cr ) :
if self . _type == ' float ' :
if self . digits_compute :
self . digits = self . digits_compute ( cr )
if self . digits :
precision , scale = self . digits
self . _symbol_set = ( ' %s ' , lambda x : float_repr ( float_round ( __builtin__ . float ( x or 0.0 ) ,
precision_digits = scale ) ,
precision_digits = scale ) )
pass
def search ( self , cr , uid , obj , name , args , context = None ) :
if not self . _fnct_search :