From da08ca7756460fd8d6342d0b222a3a3b0b705ddb Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Fri, 9 Jul 2010 13:02:52 +0200 Subject: [PATCH] [ADD] ir.model.data: added get_object_reference() and get_object() convenience methods to make target object easier to obtain bzr revid: odo@openerp.com-20100709110252-fwn0osdcgnk5f88g --- bin/addons/base/ir/ir_model.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/bin/addons/base/ir/ir_model.py b/bin/addons/base/ir/ir_model.py index 20574545fbc..2e9d4fd64f5 100644 --- a/bin/addons/base/ir/ir_model.py +++ b/bin/addons/base/ir/ir_model.py @@ -488,12 +488,25 @@ class ir_model_data(osv.osv): @tools.cache() def _get_id(self, cr, uid, module, xml_id): - ids = self.search(cr, uid, [('module','=',module),('name','=', xml_id)]) + """Returns the id of the ir.model.data record corresponding to a given module and xml_id (cached) or raise a ValueError if not found""" + ids = self.search(cr, uid, [('module','=',module), ('name','=', xml_id)]) if not ids: raise ValueError('No references to %s.%s' % (module, xml_id)) # the sql constraints ensure us we have only one result return ids[0] + @tools.cache() + def get_object_reference(self, cr, uid, module, xml_id): + """Returns (model, res_id) corresponding to a given module and xml_id (cached) or raise ValueError if not found""" + data_id = self._get_id(cr, uid, module, xml_id) + res = self.read(cr, uid, data_id, ['model', 'res_id']) + return (res['model'], res['res_id']) + + def get_object(self, cr, uid, module, xml_id, context=None): + """Returns a browsable record for the given module name and xml_id or raise ValueError if not found""" + res_model, res_id = self.get_object_reference(cr, uid, module, xml_id) + return self.pool.get(res_model).browse(cr, uid, res_id, context=context) + def _update_dummy(self,cr, uid, model, module, xml_id=False, store=True): if not xml_id: return False @@ -525,6 +538,7 @@ class ir_model_data(osv.osv): result3 = cr.fetchone() if not result3: self._get_id.clear_cache(cr.dbname, uid, module, xml_id) + self.get_object_reference.clear_cache(cr.dbname, uid, module, xml_id) cr.execute('delete from ir_model_data where id=%s', (action_id2,)) res_id = False else: