From 1e57f21cb772a8f9f54a26d9d101d9ba0ef3121a Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Thu, 7 Jul 2011 15:37:33 +0200 Subject: [PATCH] [IMP] openerp/osv/fields.py: reading a property field is now using the function get() of ir.property object. bzr revid: qdp-launchpad@openerp.com-20110707133733-aua4dygdjjmhryap --- openerp/osv/fields.py | 80 ++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index a9fd20e2a65..901f4951cef 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -1212,6 +1212,7 @@ class serialized(_column): self._symbol_get = self._deserialize_func super(serialized, self).__init__(string=string, **args) +import orm # TODO: review completly this class for speed improvement class property(function): @@ -1219,21 +1220,21 @@ class property(function): def _get_default(self, obj, cr, uid, prop_name, context=None): return self._get_defaults(obj, cr, uid, [prop_name], context=None)[0][prop_name] - def _get_defaults(self, obj, cr, uid, prop_name, context=None): + def _get_defaults(self, obj, cr, uid, prop_names, context=None): + """ + This function receives a list of property field name and returns a dictionary with + KEY: name of property fields + VALUE: the result of ir.property.get() function for res_id = False + """ prop = obj.pool.get('ir.property') - domain = [('fields_id.model', '=', obj._name), ('fields_id.name','in',prop_name), ('res_id','=',False)] - ids = prop.search(cr, uid, domain, context=context) replaces = {} - default_value = {}.fromkeys(prop_name, False) - for prop_rec in prop.browse(cr, uid, ids, context=context): - if default_value.get(prop_rec.fields_id.name, False): - continue - value = prop.get_by_record(cr, uid, prop_rec, context=context) or False - default_value[prop_rec.fields_id.name] = value - if value and (prop_rec.type == 'many2one'): - replaces.setdefault(value._name, {}) - replaces[value._name][value.id] = True - return default_value, replaces + default_value = {}.fromkeys(prop_names, False) + for prop_name in prop_names: + for value in [prop.get(cr, uid, prop_name, obj._name, context=context)]: + if isinstance(value, orm.browse_null): + continue + default_value[prop_name] = value + return default_value def _get_by_id(self, obj, cr, uid, prop_name, ids, context=None): prop = obj.pool.get('ir.property') @@ -1274,48 +1275,35 @@ class property(function): }, context=context) return False - - def _fnct_read(self, obj, cr, uid, ids, prop_name, obj_dest, context=None): + def _fnct_read(self, obj, cr, uid, ids, prop_names, obj_dest, context=None): properties = obj.pool.get('ir.property') - domain = [('fields_id.model', '=', obj._name), ('fields_id.name','in',prop_name)] - domain += [('res_id','in', [obj._name + ',' + str(oid) for oid in ids])] - nids = properties.search(cr, uid, domain, context=context) - default_val,replaces = self._get_defaults(obj, cr, uid, prop_name, context) + #get the default values (for res_id = False) for the property fields + default_val = self._get_defaults(obj, cr, uid, prop_names, context) + #build the dictionary that will be returned res = {} for id in ids: res[id] = default_val.copy() - brs = properties.browse(cr, uid, nids, context=context) - for prop in brs: - value = properties.get_by_record(cr, uid, prop, context=context) - res[prop.res_id.id][prop.fields_id.name] = value or False - if value and (prop.type == 'many2one'): - # check existence as root, as seeing the name of a related - # object depends on access right of source document, - # not target, so user may not have access. - record_exists = obj.pool.get(value._name).exists(cr, 1, value.id) - if record_exists: - replaces.setdefault(value._name, {}) - replaces[value._name][value.id] = True - else: - res[prop.res_id.id][prop.fields_id.name] = False - - for rep in replaces: - # search+name_get as root, as seeing the name of a related - # object depends on access right of source document, - # not target, so user may not have access. - nids = obj.pool.get(rep).search(cr, 1, [('id','in',replaces[rep].keys())], context=context) - replaces[rep] = dict(obj.pool.get(rep).name_get(cr, 1, nids, context=context)) - - for prop in prop_name: + for prop in prop_names: + #if the property field is a m2o field, we will append the id of the value to name_get_list_ids + #in order to make a name_get in batch for all the ids needed. + name_get_list_ids = [] for id in ids: - if res[id][prop] and hasattr(res[id][prop], '_name'): - res[id][prop] = (res[id][prop].id , replaces[res[id][prop]._name].get(res[id][prop].id, False)) - + #get the result of ir.property.get() for this res_id and save it in res if it's existing + value = properties.get(cr, uid, prop, obj._name, res_id=id, context=context) + if value: + res[id][prop] = value + if isinstance(default_val[prop], orm.browse_record): + name_get_list_ids.append(res[id][prop].id) + if isinstance(default_val[prop], orm.browse_record): + name_get_values = dict(obj.pool.get(default_val[prop]._name).name_get(cr, uid, name_get_list_ids, context=context)) + #if the property field is a m2o, we need to return a tuple with (id, name) + for id in ids: + if res[id][prop] and isinstance(res[id][prop], orm.browse_record): + res[id][prop] = (res[id][prop].id , name_get_values.get(res[id][prop].id, False)) return res - def _field_get(self, cr, uid, model_name, prop): if not self.field_id.get(cr.dbname): cr.execute('SELECT id \