From 455d8c362bcc4077cfc73c73d8ec28f071ca7c31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 7 Jul 2023 08:44:29 +0200 Subject: [PATCH] Do not copy binary field with file_id only if copied data is not changed --- trytond/trytond/model/modelstorage.py | 4 +- trytond/trytond/tests/test_field_binary.py | 48 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/trytond/trytond/model/modelstorage.py b/trytond/trytond/model/modelstorage.py index f93b3d32c9..6fa12bfb44 100644 --- a/trytond/trytond/model/modelstorage.py +++ b/trytond/trytond/model/modelstorage.py @@ -431,7 +431,9 @@ class ModelStorage(Model): data[field_name] = [('add', data[field_name])] elif ftype == 'binary': # Copy only file_id - if field.file_id and origin.get(field.file_id): + if (field.file_id + and origin.get(field.file_id) + and data[field_name] == origin.get(field_name)): del data[field_name] if 'id' in data: del data['id'] diff --git a/trytond/trytond/tests/test_field_binary.py b/trytond/trytond/tests/test_field_binary.py index 6a71e1da2a..8a5c6b899b 100644 --- a/trytond/trytond/tests/test_field_binary.py +++ b/trytond/trytond/tests/test_field_binary.py @@ -202,3 +202,51 @@ class FieldBinaryTestCase(unittest.TestCase): self.assertEqual(binary.binary, None) self.assertEqual(binary.binary_id, None) + + @with_transaction() + def test_copy(self): + "Test copy binary" + pool = Pool() + Binary = pool.get('test.binary') + binary = Binary(binary=b'foo') + binary.save() + + copy, = Binary.copy([binary]) + + self.assertEqual(binary.binary, copy.binary) + + @with_transaction() + def test_copy_with_default(self): + "Test copy binary with default" + pool = Pool() + Binary = pool.get('test.binary') + binary = Binary(binary=b'foo') + binary.save() + + copy, = Binary.copy([binary], default={'binary': b'bar'}) + + self.assertEqual(copy.binary, b'bar') + + @with_transaction() + def test_copy_with_filestorage(self): + "Test copy binary with filestorage" + pool = Pool() + Binary = pool.get('test.binary_filestorage') + binary = Binary(binary=b'foo') + binary.save() + + copy, = Binary.copy([binary]) + + self.assertEqual(binary.binary, copy.binary) + + @with_transaction() + def test_copy_with_filestorage_default(self): + "Test copy binary with filestorage and default" + pool = Pool() + Binary = pool.get('test.binary_filestorage') + binary = Binary(binary=b'foo') + binary.save() + + copy, = Binary.copy([binary], default={'binary': b'bar'}) + + self.assertEqual(copy.binary, b'bar')