From 26f533fe7dd56d4a8c44b89d4409c115a5876db5 Mon Sep 17 00:00:00 2001 From: Matteo Boscolo Date: Fri, 27 Sep 2024 15:18:01 +0200 Subject: [PATCH] FIX: better manage large file --- plm/__manifest__.py | 3 ++- plm/models/ir_attachment.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/plm/__manifest__.py b/plm/__manifest__.py index 3ea6ad42..c088aae4 100644 --- a/plm/__manifest__.py +++ b/plm/__manifest__.py @@ -20,7 +20,7 @@ ############################################################################## { "name": "Product Lifecycle Management", - "version": "17.0.0.6", + "version": "17.0.0.7", "author": "OmniaSolutions", "website": "https://odooplm.omniasolutions.website", "category": "Manufacturing/Product Lifecycle Management (PLM)", @@ -30,6 +30,7 @@ "summary": "PLM-PDM Integration with main CAD editors (SolidWorks, SolidEdge, Inventor, Autocad, Thinkdesign, Freecad, Draftsight)", "images": ["static/img/odoo_plm.png"], "depends": ["base", "board", "product", "mrp"], + 'external_dependencies': {'python': ['base64io']}, "data": [ # data "data/data.xml", diff --git a/plm/models/ir_attachment.py b/plm/models/ir_attachment.py index b4c97775..d2b516ee 100755 --- a/plm/models/ir_attachment.py +++ b/plm/models/ir_attachment.py @@ -20,6 +20,8 @@ import random import string import os +import io +from base64io import Base64IO import time import json import copy @@ -109,6 +111,28 @@ class IrAttachment(models.Model): must_update_from_cad = fields.Boolean("Must Update form CAD", compute="_compute_must_update_from_cad", help="""When this flag is enabled the 2d document must be updated in order to guaranteey the update betwin 2d and 3d document""") + + @api.depends('store_fname', 'db_datas', 'file_size') + @api.depends_context('bin_size') + def _compute_datas(self): + if self._context.get('bin_size'): + for attach in self: + attach.datas = human_size(attach.file_size) + return + + for attach in self: + attach.datas = self.get_stream_b64encode(attach.raw or b'') + + def get_stream_b64encode(self, from_stream): + source = io.BytesIO() + target = io.BytesIO() + source.write(from_stream) + source.seek(0) + with Base64IO(target) as encoded_target: + for line in source: + encoded_target.write(line) + target.seek(0) + return target.read() def _compute_must_update_from_cad(self): ir_attachment_relation = self.env['ir.attachment.relation']