Improve fields Function that have the same function

Call only once the function with a list of names instead of name.
It will works only if the names is in the signature of the function and if all
the fields Function have the same arg.
This commit is contained in:
Cédric Krier committed 2008-08-19 15:34:28 +02:00
1 parent d1c0032ce2
commit 151f4cd600
2 files changed
+32 -2

No files matched your search

+18 -2
View File
@@ -27,6 +27,7 @@ import psycopg2
import warnings
import __builtin__
import sha
import inspect
def _symbol_f(symb):
if symb is None or symb == False:
@@ -628,8 +629,23 @@ class Function(Column):
def get(self, cursor, obj, ids, name, user=None, offset=0, context=None,
values=None):
return getattr(obj, self._fnct)(cursor, user, ids, name, self._arg,
context=context)
if isinstance(name, list):
names = name
# Test is the function works with a list of names
if 'names' in inspect.getargspec(getattr(obj, self._fnct))[0]:
return getattr(obj, self._fnct)(cursor, user, ids, names,
self._arg, context=context)
res = {}
for name in names:
res[name] = getattr(obj, self._fnct)(cursor, user, ids, name,
self._arg, context=context)
return res
else:
# Test is the function works with a list of names
if 'names' in inspect.getargspec(getattr(obj, self._fnct))[0]:
name = [name]
return getattr(obj, self._fnct)(cursor, user, ids, name, self._arg,
context=context)
def set(self, cursor, obj, obj_id, name, value, user=None, context=None):
if self._fnct_inv:
+14
View File
@@ -1035,12 +1035,26 @@ class ORM(object):
# whose name is in load is False
fields_post = [x for x in fields_names if x in self._columns \
and not getattr(self._columns[x], load)]
func_fields = {}
for field in fields_post:
if isinstance(self._columns[field], fields.Function):
key = (self._columns[field]._fnct, self._columns[field]._arg)
func_fields.setdefault(key, [])
func_fields[key].append(field)
continue
# get the value of that field for all records/ids
res2 = self._columns[field].get(cursor, self, ids, field, user,
context=context, values=res)
for record in res:
record[field] = res2[record['id']]
for i in func_fields:
field_list = func_fields[i]
field = field_list[0]
res2 = self._columns[field].get(cursor, self, ids, field_list, user,
context=context, values=res)
for field in res2:
for record in res:
record[field] = res2[field][record['id']]
return res
def _validate(self, cursor, user, ids, context=None):