From daaaddad3c049a51ccdaedb9475eee1364878f28 Mon Sep 17 00:00:00 2001 From: filoquin Date: Tue, 20 Dec 2022 14:24:54 -0300 Subject: [PATCH 1/2] [MIG] attachment_s3: Migration to 16.0 --- attachment_s3/__manifest__.py | 4 +- attachment_s3/models/__init__.py | 1 + attachment_s3/models/ir_binary.py | 65 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 attachment_s3/models/ir_binary.py diff --git a/attachment_s3/__manifest__.py b/attachment_s3/__manifest__.py index 7958e620..be40ebee 100644 --- a/attachment_s3/__manifest__.py +++ b/attachment_s3/__manifest__.py @@ -5,7 +5,7 @@ { "name": "Attachments on S3 storage", "summary": "Store assets and attachments on a S3 compatible object storage", - "version": "15.0.1.0.0", + "version": "16.0.1.0.0", "author": "Camptocamp,Odoo Community Association (OCA)", "license": "AGPL-3", "category": "Knowledge Management", @@ -15,5 +15,5 @@ }, "website": "https://github.com/camptocamp/odoo-cloud-platform", "data": [], - "installable": False, + "installable": True, } diff --git a/attachment_s3/models/__init__.py b/attachment_s3/models/__init__.py index aaf38a16..f7a98ece 100644 --- a/attachment_s3/models/__init__.py +++ b/attachment_s3/models/__init__.py @@ -1 +1,2 @@ from . import ir_attachment +from . import ir_binary diff --git a/attachment_s3/models/ir_binary.py b/attachment_s3/models/ir_binary.py new file mode 100644 index 00000000..d34bbf5f --- /dev/null +++ b/attachment_s3/models/ir_binary.py @@ -0,0 +1,65 @@ +# 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 _s3_stream(self, attachment): + # we will create or own tream and return it + stream_data = self.env["ir.attachment"]._store_file_read(attachment.store_fname) + s3stream = 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 s3stream + + 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("s3://") + ): + # we will create or own stream and return it + return self._s3_stream(record) + elif ( + record._name == "documents.document" + and record.attachment_id + and record.attachment_id.store_fname + and record.attachment_id.store_fname.startswith("s3://") + ): + return self._s3_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: + # document enterprise module if not installed, we just ignore + pass From 4a2c9195ff6c6e2002826884177fc4f4a1beafe1 Mon Sep 17 00:00:00 2001 From: Juan Jose Scarafia Date: Sat, 24 Dec 2022 15:33:23 -0300 Subject: [PATCH 2/2] [REF] deal with stream on base_attachment_object_storage --- attachment_s3/models/__init__.py | 1 - attachment_s3/models/ir_binary.py | 65 ------------------------------- 2 files changed, 66 deletions(-) delete mode 100644 attachment_s3/models/ir_binary.py diff --git a/attachment_s3/models/__init__.py b/attachment_s3/models/__init__.py index f7a98ece..aaf38a16 100644 --- a/attachment_s3/models/__init__.py +++ b/attachment_s3/models/__init__.py @@ -1,2 +1 @@ from . import ir_attachment -from . import ir_binary diff --git a/attachment_s3/models/ir_binary.py b/attachment_s3/models/ir_binary.py deleted file mode 100644 index d34bbf5f..00000000 --- a/attachment_s3/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 _s3_stream(self, attachment): - # we will create or own tream and return it - stream_data = self.env["ir.attachment"]._store_file_read(attachment.store_fname) - s3stream = 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 s3stream - - 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("s3://") - ): - # we will create or own stream and return it - return self._s3_stream(record) - elif ( - record._name == "documents.document" - and record.attachment_id - and record.attachment_id.store_fname - and record.attachment_id.store_fname.startswith("s3://") - ): - return self._s3_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: - # document enterprise module if not installed, we just ignore - pass