changeset 82b1408ceaa2 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=82b1408ceaa2
description:
Fill local cache of Function field instances with values
issue10936
review377511003
diffstat:
CHANGELOG | 1 +
doc/ref/models/models.rst | 4 ++++
trytond/model/fields/function.py | 11 ++++++++++-
trytond/model/modelsql.py | 16 +++++++++-------
trytond/model/modelstorage.py | 5 +++++
trytond/tests/field_function.py | 24 ++++++++++++++++++++++++
trytond/tests/test_field_function.py | 15 +++++++++++++++
7 files changed, 68 insertions(+), 8 deletions(-)
diffs (165 lines):
diff -r 3193180548e8 -r 82b1408ceaa2 CHANGELOG
--- a/CHANGELOG Sun Nov 28 13:27:52 2021 +0100
+++ b/CHANGELOG Sun Nov 28 13:40:30 2021 +0100
@@ -1,3 +1,4 @@
+* Fill local cache of Function field instances with values
* Cache Function field getter without context in readonly transactions
* Use expire delay instead of UTC datetime in reset password email
* Use RENAME and DROP COLUMN with SQLite backend
diff -r 3193180548e8 -r 82b1408ceaa2 doc/ref/models/models.rst
--- a/doc/ref/models/models.rst Sun Nov 28 13:27:52 2021 +0100
+++ b/doc/ref/models/models.rst Sun Nov 28 13:40:30 2021 +0100
@@ -264,6 +264,10 @@
``.``. The number of *dots* in the name is not limited.
The order of the returned list is not guaranteed.
+.. classmethod:: ModelStorage.index_get_field(name)
+
+ Return the index sort order of the field get calls.
+
.. classmethod:: ModelStorage.write(records, values, [[records, values], ...])
Write ``values`` on the list of records. ``values`` is a dictionary with
diff -r 3193180548e8 -r 82b1408ceaa2 trytond/model/fields/function.py
--- a/trytond/model/fields/function.py Sun Nov 28 13:27:52 2021 +0100
+++ b/trytond/model/fields/function.py Sun Nov 28 13:40:30 2021 +0100
@@ -115,8 +115,17 @@
instance_method = is_instance_method(Model, self.getter)
multiple = self.getter_multiple(method)
+ records = Model.browse(ids)
+ for record, value in zip(records, values):
+ assert record.id == value['id']
+ for fname, val in value.items():
+ field = Model._fields.get(fname)
+ if field and field._type not in {
+ 'many2one', 'reference',
+ 'one2many', 'many2many', 'one2one'}:
+ record._local_cache[record.id][fname] = val
+
def call(name):
- records = Model.browse(ids)
if not instance_method:
return method(records, name)
else:
diff -r 3193180548e8 -r 82b1408ceaa2 trytond/model/modelsql.py
--- a/trytond/model/modelsql.py Sun Nov 28 13:27:52 2021 +0100
+++ b/trytond/model/modelsql.py Sun Nov 28 13:40:30 2021 +0100
@@ -834,6 +834,7 @@
# all fields for which there is a get attribute
getter_fields = [f for f in all_fields
if f in cls._fields and hasattr(cls._fields[f], 'get')]
+ getter_fields = sorted(getter_fields, key=cls.index_get_field)
cache = transaction.get_cache()[cls.__name__]
if getter_fields and cachable_fields:
@@ -882,24 +883,25 @@
result, record_cache_size(transaction)):
sub_results = list(sub_results)
sub_ids = []
+ sub_values = []
for row in sub_results:
if (row['id'] not in cache
or any(f not in cache[row['id']]
for f in field_list)):
sub_ids.append(row['id'])
+ sub_values.append(row)
else:
for fname in field_list:
row[fname] = cache[row['id']][fname]
getter_results = field.get(
- sub_ids, cls, field_list, values=sub_results)
+ sub_ids, cls, field_list, values=sub_values)
for fname in field_list:
getter_result = getter_results[fname]
- for row in sub_results:
- if row['id'] in sub_ids:
- row[fname] = getter_result[row['id']]
- if (transaction.readonly
- and not getter_with_context):
- cache[row['id']][fname] = row[fname]
+ for row in sub_values:
+ row[fname] = getter_result[row['id']]
+ if (transaction.readonly
+ and not getter_with_context):
+ cache[row['id']][fname] = row[fname]
def read_related(field, Target, rows, fields):
name = field.name
diff -r 3193180548e8 -r 82b1408ceaa2 trytond/model/modelstorage.py
--- a/trytond/model/modelstorage.py Sun Nov 28 13:27:52 2021 +0100
+++ b/trytond/model/modelstorage.py Sun Nov 28 13:40:30 2021 +0100
@@ -217,6 +217,11 @@
return []
@classmethod
+ def index_get_field(cls, name):
+ "Returns the index sort order of the field get calls."
+ return 0
+
+ @classmethod
def write(cls, records, values, *args):
'''
Write values on records.
diff -r 3193180548e8 -r 82b1408ceaa2 trytond/tests/field_function.py
--- a/trytond/tests/field_function.py Sun Nov 28 13:27:52 2021 +0100
+++ b/trytond/tests/field_function.py Sun Nov 28 13:40:30 2021 +0100
@@ -43,9 +43,33 @@
context.get('language', 'empty'), context.get('test', 'empty'))
+class FunctionGetterLocalCache(ModelSQL):
+ "Function Getter with local cache"
+ __name__ = 'test.function.getter_local_cache'
+
+ function1 = fields.Function(
+ fields.Char("Char 1"), 'get_function1')
+ function2 = fields.Function(
+ fields.Char("Char 2"), 'get_function2')
+
+ def get_function1(self, name):
+ return "test"
+
+ def get_function2(self, name):
+ return self.function1.upper()
+
+ @classmethod
+ def index_get_field(cls, name):
+ index = super().index_get_field(name)
+ if name == 'function2':
+ index = cls.index_get_field('function1') + 1
+ return index
+
+
def register(module):
Pool.register(
FunctionAccessor,
FunctionAccessorTarget,
FunctionGetterContext,
+ FunctionGetterLocalCache,
module=module, type_='model')
diff -r 3193180548e8 -r 82b1408ceaa2 trytond/tests/test_field_function.py
--- a/trytond/tests/test_field_function.py Sun Nov 28 13:27:52 2021 +0100
+++ b/trytond/tests/test_field_function.py Sun Nov 28 13:40:30 2021 +0100
@@ -73,6 +73,21 @@
record.function_without_context
self.assertEqual(getter.call_count, 2)
+ @with_transaction()
+ def test_getter_local_cache(self):
+ "Test getter use local cache"
+ pool = Pool()
+ Model = pool.get('test.function.getter_local_cache')
+
+ record = Model()
+ record.save()
+ with patch.object(Model, 'get_function1') as getter:
+ getter.return_value = 'test'
+
+ Model.read([record.id], ['function1', 'function2'])
+
+ self.assertEqual(getter.call_count, 1)
+
def suite():
return unittest.TestLoader().loadTestsFromTestCase(FieldFunctionTestCase)