本文整理汇总了Python中trytond.transaction.Transaction.get_cache方法的典型用法代码示例。如果您正苦于以下问题:Python Transaction.get_cache方法的具体用法?Python Transaction.get_cache怎么用?Python Transaction.get_cache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trytond.transaction.Transaction
的用法示例。
在下文中一共展示了Transaction.get_cache方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BrowseRecord
# 需要导入模块: from trytond.transaction import Transaction [as 别名]
# 或者: from trytond.transaction.Transaction import get_cache [as 别名]
class BrowseRecord(object):
'''
An object that represents record defined by a ORM object.
'''
def __init__(self, record_id, model, ids, local_cache):
assert isinstance(ids, list)
if ids:
assert record_id in ids
self._cursor = Transaction().cursor
self._user = Transaction().user
self.id = record_id
self._model = model
self._model_name = self._model._name
self._context = Transaction().context
self._pool = Pool()
self._ids = ids
cache = self._cursor.get_cache(self._context)
if model._name not in cache:
cache[model._name] = LRUDict(RECORD_CACHE_SIZE)
self._data = cache[model._name]
self._cache = cache
assert isinstance(local_cache, LRUDict)
self._local_data = local_cache
def __getitem__(self, name):
# Access to LRUDict must be atomic
result = self._local_data.get(self.id, {}).get(name)
if (self.id in self._local_data
and name in self._local_data[self.id]):
return result
result = self._data.get(self.id, {}).get(name)
if self.id in self._data and name in self._data[self.id]:
return result
# build the list of fields we will fetch
# fetch the definition of the field which was asked for
if name in self._model._columns:
col = self._model._columns[name]
elif name in self._model._inherit_fields:
col = self._model._inherit_fields[name][2]
elif hasattr(self._model, name):
return getattr(self._model, name)
else:
raise Exception('Error', 'Programming error: field "%s" ' \
'does not exist in model "%s"!' \
% (name, self._model._name))
ffields = {}
if col.loading == 'eager':
field_access_obj = self._pool.get('ir.model.field.access')
fread_accesses = {}
for inherit_name in self._model._inherits:
inherit_obj = self._pool.get(inherit_name)
fread_accesses.update(field_access_obj.check(inherit_name,
inherit_obj._columns.keys(), 'read', access=True))
fread_accesses.update(field_access_obj.check(self._model._name,
self._model._columns.keys(), 'read', access=True))
to_remove = set(x for x, y in fread_accesses.iteritems()
if not y and x != name)
threshold = BROWSE_FIELD_TRESHOLD
inherit_threshold = threshold - len(self._model._columns)
def not_cached(item):
fname, field = item
return (fname not in self._data.get(self.id, {})
and fname not in self._local_data.get(self.id, {}))
def to_load(item):
fname, field = item
return (field.loading == 'eager'
and fname not in to_remove)
def overrided(item):
fname, field = item
return fname in self._model._columns
if inherit_threshold > 0:
ifields = ((fname, field)
for fname, (_, _, field) in
self._model._inherit_fields.iteritems())
ifields = ifilterfalse(overrided,
ifilter(to_load,
ifilter(not_cached, ifields)))
ifields = islice(ifields, 0, inherit_threshold)
ffields.update(ifields)
threshold -= inherit_threshold
ifields = ifilter(to_load,
ifilter(not_cached,
self._model._columns.iteritems()))
ifields = islice(ifields, 0, threshold)
ffields.update(ifields)
ffields[name] = col
# add datetime_field
for field in ffields.values():
#.........这里部分代码省略.........