From 985f8a4ad0433e3911466394804b24c6a7810af8 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Tue, 4 Jan 2011 16:07:55 +0100 Subject: [PATCH] [IMP] orm: added get_xml_ids() variant to support retrieving multiple IDs Also refactored get_xml_id() bzr revid: odo@openerp.com-20110104150755-6r8r3ohwgen02iq5 --- bin/netsvc.py | 2 +- bin/osv/orm.py | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/bin/netsvc.py b/bin/netsvc.py index 716fe0f5d20..d1375273c31 100644 --- a/bin/netsvc.py +++ b/bin/netsvc.py @@ -476,7 +476,7 @@ class OpenERPDispatcher: def log(self, title, msg, channel=logging.DEBUG_RPC, depth=2): logger = logging.getLogger(title) if logger.isEnabledFor(channel): - for line in pformat(msg, depth=depth).split('\n'): + for line in pformat(msg, depth=100).split('\n'): logger.log(channel, line) def dispatch(self, service_name, method, params): diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 7cf337994d3..20625f7cdc9 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -4175,27 +4175,47 @@ class orm(orm_template): return False return True + def get_xml_ids(self, cr, uid, ids, *args, **kwargs): + """Find out the XML ID(s) of any database record. + + **Synopsis**: ``_get_xml_ids(cr, uid, ids) -> { 'id': ['module.xml_id'] }`` + + :return: map of ids to the list of their fully qualified XML IDs + (empty list when there's none). + """ + model_data_obj = self.pool.get('ir.model.data') + data_ids = model_data_obj.search(cr, uid, [('model', '=', self._name), ('res_id', 'in', ids)]) + data_results = model_data_obj.read(cr, uid, data_ids, ['module', 'name', 'res_id']) + result = {} + for id in ids: + # can't use dict.fromkeys() as the list would be shared! + result[id] = [] + for record in data_results: + result[record['res_id']].append('%(module)s.%(name)s' % record) + return result + def get_xml_id(self, cr, uid, ids, *args, **kwargs): """Find out the XML ID of any database record, if there is one. This method works as a possible implementation for a function field, to be able to add it to any model object easily, referencing it as ``osv.osv.get_xml_id``. + When multiple XML IDs exist for a record, only one + of them is returned (randomly). + **Synopsis**: ``get_xml_id(cr, uid, ids) -> { 'id': 'module.xml_id' }`` - :return: the fully qualified XML ID of the given object, + :return: map of ids to their fully qualified XML ID, defaulting to an empty string when there's none (to be usable as a function field). """ - result = dict.fromkeys(ids, '') - model_data_obj = self.pool.get('ir.model.data') - data_ids = model_data_obj.search(cr, uid, - [('model', '=', self._name), ('res_id', 'in', ids)]) - data_results = model_data_obj.read(cr, uid, data_ids, - ['name', 'module', 'res_id']) - for record in data_results: - result[record['res_id']] = '%(module)s.%(name)s' % record - return result + results = self.get_xml_ids(cr, uid, ids) + for k, v in results.items(): + if results[k]: + results[k] = v[0] + else: + results[k] = '' + return results # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: