Files
tryton/trytond/model/fields/function.py
T

107 lines
3.7 KiB
Python
Raw Normal View History

2009-02-03 18:58:25 +01:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
2010-03-11 16:42:50 +01:00
import inspect
import copy
2009-02-03 18:58:25 +01:00
from trytond.model.fields.field import Field
class Function(Field):
2009-03-01 13:37:31 +01:00
'''
2009-09-13 15:37:11 +02:00
Define function field (any).
2009-03-01 13:37:31 +01:00
'''
2009-02-03 18:58:25 +01:00
2011-01-07 10:58:43 +01:00
def __init__(self, field, getter, setter=None, searcher=None,
loading='lazy'):
2009-03-01 13:37:31 +01:00
'''
2010-03-11 16:42:50 +01:00
:param field: The field of the function.
:param getter: The name of the function for getting values.
:param setter: The name of the function to set value.
:param searcher: The name of the function to search.
2011-01-07 10:58:43 +01:00
:param loading: Define how the field must be loaded:
``lazy`` or ``eager``.
2009-03-01 13:37:31 +01:00
'''
2010-03-11 16:42:50 +01:00
assert isinstance(field, Field)
self._field = field
self._type = field._type
self.getter = getter
self.setter = setter
if not self.setter:
self._field.readonly = True
self.searcher = searcher
2012-04-01 23:08:15 +02:00
assert loading in ('lazy', 'eager'), \
'loading must be "lazy" or "eager"'
2011-01-07 10:58:43 +01:00
self.loading = loading
2009-03-01 13:37:31 +01:00
__init__.__doc__ += Field.__init__.__doc__
2009-02-03 18:58:25 +01:00
2010-03-11 16:42:50 +01:00
def __copy__(self):
return Function(copy.copy(self._field), self.getter,
setter=self.setter, searcher=self.searcher)
2012-09-24 22:16:53 +02:00
def __deepcopy__(self, memo):
return Function(copy.deepcopy(self._field, memo),
copy.deepcopy(self.getter, memo),
setter=copy.deepcopy(self.setter, memo),
searcher=copy.deepcopy(self.searcher, memo),
loading=copy.deepcopy(self.loading, memo))
2010-03-11 16:42:50 +01:00
def __getattr__(self, name):
return getattr(self._field, name)
2010-03-11 16:42:50 +01:00
def __getitem__(self, name):
return self._field[name]
2010-03-11 16:42:50 +01:00
def __setattr__(self, name, value):
2012-09-24 22:16:53 +02:00
if name in ('_field', '_type', 'getter', 'setter', 'searcher', 'name'):
object.__setattr__(self, name, value)
if name != 'name':
return
setattr(self._field, name, value)
2013-09-06 22:32:37 +02:00
def convert_domain(self, domain, tables, Model):
name, operator, value = domain[:3]
2010-03-11 16:42:50 +01:00
if not self.searcher:
2013-09-06 22:32:37 +02:00
Model.raise_user_error('search_function_missing', name)
return getattr(Model, self.searcher)(name, domain)
2009-02-03 18:58:25 +01:00
2012-09-24 22:16:53 +02:00
def get(self, ids, Model, name, values=None):
2009-03-01 13:37:31 +01:00
'''
2010-03-11 16:42:50 +01:00
Call the getter.
2009-03-01 13:37:31 +01:00
If the function has ``names`` in the function definition then
it will call it with a list of name.
'''
method = getattr(Model, self.getter)
2012-09-24 22:16:53 +02:00
def call(name):
records = Model.browse(ids)
if not hasattr(method, 'im_self') or method.im_self:
return method(records, name)
else:
return dict((r.id, method(r, name)) for r in records)
2009-02-03 18:58:25 +01:00
if isinstance(name, list):
names = name
# Test is the function works with a list of names
if 'names' in inspect.getargspec(method)[0]:
2012-09-24 22:16:53 +02:00
return call(names)
return dict((name, call(name)) for name in names)
2009-02-03 18:58:25 +01:00
else:
# Test is the function works with a list of names
if 'names' in inspect.getargspec(method)[0]:
2009-02-03 18:58:25 +01:00
name = [name]
2012-09-24 22:16:53 +02:00
return call(name)
2009-02-03 18:58:25 +01:00
2014-02-24 19:15:57 +01:00
def set(self, Model, name, ids, value, *args):
2009-03-01 13:37:31 +01:00
'''
2010-03-11 16:42:50 +01:00
Call the setter.
2009-03-01 13:37:31 +01:00
'''
2010-03-11 16:42:50 +01:00
if self.setter:
2014-02-24 19:15:57 +01:00
# TODO change setter API to use sequence of records, value
setter = getattr(Model, self.setter)
args = iter((ids, value) + args)
for ids, value in zip(args, args):
setter(Model.browse(ids), name, value)
2012-09-24 22:16:53 +02:00
def __set__(self, inst, value):
self._field.__set__(inst, value)