changeset 7007a9104464 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=7007a9104464
description:
        Add context field of parent to on_change calls

        Those fields are required to instantiate the parent fields with the 
proper
        context.

        issue10182
        review359301002
diffstat:

 trytond/model/fields/field.py       |  17 ++++++++++++++---
 trytond/tests/test_field_depends.py |  28 ++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)

diffs (69 lines):

diff -r 5b4d92ad2db9 -r 7007a9104464 trytond/model/fields/field.py
--- a/trytond/model/fields/field.py     Thu Mar 11 19:19:17 2021 +0100
+++ b/trytond/model/fields/field.py     Thu Mar 11 19:20:32 2021 +0100
@@ -462,13 +462,24 @@
                     changes.append('id')
 
                 for name in changes:
-                    field = model._fields.get(name)
+                    target = model
+                    if '.' in name:
+                        prefix, _ = name.rsplit('.', 1)
+                        prefix += '.'
+                    else:
+                        prefix = ''
+                    while name.startswith('_parent_'):
+                        field, name = name.split('.', 1)
+                        target = target._fields[field[8:]].get_target()
+                    field = target._fields[name]
                     if field and field.context:
                         eval_fields = get_eval_fields(field.context)
                         for context_field_name in eval_fields:
+                            prefix_ctx_field_name = (
+                                prefix + context_field_name)
                             if (context_field_name in field.depends
-                                    and context_field_name not in changes):
-                                changes.append(context_field_name)
+                                    and prefix_ctx_field_name not in changes):
+                                changes.append(prefix_ctx_field_name)
 
         name = '%s,%s' % (model.__name__, self.name)
         for attr, ttype in [('string', 'field'), ('help', 'help')]:
diff -r 5b4d92ad2db9 -r 7007a9104464 trytond/tests/test_field_depends.py
--- a/trytond/tests/test_field_depends.py       Thu Mar 11 19:19:17 2021 +0100
+++ b/trytond/tests/test_field_depends.py       Thu Mar 11 19:20:32 2021 +0100
@@ -191,6 +191,34 @@
             set(Model.name.definition(Model, 'en')['on_change']),
             {'foo', 'bar', 'id'})
 
+    @with_transaction()
+    def test_field_context_parent(self):
+        "Tests depends on parent field with context"
+
+        class Target(ModelView):
+            name = fields.Char(
+                "Name", context={'bar': Eval('bar')}, depends=['bar'])
+            bar = fields.Char("Bar")
+
+        class Model(ModelView):
+            name = fields.Char("Name")
+            foo = fields.Many2One(None, "Foo")
+
+            @fields.depends('_parent_foo.name')
+            def on_change_name(self):
+                return
+
+        Model.foo.get_target = lambda: Target
+
+        Target.__setup__()
+        Target.__post_setup__()
+        Model.__setup__()
+        Model.__post_setup__()
+
+        self.assertEqual(
+            set(Model.name.definition(Model, 'en')['on_change']),
+            {'_parent_foo.name', '_parent_foo.bar', 'id'})
+
     def test_property_depends(self):
         "Tests depends on a property"
 

Reply via email to