Skip to content

Commit b650e19

Browse files
committed
[FIX] base: better copy of attachment
It was not possible to set a different value via the field datas Add tests closes odoo#83165 X-original-commit: b1f0474 Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
1 parent 974ae7c commit b650e19

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

odoo/addons/base/models/ir_attachment.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,9 @@ def write(self, vals):
578578

579579
def copy(self, default=None):
580580
self.check('write')
581-
default = dict(default or {}, datas=self.datas)
581+
if not (default or {}).keys() & {'datas', 'db_datas', 'raw'}:
582+
# ensure the content is kept and recomputes checksum/store_fname
583+
default = dict(default or {}, raw=self.raw)
582584
return super(IrAttachment, self).copy(default)
583585

584586
def unlink(self):

odoo/addons/base/tests/test_ir_attachment.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ def setUp(self):
2424
# Blob1
2525
self.blob1 = b'blob1'
2626
self.blob1_b64 = base64.b64encode(self.blob1)
27-
blob1_hash = hashlib.sha1(self.blob1).hexdigest()
28-
self.blob1_fname = blob1_hash[:HASH_SPLIT] + '/' + blob1_hash
27+
self.blob1_hash = hashlib.sha1(self.blob1).hexdigest()
28+
self.blob1_fname = self.blob1_hash[:HASH_SPLIT] + '/' + self.blob1_hash
2929

3030
# Blob2
3131
self.blob2 = b'blob2'
32+
self.blob2_b64 = base64.b64encode(self.blob2)
3233

3334
def assertApproximately(self, value, expectedSize, delta=1):
3435
# we don't used bin_size in context, because on write, the cached value is the data and not
@@ -239,3 +240,23 @@ def test_10_image_autoresize(self):
239240
self.env['ir.config_parameter'].set_param('base.image_autoresize_max_px', '0')
240241
attach.raw = img_bin
241242
self.assertApproximately(attach.raw, fullsize)
243+
244+
def test_11_copy(self):
245+
"""
246+
Copying an attachment preserves the data
247+
"""
248+
document = self.Attachment.create({'name': 'document', 'datas': self.blob2_b64})
249+
document2 = document.copy({'name': "document (copy)"})
250+
self.assertEqual(document2.name, "document (copy)")
251+
self.assertEqual(document2.datas, document.datas)
252+
self.assertEqual(document2.db_datas, document.db_datas)
253+
self.assertEqual(document2.store_fname, document.store_fname)
254+
self.assertEqual(document2.checksum, document.checksum)
255+
256+
document3 = document.copy({'datas': self.blob1_b64})
257+
self.assertEqual(document3.datas, self.blob1_b64)
258+
self.assertEqual(document3.raw, self.blob1)
259+
self.assertTrue(self.filestore) # no data in db but has a store_fname
260+
self.assertEqual(document3.db_datas, False)
261+
self.assertEqual(document3.store_fname, self.blob1_fname)
262+
self.assertEqual(document3.checksum, self.blob1_hash)

0 commit comments

Comments
 (0)