From 9e8ffebdfa6645e59a9bfb50bb0005e2e07966b2 Mon Sep 17 00:00:00 2001 From: "ced@b2ck.com" Date: Wed, 12 Dec 2007 20:49:52 +0100 Subject: [PATCH] Fix property behavior - add properties in default_get - remove buggy cache - don't return BrowseNull for browse - factory the code to put all functions into ir.property object --- trytond/ir/property.py | 90 ++++++++++++++++++++++++++++++++++++------ trytond/osv/fields.py | 77 +++--------------------------------- trytond/osv/orm.py | 20 +++++----- 3 files changed, 94 insertions(+), 93 deletions(-) diff --git a/trytond/ir/property.py b/trytond/ir/property.py index e30e113a6a..4fad2e915c 100644 --- a/trytond/ir/property.py +++ b/trytond/ir/property.py @@ -52,18 +52,82 @@ class Property(OSV): res = super(Property, self).unlink(cursor, user, ids, context) return res - def get(self, cursor, user, name, model, res_id=False, context=None): - cursor.execute('SELECT id FROM ir_model_fields ' \ - 'WHERE name = %s AND model = %s', (name, model)) - res = cursor.fetchone() - if res: - nid = self.search(cursor, user, [ - ('fields_id', '=', res[0]), - ('res_id', '=', res_id), - ]) - if nid: - val = self.browse(cursor, user, nid[0], context).value - return (val and int(val.split(',')[1])) or False - return False + def get(self, cursor, user, name, model, res_ids=None, context=None): + """ + Return property value for each res_ids + name: property name + model: object name + """ + model_fields_obj = self.pool.get('ir.model.fields') + res = {} + + fields_id = model_fields_obj.search(cursor, user, [ + ('name', '=', name), + ('model', '=', model), + ], limit=1, context=context)[0] + + default_id = self.search(cursor, user, [ + ('fields_id', '=', fields_id), + ('res_id', '=', False), + ], limit=1, context=context) + default_val = False + if default_id: + value = self.browse(cursor, user, default_id[0], + context=context).value + default_val = (value and int(value.split(',')[1])) or False + + if not res_ids: + return default_val + + for obj_id in res_ids: + res[obj_id] = default_val + + property_ids = self.search(cursor, user, [ + ('fields_id', '=', fields_id), + ('res_id', 'in', [name + ',' + str(obj_id) \ + for obj_id in res_ids]), + ]) + for prop in self.browse(cursor, user, property_ids): + res[int(prop.res_id.split(',')[1])] = (prop.value and \ + int(prop.value.split(',')[1])) or False + + return res + + def set(self, cursor, user, name, model, res_id, val, context=None): + """ + Set property value for res_id + """ + model_fields_obj = self.pool.get('ir.model.fields') + fields_id = model_fields_obj.search(cursor, user, [ + ('name', '=', name), + ('model', '=', model), + ], limit=1, context=context)[0] + + property_ids = self.search(cursor, user, [ + ('fields_id', '=', fields_id), + ('res_id', '=', name + ',' + str(res_id)), + ], context=context) + self.unlink(cursor, property_ids, context=context) + + default_id = self.search(cursor, user, [ + ('fields_id', '=', fields_id), + ('res_id', '=', False), + ], limit=1, context=context) + default_id = False + if default_id: + default_val = self.browse(cursor, user, default_id[0], + context=context).value + +# company_id = obj.pool.get('res.users').company_get(cursor, user, user) + res = False + if (val != default_val): + res = self.create(cursor, user, { + 'name': name, + 'value': val, + 'res_id': model + ',' + str(res_id), +# 'company_id': company_id, + 'fields_id': fields_id, + }, context=context) + return res Property() diff --git a/trytond/osv/fields.py b/trytond/osv/fields.py index a623b9b6c5..268f3810ba 100644 --- a/trytond/osv/fields.py +++ b/trytond/osv/fields.py @@ -68,9 +68,6 @@ class _column(object): if args[i]: setattr(self, i, args[i]) - def restart(self): - pass - def set(self, cursor, obj, obj_id, name, value, user=None, context=None): raise Exception, 'undefined get method !' @@ -546,63 +543,14 @@ class Property(function): def _fnct_write(self, obj, cursor, user, obj_id, prop, id_val, val, context=None): - if context is None: - context = {} - (obj_dest,) = val - definition_id = self._field_get(cursor, user, obj._name, prop) - - property = obj.pool.get('ir.property') - nid = property.search(cursor, user, [('fields_id', '=', definition_id), - ('res_id', '=', obj._name + ',' + str(obj_id))]) - # TODO remove query from while statement - while len(nid): - cursor.execute('DELETE FROM ir_property WHERE id = %d', - (nid.pop(),)) - - nid = property.search(cursor, user, [('fields_id', '=', definition_id), - ('res_id', '=', False)]) - default_val = False - if nid: - default_val = property.browse(cursor, user, nid[0], context).value - - company_id = obj.pool.get('res.users').company_get(cursor, user, user) - res = False - newval = (id_val and obj_dest + ',' + str(id_val)) or False - if (newval != default_val) and newval: - propdef = obj.pool.get('ir.model.fields').browse(cursor, user, - definition_id, context=context) - res = property.create(cursor, user, { - 'name': propdef.name, - 'value': newval, - 'res_id': obj._name + ',' + str(obj_id), - 'company_id': company_id, - 'fields_id': definition_id, - }, context=context) - return res + property_obj = obj.pool.get('ir.property') + return property_obj.set(cursor, user, prop, obj._name, obj_id, + (id_val and val + ',' + str(id_val)) or False, context=context) def _fnct_read(self, obj, cursor, user, ids, prop, val, context=None): - if context is None: - context = {} - property = obj.pool.get('ir.property') - definition_id = self._field_get(cursor, user, obj._name, prop) - - nid = property.search(cursor, user, [('fields_id', '=', definition_id), - ('res_id', '=', False)]) - default_val = False - if nid: - value = property.browse(cursor, user, nid[0], context).value - default_val = (value and int(value.split(',')[1])) or False - - vids = [obj._name + ',' + str(obj_id) for obj_id in ids] - nids = property.search(cursor, user, [('fields_id', '=', definition_id), - ('res_id', 'in', vids)]) - - res = {} - for obj_id in ids: - res[obj_id] = default_val - for prop in property.browse(cursor, user, nids): - res[int(prop.res_id.split(',')[1])] = (prop.value and \ - int(prop.value.split(',')[1])) or False + property_obj = obj.pool.get('ir.property') + res = property_obj.get(cursor, user, prop, obj._name, ids, + context=context) obj = obj.pool.get(self._obj) names = obj.name_get(cursor, user, res.values(), context=context) @@ -613,21 +561,8 @@ class Property(function): res[i] = False return res - def _field_get(self, cursor, user, model_name, prop): - if not self.field_id.get(cursor.dbname): - cursor.execute('SELECT id ' \ - 'FROM ir_model_fields ' \ - 'WHERE name = %s AND model = %s', (prop, model_name)) - res = cursor.fetchone() - self.field_id[cursor.dbname] = res and res[0] - return self.field_id[cursor.dbname] - def __init__(self, obj_prop, **args): - self.field_id = {} function.__init__(self, self._fnct_read, False, self._fnct_write, (obj_prop, ), **args) - def restart(self): - self.field_id = {} - property = Property diff --git a/trytond/osv/orm.py b/trytond/osv/orm.py index 421dd95371..3498c05737 100644 --- a/trytond/osv/orm.py +++ b/trytond/osv/orm.py @@ -661,8 +661,6 @@ class ORM(object): assert (k in self._columns) or (k in self._inherit_fields), \ 'Default function defined in %s but field %s does not exist!' % \ (self._name, k,) - for field in self._columns: - self._columns[field].restart() # FIXME: does not work at all # if self._log_access: # self._columns.update({ @@ -709,13 +707,9 @@ class ORM(object): if isinstance(select, (int, long)): return BrowseRecord(cursor, user, select, self, cache, context=context, list_class=list_class) - elif isinstance(select, list): - return list_class([BrowseRecord(cursor, user, x, self, cache, - context=context, list_class=list_class) for x in select], - context) - else: - # XXX raise exception? - return BrowseNull() + return list_class([BrowseRecord(cursor, user, x, self, cache, + context=context, list_class=list_class) for x in select], + context) def __export_row(self, cursor, user, row, fields_names, context=None): lines = [] @@ -1100,6 +1094,14 @@ class ORM(object): if field in self._defaults: value[field] = self._defaults[field](self, cursor, user, context) + fld_def = ((field in self._columns) and self._columns[field]) \ + or ((field in self._inherit_fields) \ + and self._inherit_fields[field][2]) \ + or False + if isinstance(fld_def, fields.Property): + property_obj = self.pool.get('ir.property') + value[field] = property_obj.get(cursor, user, field, + self._name) # get the default values set by the user and override the default # values defined in the object