diff --git a/ir_attachment_google_cloud_storage/README.rst b/ir_attachment_google_cloud_storage/README.rst new file mode 100644 index 000000000..2b9fad64a --- /dev/null +++ b/ir_attachment_google_cloud_storage/README.rst @@ -0,0 +1,51 @@ +.. image:: https://img.shields.io/badge/license-MIT-blue.svg + :target: https://opensource.org/licenses/MIT + :alt: License: MIT + +================================= + Google Cloud Storage Attachment Storage +================================= + +TODO description intro + +TODO detailed description + +Credits +======= + +Contributors +------------ +* `Eugene Molotov `__: + + * :one::zero: init version of the module + +Sponsors +-------- +* `IT-Projects LLC `__ + +Maintainers +----------- +* `IT-Projects LLC `__ + + To get a guaranteed support + you are kindly requested to purchase the module + at `odoo apps store `__. + + Thank you for understanding! + + `IT-Projects Team `__ + +Further information +=================== + +Demo: http://runbot.it-projects.info/demo/misc-addons/11.0 + +HTML Description: https://apps.odoo.com/apps/modules/11.0/ir_attachment_google_cloud_storage/ + +Usage instructions: ``_ + +Changelog: ``_ + +Notifications on updates: `via Atom `_, `by Email `_ + +Tested on Odoo 11.0 a827d3015c6994bc3c779f9ba5cd270d8bdd8edd diff --git a/ir_attachment_google_cloud_storage/__init__.py b/ir_attachment_google_cloud_storage/__init__.py new file mode 100644 index 000000000..1d99e04b6 --- /dev/null +++ b/ir_attachment_google_cloud_storage/__init__.py @@ -0,0 +1,3 @@ +# License MIT (https://opensource.org/licenses/MIT). + +from . import models diff --git a/ir_attachment_google_cloud_storage/__manifest__.py b/ir_attachment_google_cloud_storage/__manifest__.py new file mode 100644 index 000000000..0cdceca1f --- /dev/null +++ b/ir_attachment_google_cloud_storage/__manifest__.py @@ -0,0 +1,39 @@ +# Copyright 2020 Eugene Molotov +# License MIT (https://opensource.org/licenses/MIT). + +{ + "name": """Google Cloud Storage Attachment Storage""", + "summary": """TODO description intro""", + "category": "Extra Tools", + # "live_test_url": "http://apps.it-projects.info/shop/product/DEMO-URL?version=12.0", + "images": [], + "version": "11.0.1.0.0", + "application": False, + "author": "IT-Projects LLC, Eugene Molotov", + "support": "apps@it-projects.info", + "website": "https://apps.odoo.com/apps/modules/11.0/ir_attachment_google_cloud_storage/", + "license": "Other OSI approved licence", # MIT + # "price": 9.00, + # "currency": "EUR", + "depends": ["base", "base_setup"], + "external_dependencies": {"python": ["google.cloud.storage"], "bin": []}, + "data": ["views/res_config_settings_views.xml"], + "demo": [], + "qweb": [], + "post_load": None, + "pre_init_hook": None, + "post_init_hook": None, + "uninstall_hook": None, + "auto_install": False, + "installable": True, + # "demo_title": "Google Drive Attachment Storage", + # "demo_addons": [ + # ], + # "demo_addons_hidden": [ + # ], + # "demo_url": "DEMO-URL", + # "demo_summary": "TODO description intro", + # "demo_images": [ + # "images/MAIN_IMAGE", + # ] +} diff --git a/ir_attachment_google_cloud_storage/doc/changelog.rst b/ir_attachment_google_cloud_storage/doc/changelog.rst new file mode 100644 index 000000000..5583eb326 --- /dev/null +++ b/ir_attachment_google_cloud_storage/doc/changelog.rst @@ -0,0 +1,4 @@ +`1.0.0` +------- + +- **Init version** diff --git a/ir_attachment_google_cloud_storage/doc/index.rst b/ir_attachment_google_cloud_storage/doc/index.rst new file mode 100644 index 000000000..d3870a75f --- /dev/null +++ b/ir_attachment_google_cloud_storage/doc/index.rst @@ -0,0 +1,35 @@ +================================= + Google Drive Attachment Storage +================================= + +Installation +============ +{Instruction about things to do before actual installation} + +* {OPTIONAL }`Activate longpolling `__ +* {Additional notes if any} +* `Install `__ this module in a usual way + +Configuration +============= + +{Instruction how to configure the module before start to use it} + +* `Log in as SUPERUSER `__ +* `Activate Developer Mode `__ +* Open menu ``[[ {Menu} ]] >> {Submenu} >> {Subsubmenu}`` +* Click ``[{Button Name}]`` + +Usage +===== + +{Instruction for daily usage. It should describe how to check that module works. What shall user do and what would user get.} + +* Open menu ``[[ {Menu} ]]>> {Submenu} >> {Subsubmenu}`` +* Click ``[{Button Name}]`` +* RESULT: {what user gets, how the modules changes default behaviour} + +Uninstallation +============== + +{Optional section for uninstallation notes. Delete it if you don't have notes for uninstallation.} diff --git a/ir_attachment_google_cloud_storage/models/__init__.py b/ir_attachment_google_cloud_storage/models/__init__.py new file mode 100644 index 000000000..fcf9596c4 --- /dev/null +++ b/ir_attachment_google_cloud_storage/models/__init__.py @@ -0,0 +1,4 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +from . import ir_attachment +from . import res_config_settings diff --git a/ir_attachment_google_cloud_storage/models/ir_attachment.py b/ir_attachment_google_cloud_storage/models/ir_attachment.py new file mode 100644 index 000000000..3a8410b6e --- /dev/null +++ b/ir_attachment_google_cloud_storage/models/ir_attachment.py @@ -0,0 +1,125 @@ +# Copyright 2020 Eugene Molotov +# License MIT (https://opensource.org/licenses/MIT). + +import base64 +import json +import logging + +from odoo import api, models +from odoo.tools import human_size + +_logger = logging.getLogger(__name__) +_logger.setLevel(logging.DEBUG) + +PREFIX = "google_cloud_storage://" + + +class IrAttachment(models.Model): + + _inherit = "ir.attachment" + + # это нагло скопировано с ir_attachment_url + @api.multi + def _filter_protected_attachments(self): + return self.filtered( + lambda r: r.res_model not in ["ir.ui.view", "ir.ui.menu"] + or not r.name.startswith("/web/content/") + or not r.name.startswith("/web/static/") + ) + + def _inverse_datas(self): + our_records = self._filter_protected_attachments() + + if our_records: + # make sure, you can use google drive + # otherwise - use default behaviour + try: + bucket = self.env["res.config.settings"].get_google_cloud_storage_bucket() + except Exception: + _logger.exception( + "Google Cloud Storage is not configured properly. Keeping attachments as usual" + ) + return super(IrAttachment, self)._inverse_datas() + + for attach in our_records: + bin_value = base64.b64decode(attach.datas) + checksum = self._compute_checksum(bin_value) + fname = self._file_write_google_cloud_storage(bucket, bin_value, checksum) + vals = { + "file_size": len(bin_value), + "checksum": checksum, + "index_content": self._index( + bin_value, attach.datas_fname, attach.mimetype + ), + "store_fname": fname, + "db_datas": False, + "type": "binary", + "datas_fname": attach.datas_fname, + } + super(IrAttachment, attach.sudo()).write(vals) + + return super(IrAttachment, self - our_records)._inverse_datas() + + def _file_read(self, fname, bin_size=False): + if not fname.startswith(PREFIX): + return super(IrAttachment, self)._file_read(fname, bin_size) + + bucket = self.env["res.config.settings"].get_google_cloud_storage_bucket() + + file_id = fname[len(PREFIX) :] + _logger.debug("reading file with id {}".format(file_id)) + + blob = bucket.get_blob(file_id) + + if bin_size: + return human_size(blob.size) + else: + return base64.b64encode(blob.download_as_string()) + + def _file_write_google_cloud_storage(self, bucket, bin_value, checksum): + file_id = "odoo/{}".format(checksum) + + blob = bucket.blob(file_id) + blob.upload_from_string(bin_value) + + _logger.debug("uploaded file with id {}".format(file_id)) + return PREFIX + file_id + + def _file_delete(self, fname): + if not fname.startswith(PREFIX): + return super(IrAttachment, self)._file_delete(fname) + + bucket = self.env["res.config.settings"].get_google_cloud_storage_bucket() + + file_id = fname[len(PREFIX) :] + _logger.debug("deleting file with id {}".format(file_id)) + + blob = bucket.get_blob(file_id) + + blob.delete() + + def force_storage_google_cloud_storage(self): + bucket = self.env["res.config.settings"].get_google_cloud_storage_bucket() + + attachment_ids = self._search([ + ('store_fname', 'not ilike', PREFIX), + ('store_fname', '!=', False), + ('res_model', 'not in', ["ir.ui.view", "ir.ui.menu"]), + ]) + + _logger.info("%s attachments to store to google cloud storage" % len(attachment_ids)) + for attach in map(self.browse, attachment_ids): + _logger.info("storing %s", repr(attach)) + + old_store_fname = attach.store_fname + data = self._file_read(old_store_fname, bin_size=False) + bin_data = base64.b64decode(data) if data else b'' + checksum = self._compute_checksum(bin_data) if not attach.checksum else attach.checksum + + new_store_fname = self._file_write_google_cloud_storage(bucket, bin_data, checksum) + attach.write({ + 'store_fname': new_store_fname, + }) + self._file_delete(old_store_fname) + + self.env.cr.commit() diff --git a/ir_attachment_google_cloud_storage/models/res_config_settings.py b/ir_attachment_google_cloud_storage/models/res_config_settings.py new file mode 100644 index 000000000..770f7bdde --- /dev/null +++ b/ir_attachment_google_cloud_storage/models/res_config_settings.py @@ -0,0 +1,95 @@ +# Copyright 2020 Eugene Molotov +# License MIT (https://opensource.org/licenses/MIT). + +from odoo import api, fields, models +from google.oauth2 import service_account +from google.cloud import storage +import json +import os + +GOOGLE_APPLICATION_CREDENTIALS = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") +GCS_BUCKETNAME = os.environ.get("GCS_BUCKETNAME") + +class ResConfigSettings(models.TransientModel): + + _inherit = "res.config.settings" + + def _compute_is_in_env(self): + self.is_google_cloud_storage_credentials_in_env = bool(GOOGLE_APPLICATION_CREDENTIALS) + self.is_google_cloud_storage_bucket_in_env = bool(GCS_BUCKETNAME) + + is_google_cloud_storage_credentials_in_env = fields.Boolean(compute=_compute_is_in_env, store=False) + is_google_cloud_storage_bucket_in_env = fields.Boolean(compute=_compute_is_in_env, store=False) + + google_cloud_storage_credentials = fields.Char( + string="Google Application Credentials (for Google Cloud Storage)", + config_parameter="google_cloud_storage.credentials" + ) + google_cloud_storage_bucket = fields.Char( + string="Bucket (for Google Cloud Storage)", + config_parameter="google_cloud_storage.bucket" + ) + + + def get_google_cloud_storage_client(self): + credentials = None + + if GOOGLE_APPLICATION_CREDENTIALS: + with open(GOOGLE_APPLICATION_CREDENTIALS, "r") as f: + credentials = f.read() + else: + icp = self.env["ir.config_parameter"].sudo() + credentials = icp.get_param("google_cloud_storage.credentials") + + if not credentials: + raise Exception("No Google Cloud Storage credendtials given") + + return storage.Client( + None, + credentials=service_account.Credentials.from_service_account_info( + json.loads(credentials) + ) + ) + + def get_google_cloud_storage_bucket(self): + client = self.get_google_cloud_storage_client() + bucket = None + + if GCS_BUCKETNAME: + bucket = GCS_BUCKETNAME + else: + icp = self.env["ir.config_parameter"].sudo() + bucket = icp.get_param("google_cloud_storage.bucket") + + if not bucket: + raise Exception("No Google Cloud Storage bucket given") + + return client.get_bucket(bucket) + + + @api.model + def get_values(self): + res = super(ResConfigSettings, self).get_values() + icp = self.env["ir.config_parameter"].sudo() + + res.update( + google_cloud_storage_credentials=icp.get_param( + "google_cloud_storage.credentials", "" + ), + google_cloud_storage_bucket=icp.get_param( + "google_cloud_storage.bucket", "" + ), + ) + return res + + def set_values(self): + super(ResConfigSettings, self).set_values() + icp = self.env["ir.config_parameter"].sudo() + icp.set_param( + "google_cloud_storage.credentials", + self.google_cloud_storage_credentials or "{}", + ) + icp.set_param( + "google_cloud_storage.bucket", + self.google_cloud_storage_bucket or "", + ) diff --git a/ir_attachment_google_cloud_storage/static/description/icon.png b/ir_attachment_google_cloud_storage/static/description/icon.png new file mode 100644 index 000000000..b43a0a135 Binary files /dev/null and b/ir_attachment_google_cloud_storage/static/description/icon.png differ diff --git a/ir_attachment_google_cloud_storage/views/res_config_settings_views.xml b/ir_attachment_google_cloud_storage/views/res_config_settings_views.xml new file mode 100644 index 000000000..d3f461142 --- /dev/null +++ b/ir_attachment_google_cloud_storage/views/res_config_settings_views.xml @@ -0,0 +1,32 @@ + + + + + res.config.settings.view.form.inherit.base_setup + res.config.settings + + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/requirements.txt b/requirements.txt index 27fc94ffb..49ada2243 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ pretty_bad_protocol requests_mock dropbox google-api-python-client +google-cloud-storage