Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions fs_attachment/models/ir_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ class IrBinary(models.AbstractModel):
_inherit = "ir.binary"

def _get_fs_attachment_for_field(self, record, field_name):
if record._name == "ir.attachment" and record.fs_filename:
return record
field_def = record._fields.get(field_name)
# a field stored in its own attachment (ex: thumbnail) is not the file of
# the attachment itself
if (
record._name == "ir.attachment"
and record.fs_filename
and not (field_def and field_def.attachment)
):
return record
if field_def:
record._check_field_access(field_def, "read")
if field_def.attachment and field_def.store:
Expand Down
33 changes: 33 additions & 0 deletions fs_attachment/tests/test_fs_attachment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2023 ACSONE SA/NV (http://acsone.eu).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import copy
import os
from pathlib import Path
from unittest import mock
Expand Down Expand Up @@ -568,3 +569,35 @@ def test_store_in_db_instead_of_object_storage_domain(self):
]
),
)

def test_get_fs_attachment_for_field_stored_in_attachment(self):
"""A field stored in its own attachment (ex: the thumbnail added by mail)
must not be replaced by the file of the parent attachment"""
parent = (
self.env["ir.attachment"]
.with_context(storage_location=self.temp_backend.code)
.create({"name": "doc.pdf", "raw": b"parent"})
)
thumbnail = (
self.env["ir.attachment"]
.with_context(storage_location=self.temp_backend.code)
.create(
{
"name": "thumbnail",
"raw": b"thumbnail",
"res_model": "ir.attachment",
"res_id": parent.id,
"res_field": "thumbnail",
}
)
)
field = copy.copy(parent._fields["raw"])
field.attachment = True
field.store = True
ir_binary = self.env["ir.binary"]
fields_with_thumbnail = {**parent._fields, "thumbnail": field}
with mock.patch.object(type(parent), "_fields", fields_with_thumbnail):
self.assertEqual(
ir_binary._get_fs_attachment_for_field(parent, "thumbnail"), thumbnail
)
self.assertEqual(ir_binary._get_fs_attachment_for_field(parent, "raw"), parent)
Loading