Cédric Krier pushed to branch branch/default at Tryton / Tryton


Commits:
ffe8c67d by Cédric Krier at 2023-01-07T20:43:22+01:00
Do not use class value of Reference field if it is not a ModelStorage

When Reference fields store partial reference, the value is the stored string
instead of an instance. So the class of the value is then str which can not be
used for domain validation.

Closes #11954
- - - - -


2 changed files:

- trytond/trytond/model/modelstorage.py
- trytond/trytond/tests/test_field_reference.py


Changes:

=====================================
trytond/trytond/model/modelstorage.py
=====================================
@@ -1207,7 +1207,9 @@
                     Relation = field.get_target()
                 elif field._type == 'reference':
                     value = getattr(record, field.name)
-                    Relation = value.__class__ if value else None
+                    Relation = (
+                        value.__class__ if isinstance(value, ModelStorage)
+                        else None)
                 else:
                     Relation = cls
                 return Relation


=====================================
trytond/trytond/tests/test_field_reference.py
=====================================
@@ -511,6 +511,28 @@
         self.assertEqual(reference.reference, None)
 
     @with_transaction()
+    def test_domain_no_id_value(self):
+        "Test reference with domain and no id"
+        pool = Pool()
+        Reference = pool.get('test.reference_domainvalidation')
+
+        reference, = Reference.create([{
+                    'reference': 'test.reference.target,',
+                    }])
+        self.assertEqual(reference.reference, 'test.reference.target,')
+
+    @with_transaction()
+    def test_domain_negative_id_value(self):
+        "Test reference with domain and negative id"
+        pool = Pool()
+        Reference = pool.get('test.reference_domainvalidation')
+
+        reference, = Reference.create([{
+                    'reference': 'test.reference.target,-1',
+                    }])
+        self.assertEqual(reference.reference, 'test.reference.target,-1')
+
+    @with_transaction()
     def test_domain_different_targets(self):
         "Test reference with domain and different targets"
         pool = Pool()
@@ -545,6 +567,28 @@
         self.assertEqual(reference.reference, target)
 
     @with_transaction()
+    def test_domain_pyson_no_id_value(self):
+        "Test reference with pyson domain and no id"
+        pool = Pool()
+        Reference = pool.get('test.reference_domainvalidation_pyson')
+
+        reference, = Reference.create([{
+                    'reference': 'test.reference.target,',
+                    }])
+        self.assertEqual(reference.reference, 'test.reference.target,')
+
+    @with_transaction()
+    def test_domain_pyson_negative_id_value(self):
+        "Test reference with pyson domain and negative id"
+        pool = Pool()
+        Reference = pool.get('test.reference_domainvalidation_pyson')
+
+        reference, = Reference.create([{
+                    'reference': 'test.reference.target,-1',
+                    }])
+        self.assertEqual(reference.reference, 'test.reference.target,-1')
+
+    @with_transaction()
     def test_domain_pyson_different_targets(self):
         "Test reference with pyson domain and different targets"
         pool = Pool()



View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/commit/ffe8c67da67c697e9e924139b2e15ef9df6dc2d0

-- 
View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/commit/ffe8c67da67c697e9e924139b2e15ef9df6dc2d0
You're receiving this email because of your account on foss.heptapod.net.


Reply via email to