Skip to content

Commit

Permalink
added fixe
Browse files Browse the repository at this point in the history
  • Loading branch information
em230418 committed Mar 24, 2020
1 parent dbbf00b commit fb12bc8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
26 changes: 26 additions & 0 deletions ir_attachment_google_cloud_storage/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,29 @@ def _file_delete(self, fname):
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()
29 changes: 25 additions & 4 deletions ir_attachment_google_cloud_storage/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@
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"
Expand All @@ -21,8 +32,14 @@ class ResConfigSettings(models.TransientModel):


def get_google_cloud_storage_client(self):
icp = self.env["ir.config_parameter"].sudo()
credentials = icp.get_param("google_cloud_storage.credentials")
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")
Expand All @@ -36,9 +53,13 @@ def get_google_cloud_storage_client(self):

def get_google_cloud_storage_bucket(self):
client = self.get_google_cloud_storage_client()
bucket = None

icp = self.env["ir.config_parameter"].sudo()
bucket = icp.get_param("google_cloud_storage.bucket")
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
</div>
<div class="o_setting_right_pane">
<label for="google_cloud_storage_credentials"/>
<field name="google_cloud_storage_credentials" widget="text" />
<field name="is_google_cloud_storage_credentials_in_env" attrs="{'invisible': 1}"/>
<field name="google_cloud_storage_credentials" widget="text" attrs="{'invisible': [('is_google_cloud_storage_credentials_in_env', '=', True)]}"/>
<p attrs="{'invisible': [('is_google_cloud_storage_credentials_in_env', '=', False)]}">Defined in enviroment</p>
</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" />
<field name="is_google_cloud_storage_bucket_in_env" attrs="{'invisible': 1}"/>
<field name="google_cloud_storage_bucket" attrs="{'invisible': [('is_google_cloud_storage_bucket_in_env', '=', True)]}"/>
<p attrs="{'invisible': [('is_google_cloud_storage_bucket_in_env', '=', False)]}">Defined in environment</p>
</div>
</div>
</xpath>
Expand Down

0 comments on commit fb12bc8

Please sign in to comment.