Reviewers: ,
Please review this at http://codereview.tryton.org/222005/
Affected files:
M tryton/gui/window/view_form/model/record.py
Index: tryton/gui/window/view_form/model/record.py
===================================================================
--- a/tryton/gui/window/view_form/model/record.py
+++ b/tryton/gui/window/view_form/model/record.py
@@ -1,5 +1,7 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
+from weakref import WeakSet
+from collections import defaultdict
import tryton.rpc as rpc
from tryton.signal_event import SignalEvent
import tryton.common as common
@@ -11,6 +13,8 @@
from functools import reduce
from tryton.exceptions import TrytonServerError
+POOL = defaultdict(WeakSet)
+
class Record(SignalEvent):
@@ -37,10 +41,13 @@
self.value = {}
self.autocompletion = {}
self.exception = False
+ POOL[model_name].add(self)
def __getitem__(self, name, raise_exception=False):
if name not in self._loaded and self.id >= 0:
- ids = [self.id]
+ id2record = {
+ self.id: self,
+ }
if name == '*':
loading = reduce(
lambda x, y: 'eager' if x == y == 'eager'
else 'lazy',
@@ -53,17 +60,24 @@
idx = self.group.index(self)
length = len(self.group)
n = 1
- while len(ids) < 80 and (idx - n >= 0 or \
+ while len(id2record) < 80 and (idx - n >= 0 or \
idx + n < length) and n < 100:
if idx - n >= 0:
record = self.group[idx - n]
if name not in record._loaded and record.id >= 0:
- ids.append(record.id)
+ id2record[record.id] = record
if idx + n < length:
record = self.group[idx + n]
if name not in record._loaded and record.id >= 0:
- ids.append(record.id)
+ id2record[record.id] = record
n += 1
+ if len(id2record) < 80:
+ for record in POOL[self.model_name]:
+ if (name not in record._loaded and record.id >= 0
+ and record.id not in id2record):
+ id2record[record.id] = record
+ if len(id2record) == 80:
+ break
if loading == 'eager':
fields = [fname for fname, field in
self.group.fields.iteritems()
if field.attrs.get('loading', 'eager') == 'eager']
@@ -79,7 +93,7 @@
ctx.update(dict(('%s.%s' % (self.model_name, fname), 'size')
for fname, field in self.group.fields.iteritems()
if field.attrs['type'] == 'binary'))
- args = ('model', self.model_name, 'read', ids, fields, ctx)
+ args = ('model', self.model_name, 'read', id2record.keys(),
fields, ctx)
exception = None
try:
values = rpc.execute(*args)
@@ -88,15 +102,15 @@
raise
values = common.process_exception(exception, *args)
if not values:
- values = [{'id': x} for x in ids]
+ values = [{'id': x} for x in id2record]
default_values = dict((f, False) for f in fields)
for value in values:
value.update(default_values)
self.exception = True
id2value = dict((value['id'], value) for value in values)
- if ids != [self.id]:
- for id in ids:
- record = self.group.get(id)
+ if len(id2record) > 1:
+ for id, record in id2record.iteritems():
+ record = id2record[id]
if not record.exception:
record.exception = bool(exception)
value = id2value.get(id)
--
[email protected] mailing list