changeset 718739cf087d in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=718739cf087d
description:
        Allow copying Python instances of Model

        issue9869
        review337071002
diffstat:

 CHANGELOG                     |   1 +
 trytond/model/model.py        |   8 ++++++++
 trytond/model/modelstorage.py |   2 +-
 trytond/tests/test_model.py   |  28 ++++++++++++++++++++++++++++
 4 files changed, 38 insertions(+), 1 deletions(-)

diffs (83 lines):

diff -r 3f0824918382 -r 718739cf087d CHANGELOG
--- a/CHANGELOG Sat Dec 05 11:49:50 2020 +0100
+++ b/CHANGELOG Sat Dec 05 11:58:41 2020 +0100
@@ -1,3 +1,4 @@
+* Allow copying Python instances of Model
 * Add all buttons to default form view
 * Unify PYSON string format
 * Forbid all white spaces except space in Char field
diff -r 3f0824918382 -r 718739cf087d trytond/model/model.py
--- a/trytond/model/model.py    Sat Dec 05 11:49:50 2020 +0100
+++ b/trytond/model/model.py    Sat Dec 05 11:58:41 2020 +0100
@@ -277,7 +277,15 @@
             self._values = None
             self._init_values = None
 
+    def __copy__(self):
+        copied = self.__class__(self.id)
+        copied._values = copy.copy(self._values)
+        copied._init_values = copy.copy(self._init_values)
+        return copied
+
     def __getattr__(self, name):
+        if name.startswith('__') and name.endswith('__'):
+            raise AttributeError
         try:
             return self._values[name]
         except (KeyError, TypeError):
diff -r 3f0824918382 -r 718739cf087d trytond/model/modelstorage.py
--- a/trytond/model/modelstorage.py     Sat Dec 05 11:49:50 2020 +0100
+++ b/trytond/model/modelstorage.py     Sat Dec 05 11:58:41 2020 +0100
@@ -1439,7 +1439,7 @@
         try:
             return super(ModelStorage, self).__getattr__(name)
         except AttributeError:
-            if self.id is None or self.id < 0 or name.startswith('_'):
+            if name.startswith('_') or self.id is None or self.id < 0:
                 raise
 
         self._local_cache.refresh()
diff -r 3f0824918382 -r 718739cf087d trytond/tests/test_model.py
--- a/trytond/tests/test_model.py       Sat Dec 05 11:49:50 2020 +0100
+++ b/trytond/tests/test_model.py       Sat Dec 05 11:58:41 2020 +0100
@@ -1,5 +1,6 @@
 # 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 copy import copy
 
 import unittest
 
@@ -188,6 +189,33 @@
 
         self.assertTrue(definition['name']['readonly'])
 
+    @with_transaction()
+    def test_copy(self):
+        "Test copy an instance"
+        pool = Pool()
+        Model = pool.get('test.model')
+
+        record = Model(name='foo')
+
+        record_copied = copy(record)
+
+        self.assertEqual(record.name, record_copied.name)
+        self.assertNotEqual(id(record), id(record_copied))
+
+    @with_transaction()
+    def test_copy_values(self):
+        "Test copied instance has different values"
+        pool = Pool()
+        Model = pool.get('test.model')
+
+        record = Model(name='foo')
+
+        record_copied = copy(record)
+        record_copied.name = 'bar'
+
+        self.assertEqual(record.name, 'foo')
+        self.assertEqual(record_copied.name, 'bar')
+
 
 class ModelTranslationTestCase(unittest.TestCase):
     "Test Model translation"

Reply via email to