-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
13.0 ir_attachment_google_cloud_storage #796
Open
em230418
wants to merge
1
commit into
it-projects-llc:misc-addons-13.0-ir_attachment_google_cloud_storage
Choose a base branch
from
em230418:misc-addons-13.0-ir_attachment_google_cloud_storage
base: misc-addons-13.0-ir_attachment_google_cloud_storage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <https://it-projects.info/team/em230418>`__: | ||
|
||
* :one::zero: init version of the module | ||
|
||
Sponsors | ||
-------- | ||
* `IT-Projects LLC <https://it-projects.info>`__ | ||
|
||
Maintainers | ||
----------- | ||
* `IT-Projects LLC <https://it-projects.info>`__ | ||
|
||
To get a guaranteed support | ||
you are kindly requested to purchase the module | ||
at `odoo apps store <https://apps.odoo.com/apps/modules/11.0/ir_attachment_google_cloud_storage/>`__. | ||
|
||
Thank you for understanding! | ||
|
||
`IT-Projects Team <https://www.it-projects.info/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: `<doc/index.rst>`_ | ||
|
||
Changelog: `<doc/changelog.rst>`_ | ||
|
||
Notifications on updates: `via Atom <https://github.com/it-projects-llc/misc-addons/commits/11.0/ir_attachment_google_cloud_storage.atom>`_, `by Email <https://blogtrottr.com/?subscribe=https://github.com/it-projects-llc/misc-addons/commits/11.0/ir_attachment_google_cloud_storage.atom>`_ | ||
|
||
Tested on Odoo 11.0 a827d3015c6994bc3c779f9ba5cd270d8bdd8edd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# License MIT (https://opensource.org/licenses/MIT). | ||
|
||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2020 Eugene Molotov <https://it-projects.info/team/em230418> | ||
# 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": "13.0.1.0.0", | ||
"application": False, | ||
"author": "IT-Projects LLC, Eugene Molotov", | ||
"support": "[email protected]", | ||
"website": "https://apps.odoo.com/apps/modules/11.0/ir_attachment_google_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", | ||
# ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
`1.0.0` | ||
------- | ||
|
||
- **Init version** | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
================================= | ||
Google Drive Attachment Storage | ||
================================= | ||
|
||
Installation | ||
============ | ||
{Instruction about things to do before actual installation} | ||
|
||
* {OPTIONAL }`Activate longpolling <https://odoo-development.readthedocs.io/en/latest/admin/longpolling.html>`__ | ||
* {Additional notes if any} | ||
* `Install <https://odoo-development.readthedocs.io/en/latest/odoo/usage/install-module.html>`__ this module in a usual way | ||
|
||
Configuration | ||
============= | ||
|
||
{Instruction how to configure the module before start to use it} | ||
|
||
* `Log in as SUPERUSER <https://odoo-development.readthedocs.io/en/latest/odoo/usage/login-as-superuser.html>`__ | ||
* `Activate Developer Mode <https://odoo-development.readthedocs.io/en/latest/odoo/usage/debug-mode.html>`__ | ||
* 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.} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
104 changes: 104 additions & 0 deletions
104
ir_attachment_google_cloud_storage/models/ir_attachment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Copyright 2020 Eugene Molotov <https://it-projects.info/team/em230418> | ||
# 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" | ||
|
||
@api.model_create_multi | ||
def create(self, vals_list): | ||
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).create(vals_list) | ||
|
||
# based on https://github.com/odoo/odoo/blob/fa852ba1c5707b71469c410063f338eef261ab2b/odoo/addons/base/models/ir_attachment.py#L506-L524 | ||
record_tuple_set = set() | ||
for values in vals_list: | ||
# remove computed field depending of datas | ||
for field in ('file_size', 'checksum'): | ||
values.pop(field, False) | ||
values = self._check_contents(values) | ||
if 'datas' in values: | ||
# =============== | ||
# check, if attachment must be saved as google drive attachment | ||
# start | ||
data = values.pop('datas') | ||
mimetype = values.pop('mimetype') | ||
if values.get("res_model") not in ["ir.ui.view", "ir.ui.menu"] and self._storage() != 'db' and data: | ||
bin_data = base64.b64decode(data) if data else b'' | ||
checksum = self._compute_checksum(bin_data) | ||
values.update({ | ||
'file_size': len(bin_data), | ||
'checksum': checksum, | ||
'index_content': self._index(bin_data, mimetype), | ||
'store_fname': self._file_write_google_cloud_storage(bucket, bin_data, checksum), | ||
'db_datas': False, | ||
}) | ||
else: | ||
values.update(self._get_datas_related_values(data, mimetype)) | ||
# end | ||
# =============== | ||
# 'check()' only uses res_model and res_id from values, and make an exists. | ||
# We can group the values by model, res_id to make only one query when | ||
# creating multiple attachments on a single record. | ||
record_tuple = (values.get('res_model'), values.get('res_id')) | ||
record_tuple_set.add(record_tuple) | ||
for record_tuple in record_tuple_set: | ||
(res_model, res_id) = record_tuple | ||
self.check('create', values={'res_model':res_model, 'res_id':res_id}) | ||
return super(IrAttachment, self).create(vals_list) | ||
|
||
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() |
74 changes: 74 additions & 0 deletions
74
ir_attachment_google_cloud_storage/models/res_config_settings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright 2020 Eugene Molotov <https://it-projects.info/team/em230418> | ||
# 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 | ||
|
||
class ResConfigSettings(models.TransientModel): | ||
|
||
_inherit = "res.config.settings" | ||
|
||
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): | ||
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() | ||
|
||
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 "", | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions
30
ir_attachment_google_cloud_storage/views/res_config_settings_views.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- Copyright 2020 Eugene Molotov <https://it-projects.info/team/em230418> | ||
License MIT (https://opensource.org/licenses/MIT). --> | ||
<odoo> | ||
<record id="res_config_settings_view_form" model="ir.ui.view"> | ||
<field name="name">res.config.settings.view.form.inherit.base_setup</field> | ||
<field name="model">res.config.settings</field> | ||
<field name="inherit_id" ref="base_setup.res_config_settings_view_form"/> | ||
<field name="arch" type="xml"> | ||
<xpath expr="//field[@name='module_google_drive']/../.." position="after"> | ||
<div class="col-12 col-lg-6 o_setting_box"> | ||
<div class="o_setting_left_pane"> | ||
</div> | ||
<div class="o_setting_right_pane"> | ||
<label for="google_cloud_storage_credentials"/> | ||
<field name="google_cloud_storage_credentials" widget="text" /> | ||
</div> | ||
</div> | ||
<div class="col-12 col-lg-6 o_setting_box"> | ||
<div class="o_setting_left_pane"> | ||
</div> | ||
<div class="o_setting_right_pane"> | ||
<label for="google_cloud_storage_bucket"/> | ||
<field name="google_cloud_storage_bucket" /> | ||
</div> | ||
</div> | ||
</xpath> | ||
</field> | ||
</record> | ||
</odoo> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍