From 9069a09ad6eb4d4232800469ded424fc83ec1b77 Mon Sep 17 00:00:00 2001 From: Vincent Renaville Date: Wed, 7 Jun 2023 11:19:11 +0200 Subject: [PATCH] feat: clean version for storage (#427) --- attachment_azure/models/__init__.py | 1 - attachment_azure/models/ir_binary.py | 65 ---------------------- base_attachment_object_storage/__init__.py | 26 +++++++++ 3 files changed, 26 insertions(+), 66 deletions(-) delete mode 100644 attachment_azure/models/ir_binary.py diff --git a/attachment_azure/models/__init__.py b/attachment_azure/models/__init__.py index c4b1a34..cb9b196 100644 --- a/attachment_azure/models/__init__.py +++ b/attachment_azure/models/__init__.py @@ -2,4 +2,3 @@ # Copyright 2021 Open Source Integrators # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) from . import ir_attachment -from . import ir_binary diff --git a/attachment_azure/models/ir_binary.py b/attachment_azure/models/ir_binary.py deleted file mode 100644 index f08fc87..0000000 --- a/attachment_azure/models/ir_binary.py +++ /dev/null @@ -1,65 +0,0 @@ -# Copyright 2016-2019 Camptocamp SA -# Copyright 2021 Open Source Integrators -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) -from odoo import models -from odoo.http import Stream - - -class IrBinary(models.AbstractModel): - _inherit = "ir.binary" - _description = "File streaming helper model for controllers" - - def _azure_stream(self, attachment): - # we will create or own tream and return it - stream_data = self.env["ir.attachment"]._store_file_read(attachment.store_fname) - azurestream = Stream( - type="data", - data=stream_data, - path=None, - url=None, - mimetype=attachment.mimetype or None, - download_name=attachment.name, - size=len(stream_data), - etag=attachment.checksum, - ) - return azurestream - - def _record_to_stream(self, record, field_name): - """ - Low level method responsible for the actual conversion from a - model record to a stream. This method is an extensible hook for - other modules. It is not meant to be directly called from - outside or the ir.binary model. - - :param record: the record where to load the data from. - :param str field_name: the binary field where to load the data - from. - :rtype: odoo.http.Stream - """ - if ( - record._name == "ir.attachment" - and record.store_fname - and record.store_fname.startswith("azure://") - ): - # we will create or own tream and return it - return self._azure_stream(record) - elif ( - record._name == "documents.document" - and record.attachment_id - and record.attachment_id.store_fname - and record.attachment_id.store_fname.startswith("azure://") - ): - return self._azure_stream(record.attachment_id) - - else: - return super()._record_to_stream(record, field_name) - - -# This part is used if the customer install tne enterprise module documents -try: - from odoo.addons import documents - - documents.models.ir_binary.IrBinary._record_to_stream = IrBinary._record_to_stream -except ImportError: # pylint: disable=except-pass - # document enterprise module if not installed, we just ignore - pass diff --git a/base_attachment_object_storage/__init__.py b/base_attachment_object_storage/__init__.py index 0650744..7f0956d 100644 --- a/base_attachment_object_storage/__init__.py +++ b/base_attachment_object_storage/__init__.py @@ -1 +1,27 @@ from . import models +from odoo.http import Stream + + +old_from_attachment = Stream.from_attachment + + +@classmethod +def from_attachment(cls, attachment): + if attachment.store_fname and attachment._is_file_from_a_store( + attachment.store_fname + ): + self = cls( + mimetype=attachment.mimetype, + download_name=attachment.name, + conditional=True, + etag=attachment.checksum, + ) + self.type = "data" + self.data = attachment.raw + self.last_modified = attachment["__last_update"] + self.size = len(self.data) + return self + return old_from_attachment(attachment) + + +Stream.from_attachment = from_attachment