changeset fd082be2daf4 in trytond:5.8
details: https://hg.tryton.org/trytond?cmd=changeset&node=fd082be2daf4
description:
        Evaluate invalid domain if there are many fields

        We must find the field that is invalid instead of using the first one 
with a
        domain constraint.

        issue10418
        review360371002
        (grafted from b64005c6aaa91dbb07b06b1269f35be937747cf0)
diffstat:

 trytond/model/modelstorage.py      |   9 ++++++---
 trytond/tests/modelstorage.py      |  20 ++++++++++++++++++++
 trytond/tests/test_modelstorage.py |  35 +++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 3 deletions(-)

diffs (111 lines):

diff -r 406b95a3ae8d -r fd082be2daf4 trytond/model/modelstorage.py
--- a/trytond/model/modelstorage.py     Wed Oct 20 15:16:10 2021 +0200
+++ b/trytond/model/modelstorage.py     Sat Oct 30 02:20:23 2021 +0200
@@ -16,7 +16,7 @@
 from trytond.model import fields
 from trytond.tools import reduce_domain, is_instance_method, grouped_slice
 from trytond.tools.domain_inversion import (
-    domain_inversion, parse as domain_parse)
+    domain_inversion, eval_domain, parse as domain_parse)
 from trytond.pyson import PYSONEncoder, PYSONDecoder, PYSON
 from trytond.const import OPERATORS
 from trytond.config import config
@@ -1174,11 +1174,14 @@
                         else:
                             fields.add(field.name)
                         for field_name in sorted(fields):
+                            env = EvalEnvironment(invalid_record, Relation)
                             invalid_domain = domain_inversion(
-                                domain, field_name,
-                                EvalEnvironment(invalid_record, Relation))
+                                domain, field_name, env)
                             if isinstance(invalid_domain, bool):
                                 continue
+                            if (len(fields) > 1  # no need to evaluate
+                                    and eval_domain(invalid_domain, env)):
+                                continue
                             field_def = Relation.fields_get(
                                 [field_name], level=level)
                             raise DomainValidationError(
diff -r 406b95a3ae8d -r fd082be2daf4 trytond/tests/modelstorage.py
--- a/trytond/tests/modelstorage.py     Wed Oct 20 15:16:10 2021 +0200
+++ b/trytond/tests/modelstorage.py     Sat Oct 30 02:20:23 2021 +0200
@@ -86,6 +86,24 @@
     value = fields.Char("Value")
 
 
+class ModelStorageRelationMultiDomain(ModelSQL):
+    "Model stored containing a relation fields with multi domain"
+    __name__ = 'test.modelstorage.relation_multi_domain'
+    relation = fields.Many2One(
+        'test.modelstorage.relation_multi_domain.target', "Value",
+        domain=[
+            ('test1', '=', True),
+            ('test2', '=', True),
+            ])
+
+
+class ModelStorageRelationMultiDomainTarget(ModelSQL):
+    "Target of Model stored containing a relation field with multi domain"
+    __name__ = 'test.modelstorage.relation_multi_domain.target'
+    test1 = fields.Boolean("Test 1")
+    test2 = fields.Boolean("Test 2")
+
+
 class ModelStorageRelationDomain2(ModelSQL):
     "Model stored containing a relation field with a domain with 2 level"
     __name__ = 'test.modelstorage.relation_domain2'
@@ -132,6 +150,8 @@
         ModelStoragePYSONDomain,
         ModelStorageRelationDomain,
         ModelStorageRelationDomainTarget,
+        ModelStorageRelationMultiDomain,
+        ModelStorageRelationMultiDomainTarget,
         ModelStorageRelationDomain2,
         ModelStorageRelationDomain2Target,
         ModelStorageEvalEnvironment,
diff -r 406b95a3ae8d -r fd082be2daf4 trytond/tests/test_modelstorage.py
--- a/trytond/tests/test_modelstorage.py        Wed Oct 20 15:16:10 2021 +0200
+++ b/trytond/tests/test_modelstorage.py        Sat Oct 30 02:20:23 2021 +0200
@@ -367,6 +367,41 @@
         self.assertTrue(cm.exception.domain[1]['value'])
 
     @with_transaction()
+    def test_relation_domain_multi(self):
+        "Test relation multi domain"
+        pool = Pool()
+        Model = pool.get('test.modelstorage.relation_multi_domain')
+        Target = pool.get('test.modelstorage.relation_multi_domain.target')
+
+        target, = Target.create([{'test1': True, 'test2': True}])
+
+        record, = Model.create([{'relation': target.id}])
+
+    @with_transaction()
+    def test_relation_domain_multi_invalid(self):
+        "Test invalid relation multi domain"
+        pool = Pool()
+        Model = pool.get('test.modelstorage.relation_multi_domain')
+        Target = pool.get('test.modelstorage.relation_multi_domain.target')
+
+        target1, target2 = Target.create(
+            [{'test1': False, 'test2': True}, {'test1': True, 'test2': False}])
+
+        with self.assertRaises(DomainValidationError) as cm:
+            Model.create([{
+                        'relation': target1.id,
+                        }])
+        self.assertEqual(cm.exception.domain[0], [('test1', '=', True)])
+        self.assertEqual(cm.exception.domain[1].keys(), {'test1'})
+
+        with self.assertRaises(DomainValidationError) as cm:
+            Model.create([{
+                        'relation': target2.id,
+                        }])
+        self.assertEqual(cm.exception.domain[0], [('test2', '=', True)])
+        self.assertEqual(cm.exception.domain[1].keys(), {'test2'})
+
+    @with_transaction()
     def test_relation_pyson_domain(self):
         "Test valid relation with PYSON"
         pool = Pool()

Reply via email to