@ -2,22 +2,24 @@
# Copyright 2016-2017 Camptocamp (http://www.camptocamp.com/)
# Copyright 2016-2017 Camptocamp (http://www.camptocamp.com/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api , models
from odoo import api , models
class Base ( models . AbstractModel ) :
_inherit = ' base '
def get_new_values ( model , record , on_change_result ) :
@api.model
def _get_new_values ( self , record , on_change_result ) :
vals = on_change_result . get ( ' value ' , { } )
vals = on_change_result . get ( ' value ' , { } )
new_values = { }
new_values = { }
for fieldname , value in vals . items ( ) :
for fieldname , value in vals . iter items( ) :
if fieldname not in record :
if fieldname not in record :
column = model . _fields [ fieldname ]
column = self . _fields [ fieldname ]
if value and column . type == ' many2one ' :
if value and column . type == ' many2one ' :
value = value [ 0 ] # many2one are tuple (id, name)
value = value [ 0 ] # many2one are tuple (id, name)
new_values [ fieldname ] = value
new_values [ fieldname ] = value
return new_values
return new_values
@api.model
def play_onchanges ( self , values , onchange_fields ) :
def play_onchanges ( self , values , onchange_fields ) :
onchange_specs = self . _onchange_spec ( )
onchange_specs = self . _onchange_spec ( )
# we need all fields in the dict even the empty ones
# we need all fields in the dict even the empty ones
@ -27,25 +29,25 @@ def play_onchanges(self, values, onchange_fields):
if field not in all_values :
if field not in all_values :
all_values [ field ] = False
all_values [ field ] = False
# we work on a temporary record
new_record = self . new ( all_values )
# If self is a record (play onchange on existing record)
# we take the value of the field
# If self is an empty record we will have an empty value
if self :
self . ensure_one ( )
record_values = self . _convert_to_write ( self . read ( ) [ 0 ] )
else :
record_values = { }
for field in self . _fields :
if field not in all_values :
all_values [ field ] = record_values . get ( field , False )
new_values = { }
new_values = { }
for field in onchange_fields :
for field in onchange_fields :
onchange_values = new_record . onchange ( all_values ,
field , onchange_specs )
new_values . update ( get_new_values ( self , values , onchange_values ) )
onchange_values = self . onchange ( all_values , field , onchange_specs )
new_values . update ( self . _get_new_values ( values , onchange_values ) )
all_values . update ( new_values )
all_values . update ( new_values )
res = { f : v for f , v in all_values . items ( )
if f in values or f in new_values }
return res
class Base ( models . AbstractModel ) :
_inherit = ' base '
def _setup_complete ( self ) :
if not hasattr ( models . BaseModel , ' play_onchanges ' ) :
setattr ( models . BaseModel , ' play_onchanges ' , play_onchanges )
return super ( Base , self ) . _setup_complete ( )
return {
f : v for f , v in all_values . iteritems ( )
if not self . _fields [ f ] . compute
and ( f in values or f in new_values ) }