From 049205e5607e984d1c3d08762267b0386b931341 Mon Sep 17 00:00:00 2001 From: Patrick Tombez Date: Tue, 3 Nov 2020 11:36:50 +0100 Subject: [PATCH] [MIG] attachment_s3: Migration to 14.0 --- attachment_s3/__manifest__.py | 4 +- .../migrations/13.0.0.0.1/post-migration.py | 61 ------------------- attachment_s3/models/ir_attachment.py | 8 +-- 3 files changed, 5 insertions(+), 68 deletions(-) delete mode 100644 attachment_s3/migrations/13.0.0.0.1/post-migration.py diff --git a/attachment_s3/__manifest__.py b/attachment_s3/__manifest__.py index 6587650..35e1468 100644 --- a/attachment_s3/__manifest__.py +++ b/attachment_s3/__manifest__.py @@ -4,7 +4,7 @@ {'name': 'Attachments on S3 storage', 'summary': 'Store assets and attachments on a S3 compatible object storage', - 'version': '13.0.1.0.0', + 'version': "14.0.1.0.0", 'author': 'Camptocamp,Odoo Community Association (OCA)', 'license': 'AGPL-3', 'category': 'Knowledge Management', @@ -14,5 +14,5 @@ }, 'website': 'https://www.camptocamp.com', 'data': [], - 'installable': False, + 'installable': True, } diff --git a/attachment_s3/migrations/13.0.0.0.1/post-migration.py b/attachment_s3/migrations/13.0.0.0.1/post-migration.py deleted file mode 100644 index 0e3d96f..0000000 --- a/attachment_s3/migrations/13.0.0.0.1/post-migration.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright 2016-2019 Camptocamp SA -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) - -import logging -import os - -from contextlib import closing - -import odoo - -_logger = logging.getLogger(__name__) - - -def migrate(cr, version): - if not version: - return - cr.execute(""" - SELECT value FROM ir_config_parameter - WHERE key = 'ir_attachment.location' - """) - row = cr.fetchone() - bucket = os.environ.get('AWS_BUCKETNAME') - - if row[0] == 's3' and bucket: - uid = odoo.SUPERUSER_ID - registry = odoo.modules.registry.Registry(cr.dbname) - new_cr = registry.cursor() - with closing(new_cr): - with odoo.api.Environment.manage(): - env = odoo.api.Environment(new_cr, uid, {}) - store_local = env['ir.attachment'].search( - [('store_fname', '=like', 's3://%'), - '|', ('res_model', '=', 'ir.ui.view'), - ('res_field', 'in', ['image_small', - 'image_medium', - 'web_icon_data']) - ], - ) - - _logger.info( - 'Moving %d attachments from S3 to DB for fast access', - len(store_local) - ) - for attachment_id in store_local.ids: - # force re-storing the document, will move - # it from the object storage to the database - - # This is a trick to avoid having the 'datas' function - # fields computed for every attachment on each - # iteration of the loop. The former issue being that - # it reads the content of the file of ALL the - # attachments on each loop. - try: - env.clear() - attachment = env['ir.attachment'].browse(attachment_id) - _logger.info('Moving attachment %s (id: %s)', - attachment.name, attachment.id) - attachment.write({'datas': attachment.datas}) - new_cr.commit() - except: - new_cr.rollback() diff --git a/attachment_s3/models/ir_attachment.py b/attachment_s3/models/ir_attachment.py index 657cce7..8ce5f49 100644 --- a/attachment_s3/models/ir_attachment.py +++ b/attachment_s3/models/ir_attachment.py @@ -107,7 +107,7 @@ class IrAttachment(models.Model): return bucket @api.model - def _store_file_read(self, fname, bin_size=False): + def _store_file_read(self, fname): if fname.startswith('s3://'): s3uri = S3Uri(fname) try: @@ -120,10 +120,8 @@ class IrAttachment(models.Model): try: key = s3uri.item() bucket.meta.client.head_object( - Bucket=bucket.name, Key=key + Bucket=bucket.name, Key=key ) - if bin_size: - return bucket.Object(key).content_length with io.BytesIO() as res: bucket.download_fileobj(key, res) res.seek(0) @@ -135,7 +133,7 @@ class IrAttachment(models.Model): ) return read else: - return super()._store_file_read(fname, bin_size) + return super()._store_file_read(fname) @api.model def _store_file_write(self, key, bin_data):