From 5db2dc40256d984430fe1da9de39448c02a62455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 19 Mar 2010 15:17:50 +0100 Subject: [PATCH] Models that uses _inherits will search in inherited parents for missing functions This will prevent the needs to define on_change_* function and button method in the child Model. In case of diamond problem, it is the first parent in the dictionary that will be taken. --- CHANGELOG | 1 + trytond/model/model.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index f4f7f90588..56e38b1a2c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Models that uses _inherits will search in inherited parents for missing functions * Remove ids from on_change* calls * Improve search on translatable fields * export_data return empty value for invisible fields diff --git a/trytond/model/model.py b/trytond/model/model.py index d21bfca3b1..24a4b23e14 100644 --- a/trytond/model/model.py +++ b/trytond/model/model.py @@ -113,6 +113,35 @@ class Model(object): if on_change_with not in self._rpc: self._rpc[on_change_with] = False + def __getattr__(self, name): + try: + super(Model, self).__getattr__(name) + except AttributeError: + # Search if a function exists in inherits parents + for model_name, field_name in self._inherits.iteritems(): + model_obj = self.pool.get(model_name) + if hasattr(model_obj, name) and \ + callable(getattr(model_obj, name)): + def func(cursor, user, *args, **kwargs): + if args: + ids = args[0] + int_id = False + if isinstance(ids, (int, long)): + int_id = True + ids = [ids] + if reduce(lambda x, y: x and + isinstance(y, (int, long)), ids, True): + # Replace ids by the parent ids + ids = [getattr(x, field_name).id for x in + self.browse(cursor, user, ids)] + if int_id: + ids = ids[0] + args = tuple([ids] + list(args[1:])) + return getattr(model_obj, name)(cursor, user, *args, + **kwargs) + return func + raise + def _inherits_reload(self): """ Reconstruct _inherit_fields