From ca338643f2dc319b6f140e6f4a2a083021452c94 Mon Sep 17 00:00:00 2001 From: ced <> Date: Fri, 23 Feb 2007 16:05:32 +0000 Subject: [PATCH] KERNEL: add store option to fields function bzr revid: ced-1c0292e8f8d6f26be64c449112686c3e164cf872 --- bin/osv/fields.py | 7 ++++++- bin/osv/orm.py | 27 +++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/bin/osv/fields.py b/bin/osv/fields.py index 752b87bff5c..fed5de780e9 100644 --- a/bin/osv/fields.py +++ b/bin/osv/fields.py @@ -429,7 +429,7 @@ class function(_column): _type = 'function' _properties = True - def __init__(self, fnct, arg=None, fnct_inv=None, fnct_inv_arg=None, type='float', fnct_search=None, obj=None, method=False, **args): + def __init__(self, fnct, arg=None, fnct_inv=None, fnct_inv_arg=None, type='float', fnct_search=None, obj=None, method=False, store=False, **args): _column.__init__(self, **args) self._obj = obj self._method = method @@ -443,6 +443,11 @@ class function(_column): self.readonly = 1 self._type = type self._fnct_search = fnct_search + self.store = store + if type == 'float': + self._symbol_c = '%f' + self._symbol_f = lambda x: __builtin__.float(x or 0.0) + self._symbol_set = (self._symbol_c, self._symbol_f) def search(self, cr, uid, obj, name, args): if not self._fnct_search: diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 6853cb6b973..480adabfdba 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -233,6 +233,11 @@ def get_pg_type(f): f_type = ('int4', 'INTEGER') else: f_type = ('varchar', 'VARCHAR(%d)' % f_size) + elif isinstance(f, fields.function) and type_dict.has_key(type(locals().get('fields.'+(f._type)))): + t=type(locals().get('fields.'+(f._type))) + f_type = (type_dict[t], type_dict[t]) + elif isinstance(f, fields.function) and f._type == 'float': + f_type = ('float8', 'DOUBLE PRECISION') else: print "WARNING: type not supported!" f_type = None @@ -348,7 +353,7 @@ class orm(object): cr.execute(q) res = cr.dictfetchall() if not res: - if not isinstance(f,fields.function): + if not isinstance(f,fields.function) or f.store: # add the missing field cr.execute("ALTER TABLE %s ADD COLUMN %s %s" % (self._table, k, get_pg_type(f)[1])) @@ -387,7 +392,7 @@ class orm(object): f_pg_type = f_pg_def['typname'] f_pg_size = f_pg_def['size'] f_pg_notnull = f_pg_def['attnotnull'] - if isinstance(f, fields.function): + if isinstance(f, fields.function) and not f.store: print '-'*60 print "WARNING: column %s (%s) in table %s was converted to a function !" % (k, f.string, self._table) print "\tYou should remove this column from your database." @@ -887,6 +892,7 @@ class orm(object): wf_service = netsvc.LocalService("workflow") for id in ids: wf_service.trg_write(user, self._name, id, cr) + self._update_function_stored(cr, user, ids, context=context) return True # @@ -961,8 +967,25 @@ class orm(object): wf_service = netsvc.LocalService("workflow") wf_service.trg_create(user, self._name, id_new, cr) + self._update_function_stored(cr, user, [id_new], context=context) return id_new + def _update_function_stored(self, cr, user, ids, context={}): + f=filter(lambda a: isinstance(self._columns[a], fields.function) and self._columns[a].store, self._columns) + if f: + result=self.read(cr, user, ids, fields=f, context=context) + for res in result: + upd0=[] + upd1=[] + for field in res: + if field not in f: + continue + upd0.append(field+'='+self._columns[field]._symbol_set[0]) + upd1.append(self._columns[field]._symbol_set[1](res[field])) + upd1.append(res['id']) + cr.execute('update '+self._table+' set '+string.join(upd0,',')+ ' where id = %d', upd1) + return True + # # TODO: Validate #