Cédric Krier pushed to branch branch/default at Tryton / Tryton
Commits: be7a865f by Cédric Krier at 2023-04-14T13:09:23+02:00 Convert instances in RPC call result for on_change_with on field Since d142b33fa3ab0fe5af25957f90ee77fe7eee51b3 the on_change_with method can return instances. Closes #12217 - - - - - f4f2af12 by Cédric Krier at 2023-04-15T09:15:46+02:00 Reuse on_change_with_result to convert result of Function field getter - - - - - 2 changed files: - trytond/trytond/model/fields/field.py - trytond/trytond/model/fields/function.py Changes: ===================================== trytond/trytond/model/fields/field.py ===================================== @@ -1,7 +1,7 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. import warnings -from functools import wraps +from functools import partial, wraps import sql from sql import ( @@ -195,6 +195,20 @@ return record._changed_values +def on_change_with_result(field, value): + from ..modelstorage import ModelStorage + if field._type in {'many2one', 'one2one', 'reference'}: + if isinstance(value, ModelStorage): + if field._type == 'reference': + value = str(value) + else: + value = value.id + elif field._type in {'one2many', 'many2many'}: + if isinstance(value, (list, tuple)): + value = [int(r) for r in value] + return value + + def domain_method(func): @wraps(func) def wrapper(self, domain, tables, Model): @@ -455,7 +469,7 @@ def set_rpc(self, model): for attribute, result in ( ('on_change', on_change_result), - ('on_change_with', None), + ('on_change_with', partial(on_change_with_result, self)), ): if not getattr(self, attribute): continue ===================================== trytond/trytond/model/fields/function.py ===================================== @@ -9,7 +9,7 @@ from trytond.tools import is_instance_method from trytond.transaction import Transaction, without_check_access -from .field import Field, domain_method +from .field import Field, domain_method, on_change_with_result def getter_context(func): @@ -108,7 +108,6 @@ If the function has ``names`` in the function definition then it will call it with a list of name. ''' - from ..modelstorage import ModelStorage method = getattr(Model, self.getter) instance_method = is_instance_method(Model, self.getter) multiple = self.getter_multiple(method) @@ -123,17 +122,5 @@ 'one2many', 'many2many', 'one2one'}: record._local_cache[record.id][fname] = val - def convert(value): - if self._type in {'many2one', 'one2one', 'reference'}: - if isinstance(value, ModelStorage): - if self._type == 'reference': - value = str(value) - else: - value = value.id - elif self._type in {'one2many', 'many2many'}: - if isinstance(value, (list, tuple)): - value = [int(r) for r in value] - return value - def call(name): if not instance_method: @@ -138,4 +125,4 @@ def call(name): if not instance_method: - return convert(method(records, name)) + return on_change_with_result(self, method(records, name)) else: @@ -141,5 +128,7 @@ else: - return dict((r.id, convert(method(r, name))) for r in records) + return { + r.id: on_change_with_result(self, method(r, name)) + for r in records} if isinstance(name, list): names = name if multiple: View it on Heptapod: https://foss.heptapod.net/tryton/tryton/-/compare/d800175396c4cc26de6fee738bd459d686070199...f4f2af12d6d93dd00090c3ccddfdade9707cd99d -- View it on Heptapod: https://foss.heptapod.net/tryton/tryton/-/compare/d800175396c4cc26de6fee738bd459d686070199...f4f2af12d6d93dd00090c3ccddfdade9707cd99d You're receiving this email because of your account on foss.heptapod.net.