From a2b7c162a39262d117647d8cb49a6838d2c5e2df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Tue, 1 Oct 2024 16:30:42 +0200 Subject: [PATCH] [MIG] dms_attachment_link: Finish migration --- dms_attachment_link/models/__init__.py | 1 + dms_attachment_link/models/ir_attachment.py | 2 +- dms_attachment_link/models/ir_binary.py | 15 +++++++++++++++ .../tests/test_dms_attachment_link.py | 13 +++++++------ 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 dms_attachment_link/models/ir_binary.py diff --git a/dms_attachment_link/models/__init__.py b/dms_attachment_link/models/__init__.py index 1cce9f551..c0811a10f 100644 --- a/dms_attachment_link/models/__init__.py +++ b/dms_attachment_link/models/__init__.py @@ -1,2 +1,3 @@ from . import dms_file from . import ir_attachment +from . import ir_binary diff --git a/dms_attachment_link/models/ir_attachment.py b/dms_attachment_link/models/ir_attachment.py index 54967ac1c..d38fa20d5 100644 --- a/dms_attachment_link/models/ir_attachment.py +++ b/dms_attachment_link/models/ir_attachment.py @@ -9,7 +9,7 @@ class IrAttachment(models.Model): dms_file_id = fields.Many2one(comodel_name="dms.file") - @api.depends("dms_file_id.content") + @api.depends("dms_file_id", "dms_file_id.content") def _compute_datas(self): """Get the contents of the attachment directly from the DMS file.""" _self = self.filtered("dms_file_id") diff --git a/dms_attachment_link/models/ir_binary.py b/dms_attachment_link/models/ir_binary.py new file mode 100644 index 000000000..1ee687ead --- /dev/null +++ b/dms_attachment_link/models/ir_binary.py @@ -0,0 +1,15 @@ +# Copyright 2024 Tecnativa - Víctor Martínez +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import models + + +class IrBinary(models.AbstractModel): + _inherit = "ir.binary" + + def _record_to_stream(self, record, field_name): + """We need to overwrite for the download and preview to be correct.""" + if record._name == "ir.attachment" and record.dms_file_id: + record = record.dms_file_id + field_name = "content" + return super()._record_to_stream(record=record, field_name=field_name) diff --git a/dms_attachment_link/tests/test_dms_attachment_link.py b/dms_attachment_link/tests/test_dms_attachment_link.py index 684a3849f..84935eae7 100644 --- a/dms_attachment_link/tests/test_dms_attachment_link.py +++ b/dms_attachment_link/tests/test_dms_attachment_link.py @@ -1,14 +1,15 @@ # Copyright 2023 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo.tests import common +from odoo.addons.base.tests.common import BaseCommon -class TestDmsAttachmentLink(common.TransactionCase): - def setUp(self): - super().setUp() - self.partner = self.env["res.partner"].create({"name": "Test partner"}) - self.dms_file = self.env.ref("dms.file_01_demo") +class TestDmsAttachmentLink(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner = cls.env["res.partner"].create({"name": "Test partner"}) + cls.dms_file = cls.env.ref("dms.file_01_demo") def test_add_url_attachment(self): attachment = self.dms_file.with_context(