From e4e0c2dc5d3fd8422075b9a198e2a03c5d2d5aed Mon Sep 17 00:00:00 2001 From: Hiren Pattani-SerpentCS Date: Thu, 7 Oct 2021 19:18:59 +0530 Subject: [PATCH] [ADD] Added the attachment_azure with db operations --- attachment_azure/__init__.py | 19 +++++++ attachment_azure/__manifest__.py | 1 + attachment_azure/controllers/__init__.py | 1 + attachment_azure/controllers/main.py | 66 ++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 attachment_azure/controllers/__init__.py create mode 100644 attachment_azure/controllers/main.py diff --git a/attachment_azure/__init__.py b/attachment_azure/__init__.py index 0650744..025ee2d 100644 --- a/attachment_azure/__init__.py +++ b/attachment_azure/__init__.py @@ -1 +1,20 @@ +# Copyright 2016-2019 Camptocamp SA +# Copyright 2021 Open Source Integrators +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) +import os + from . import models +from . import controllers + +from odoo import api, SUPERUSER_ID + + +def _post_init_hook(cr, registry): + # create the Azure container after module installation + if os.environ.get("AZURE_STORAGE_CONNECTION_STRING", False): + env = api.Environment(cr, SUPERUSER_ID, {}) + env["ir.attachment"]._get_azure_container() + env["ir.config_parameter"].create( + {"key": "ir_attachment.location", "value": "azure"} + ) + env["ir.attachment"].force_storage() diff --git a/attachment_azure/__manifest__.py b/attachment_azure/__manifest__.py index 42200f3..d4003a3 100644 --- a/attachment_azure/__manifest__.py +++ b/attachment_azure/__manifest__.py @@ -21,5 +21,6 @@ "website": "https://github.com/camptocamp/odoo-cloud-platform", "installable": True, "development_status": "Beta", + "post_init_hook": "_post_init_hook", "maintainers": ["max3903"], } diff --git a/attachment_azure/controllers/__init__.py b/attachment_azure/controllers/__init__.py new file mode 100644 index 0000000..12a7e52 --- /dev/null +++ b/attachment_azure/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/attachment_azure/controllers/main.py b/attachment_azure/controllers/main.py new file mode 100644 index 0000000..2a28987 --- /dev/null +++ b/attachment_azure/controllers/main.py @@ -0,0 +1,66 @@ +import logging +import os + +from odoo.addons.web.controllers.main import Database +from odoo import http +from odoo import exceptions +from odoo.http import request + +_logger = logging.getLogger(__name__) + + +class Database(Database): + @http.route() + def drop(self, master_pwd, name): + res = super().drop(master_pwd, name) + delete = os.environ.get("AZURE_DELETE_ON_DBDROP", False) + if delete: + attachment_obj = request.env["ir.attachment"] + try: + container_name = attachment_obj._get_container_name() + blob_service_client = attachment_obj._get_blob_service_client() + container_client = blob_service_client.get_container_client( + container_name + ) + container_client.delete_container() + except exceptions.UserError: + _logger.exception("Error deleting attachments from object storage.") + return res + + @http.route() + def duplicate(self, master_pwd, name, new_name): + res = super().duplicate(master_pwd, name, new_name) + duplicate = os.environ.get("AWS_DUPLICATE", False) + if duplicate: + attachment_obj = request.env["ir.attachment"] + try: + copy_from_container = attachment_obj._get_container_name() + copy_to_container = attachment_obj._get_container_name(new_name) + blob_service = attachment_obj._get_blob_service_client() + container_client = blob_service.get_container_client( + copy_from_container + ) + + blobs_list = container_client.list_blobs() + for blob in blobs_list: + blob_url = container_client.make_blob_url( + copy_from_container, blob.name + ) + container_client.copy_blob(copy_to_container, blob.name, blob_url) + except exceptions.UserError: + _logger.exception("Error writing attachments to object storage.") + return res + + @http.route() + def restore(self, master_pwd, backup_file, name, copy=False): + res = super().restore(master_pwd, backup_file, name, copy) + attachment_obj = request.env["ir.attachment"] + try: + attachment_obj._get_azure_container() + request.env["ir.config_parameter"].create( + {"key": "ir_attachment.location", "value": "azure"} + ) + attachment_obj.force_storage() + except exceptions.UserError: + _logger.exception("Error writing attachments to object storage.") + return res