diff --git a/delivery_carrier_label/README.rst b/delivery_carrier_label/README.rst new file mode 100644 index 0000000..5a63ce1 --- /dev/null +++ b/delivery_carrier_label/README.rst @@ -0,0 +1,4 @@ +Shipping label external location +-------------------------------- + +* Add external storage location management for shipping label. diff --git a/delivery_carrier_label/__init__.py b/delivery_carrier_label/__init__.py new file mode 100644 index 0000000..b140940 --- /dev/null +++ b/delivery_carrier_label/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2022 Vincent Renaville (Camptocamp) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import models diff --git a/delivery_carrier_label/__openerp__.py b/delivery_carrier_label/__openerp__.py new file mode 100644 index 0000000..9adbe5b --- /dev/null +++ b/delivery_carrier_label/__openerp__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# © 2022 Vincent Renaville (Camptocamp) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + 'name': 'External storage - Shipping Label', + 'version': '9.0.1.0.0', + 'author': 'Camptocamp', + 'license': 'AGPL-3', + 'category': 'Knowledge Management', + 'website': 'http://www.camptocamp.com', + 'images': [], + 'depends': [ + 'base_delivery_carrier_label', + 'base_attachment_object_storage', + ], + 'data': [ + ], + 'test': [], + 'installable': True, + 'auto_install': True, +} diff --git a/delivery_carrier_label/models/__init__.py b/delivery_carrier_label/models/__init__.py new file mode 100644 index 0000000..6abe813 --- /dev/null +++ b/delivery_carrier_label/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2022 Vincent Renaville (Camptocamp) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import shipping_label diff --git a/delivery_carrier_label/models/shipping_label.py b/delivery_carrier_label/models/shipping_label.py new file mode 100644 index 0000000..756789b --- /dev/null +++ b/delivery_carrier_label/models/shipping_label.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +from openerp import api, fields, models + + +class ShippingLabel(models.Model): + """Inherit of shipping label to store datas + in the right location if external stroage activated""" + + _inherit = "shipping.label" + + datas = fields.Binary( + compute="_compute_datas", + inverse="_inverse_datas", + string="File Content", + nodrop=True, + ) + + @api.depends("store_fname", "db_datas") + def _compute_datas(self): + for label in self: + values = label.attachment_id._data_get("datas", None) + label.datas = values.get(label.id) + + def _inverse_datas(self): + # override in order to store files that need fast access, + # we keep them in the database instead of the object storage + for label in self: + location = label.attachment_id._storage() + if ( + location == label.attachment_id._get_stores() + and self.attachment_id._save_in_db_anyway() + ): + # compute the fields that depend on datas + value = label.datas + bin_data = value and value.decode("base64") or "" + vals = { + "file_size": len(bin_data), + "checksum": self.attachment_id._compute_checksum(bin_data), + "db_datas": value, + # we seriously don't need index content on those fields + "index_content": False, + "store_fname": False, + } + fname = label.store_fname + # write as superuser, as user probably does not + # have write access + super(ShippingLabel, label.sudo()).write(vals) + if fname: + self.attachment_id._file_delete(fname) + continue + self.attachment_id._data_set("datas", label.datas, None)