[ADD] ir.model.data: added get_object_reference() and get_object() convenience method...
authorOlivier Dony <odo@openerp.com>
Fri, 9 Jul 2010 11:02:52 +0000 (13:02 +0200)
committerOlivier Dony <odo@openerp.com>
Fri, 9 Jul 2010 11:02:52 +0000 (13:02 +0200)
bzr revid: odo@openerp.com-20100709110252-fwn0osdcgnk5f88g

bin/addons/base/ir/ir_model.py

index 2057454..2e9d4fd 100644 (file)
@@ -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: