From 72a71019c0b7aec8246569fca729918334502fa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 28 Mar 2020 16:57:05 +0100 Subject: [PATCH] Do not copy Binary content with file_id This avoid the filestore to reprocess the binary data as the file_id is already copied. issue9153 review297241002 --- CHANGELOG | 1 + trytond/model/modelstorage.py | 4 ++++ trytond/tests/copy_.py | 9 +++++++++ trytond/tests/test_copy.py | 26 ++++++++++++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 0d5743e489..ba3d2c765f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Do not copy Binary content with file_id * Replace memoize with functools.lru_cache * Add support for Python 3.8 * Set all fields readonly for inactive record diff --git a/trytond/model/modelstorage.py b/trytond/model/modelstorage.py index 436feba248..faa8a7300a 100644 --- a/trytond/model/modelstorage.py +++ b/trytond/model/modelstorage.py @@ -383,6 +383,10 @@ class ModelStorage(Model): del data[field_name] elif data[field_name]: data[field_name] = [('add', data[field_name])] + elif ftype == 'binary': + # Copy only file_id + if field.file_id and origin.get(field.file_id): + del data[field_name] if 'id' in data: del data['id'] return data diff --git a/trytond/tests/copy_.py b/trytond/tests/copy_.py index 78f3dc0433..e902f0da10 100644 --- a/trytond/tests/copy_.py +++ b/trytond/tests/copy_.py @@ -93,6 +93,14 @@ class CopyMany2ManyReferenceRelation(ModelSQL): 'Many2ManyReference Target') +class CopyBinary(ModelSQL): + "Copy Binary" + __name__ = 'test.copy.binary' + binary = fields.Binary("Binary") + binary_id = fields.Binary("Binary with ID", file_id='file_id') + file_id = fields.Char("Binary ID") + + def register(module): Pool.register( Copy, @@ -106,4 +114,5 @@ def register(module): CopyMany2ManyReference, CopyMany2ManyReferenceTarget, CopyMany2ManyReferenceRelation, + CopyBinary, module=module, type_='model') diff --git a/trytond/tests/test_copy.py b/trytond/tests/test_copy.py index c3f34da95d..7f63931a1c 100644 --- a/trytond/tests/test_copy.py +++ b/trytond/tests/test_copy.py @@ -3,6 +3,7 @@ # this repository contains the full copyright notices and license terms. import unittest +from trytond.model import fields from trytond.tests.test_tryton import activate_module, with_transaction from trytond.pool import Pool @@ -169,6 +170,31 @@ class CopyTestCase(unittest.TestCase): self.assertSequenceEqual(record_copy.many2many, [target]) + @with_transaction() + def test_binary(self): + "Test copy binary" + pool = Pool() + Binary = pool.get('test.copy.binary') + record = Binary(binary=fields.Binary.cast(b'data')) + record.save() + + record_copy, = Binary.copy([record]) + + self.assertEqual(record_copy.binary, record.binary) + + @with_transaction() + def test_binary_file_id(self): + "Test copy binary with file_id" + pool = Pool() + Binary = pool.get('test.copy.binary') + record = Binary(binary_id=fields.Binary.cast(b'data')) + record.save() + + record_copy, = Binary.copy([record]) + + self.assertEqual(record_copy.file_id, record.file_id) + self.assertEqual(record_copy.binary_id, record.binary_id) + def suite(): return unittest.TestLoader().loadTestsFromTestCase(CopyTestCase)