mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-06-24 02:08:36 +00:00
Change CI to GitHub actions
Use copier template from oca/oca-addons-repo-template Target Python3.8 Apply linting Fix a missing call to super Ensure all modules have a 13.0.x.x.x version
This commit is contained in:
@@ -1,2 +1 @@
|
||||
|
||||
from . import models
|
||||
|
||||
@@ -2,17 +2,16 @@
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
|
||||
|
||||
|
||||
{'name': 'Attachments on S3 storage',
|
||||
'summary': 'Store assets and attachments on a S3 compatible object storage',
|
||||
'version': '13.0.1.0.0',
|
||||
'author': 'Camptocamp,Odoo Community Association (OCA)',
|
||||
'license': 'AGPL-3',
|
||||
'category': 'Knowledge Management',
|
||||
'depends': ['base', 'base_attachment_object_storage'],
|
||||
'external_dependencies': {
|
||||
'python': ['boto3'],
|
||||
},
|
||||
'website': 'https://www.camptocamp.com',
|
||||
'data': [],
|
||||
'installable': True,
|
||||
}
|
||||
{
|
||||
"name": "Attachments on S3 storage",
|
||||
"summary": "Store assets and attachments on a S3 compatible object storage",
|
||||
"version": "13.0.1.0.0",
|
||||
"author": "Camptocamp,Odoo Community Association (OCA)",
|
||||
"license": "AGPL-3",
|
||||
"category": "Knowledge Management",
|
||||
"depends": ["base", "base_attachment_object_storage"],
|
||||
"external_dependencies": {"python": ["boto3"]},
|
||||
"website": "https://github.com/camptocamp/odoo-cloud-platform",
|
||||
"data": [],
|
||||
"installable": True,
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from contextlib import closing
|
||||
|
||||
import odoo
|
||||
@@ -14,32 +13,38 @@ _logger = logging.getLogger(__name__)
|
||||
def migrate(cr, version):
|
||||
if not version:
|
||||
return
|
||||
cr.execute("""
|
||||
cr.execute(
|
||||
"""
|
||||
SELECT value FROM ir_config_parameter
|
||||
WHERE key = 'ir_attachment.location'
|
||||
""")
|
||||
"""
|
||||
)
|
||||
row = cr.fetchone()
|
||||
bucket = os.environ.get('AWS_BUCKETNAME')
|
||||
bucket = os.environ.get("AWS_BUCKETNAME")
|
||||
|
||||
if row[0] == 's3' and bucket:
|
||||
if row[0] == "s3" and bucket:
|
||||
uid = odoo.SUPERUSER_ID
|
||||
registry = odoo.modules.registry.Registry(cr.dbname)
|
||||
new_cr = registry.cursor()
|
||||
with closing(new_cr):
|
||||
with odoo.api.Environment.manage():
|
||||
env = odoo.api.Environment(new_cr, uid, {})
|
||||
store_local = env['ir.attachment'].search(
|
||||
[('store_fname', '=like', 's3://%'),
|
||||
'|', ('res_model', '=', 'ir.ui.view'),
|
||||
('res_field', 'in', ['image_small',
|
||||
'image_medium',
|
||||
'web_icon_data'])
|
||||
],
|
||||
store_local = env["ir.attachment"].search(
|
||||
[
|
||||
("store_fname", "=like", "s3://%"),
|
||||
"|",
|
||||
("res_model", "=", "ir.ui.view"),
|
||||
(
|
||||
"res_field",
|
||||
"in",
|
||||
["image_small", "image_medium", "web_icon_data"],
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
_logger.info(
|
||||
'Moving %d attachments from S3 to DB for fast access',
|
||||
len(store_local)
|
||||
"Moving %d attachments from S3 to DB for fast access",
|
||||
len(store_local),
|
||||
)
|
||||
for attachment_id in store_local.ids:
|
||||
# force re-storing the document, will move
|
||||
@@ -52,10 +57,13 @@ def migrate(cr, version):
|
||||
# attachments on each loop.
|
||||
try:
|
||||
env.clear()
|
||||
attachment = env['ir.attachment'].browse(attachment_id)
|
||||
_logger.info('Moving attachment %s (id: %s)',
|
||||
attachment.name, attachment.id)
|
||||
attachment.write({'datas': attachment.datas})
|
||||
attachment = env["ir.attachment"].browse(attachment_id)
|
||||
_logger.info(
|
||||
"Moving attachment %s (id: %s)",
|
||||
attachment.name,
|
||||
attachment.id,
|
||||
)
|
||||
attachment.write({"datas": attachment.datas})
|
||||
new_cr.commit()
|
||||
except:
|
||||
except Exception:
|
||||
new_cr.rollback()
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
|
||||
from . import ir_attachment
|
||||
|
||||
@@ -3,12 +3,13 @@
|
||||
|
||||
|
||||
import base64
|
||||
import io
|
||||
import logging
|
||||
import os
|
||||
import io
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from odoo import _, api, exceptions, models
|
||||
|
||||
from ..s3uri import S3Uri
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
@@ -27,9 +28,9 @@ class IrAttachment(models.Model):
|
||||
_inherit = "ir.attachment"
|
||||
|
||||
def _get_stores(self):
|
||||
l = ['s3']
|
||||
l += super()._get_stores()
|
||||
return l
|
||||
stores = ["s3"]
|
||||
stores += super()._get_stores()
|
||||
return stores
|
||||
|
||||
@api.model
|
||||
def _get_s3_bucket(self, name=None):
|
||||
@@ -46,42 +47,43 @@ class IrAttachment(models.Model):
|
||||
from the environment variable ``AWS_BUCKETNAME`` will be read.
|
||||
|
||||
"""
|
||||
host = os.environ.get('AWS_HOST')
|
||||
host = os.environ.get("AWS_HOST")
|
||||
|
||||
# Ensure host is prefixed with a scheme (use https as default)
|
||||
if host and not urlsplit(host).scheme:
|
||||
host = 'https://%s' % host
|
||||
host = "https://%s" % host
|
||||
|
||||
region_name = os.environ.get('AWS_REGION')
|
||||
access_key = os.environ.get('AWS_ACCESS_KEY_ID')
|
||||
secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
|
||||
bucket_name = name or os.environ.get('AWS_BUCKETNAME')
|
||||
region_name = os.environ.get("AWS_REGION")
|
||||
access_key = os.environ.get("AWS_ACCESS_KEY_ID")
|
||||
secret_key = os.environ.get("AWS_SECRET_ACCESS_KEY")
|
||||
bucket_name = name or os.environ.get("AWS_BUCKETNAME")
|
||||
# replaces {db} by the database name to handle multi-tenancy
|
||||
bucket_name = bucket_name.format(db=self.env.cr.dbname)
|
||||
|
||||
params = {
|
||||
'aws_access_key_id': access_key,
|
||||
'aws_secret_access_key': secret_key,
|
||||
"aws_access_key_id": access_key,
|
||||
"aws_secret_access_key": secret_key,
|
||||
}
|
||||
if host:
|
||||
params['endpoint_url'] = host
|
||||
params["endpoint_url"] = host
|
||||
if region_name:
|
||||
params['region_name'] = region_name
|
||||
params["region_name"] = region_name
|
||||
if not (access_key and secret_key and bucket_name):
|
||||
msg = _('If you want to read from the %s S3 bucket, the following '
|
||||
'environment variables must be set:\n'
|
||||
'* AWS_ACCESS_KEY_ID\n'
|
||||
'* AWS_SECRET_ACCESS_KEY\n'
|
||||
'If you want to write in the %s S3 bucket, this variable '
|
||||
'must be set as well:\n'
|
||||
'* AWS_BUCKETNAME\n'
|
||||
'Optionally, the S3 host can be changed with:\n'
|
||||
'* AWS_HOST\n'
|
||||
) % (bucket_name, bucket_name)
|
||||
msg = _(
|
||||
"If you want to read from the %s S3 bucket, the following "
|
||||
"environment variables must be set:\n"
|
||||
"* AWS_ACCESS_KEY_ID\n"
|
||||
"* AWS_SECRET_ACCESS_KEY\n"
|
||||
"If you want to write in the %s S3 bucket, this variable "
|
||||
"must be set as well:\n"
|
||||
"* AWS_BUCKETNAME\n"
|
||||
"Optionally, the S3 host can be changed with:\n"
|
||||
"* AWS_HOST\n"
|
||||
) % (bucket_name, bucket_name)
|
||||
|
||||
raise exceptions.UserError(msg)
|
||||
# try:
|
||||
s3 = boto3.resource('s3', **params)
|
||||
s3 = boto3.resource("s3", **params)
|
||||
bucket = s3.Bucket(bucket_name)
|
||||
exists = True
|
||||
try:
|
||||
@@ -89,12 +91,12 @@ class IrAttachment(models.Model):
|
||||
except ClientError as e:
|
||||
# If a client error is thrown, then check that it was a 404 error.
|
||||
# If it was a 404 error, then the bucket does not exist.
|
||||
error_code = e.response['Error']['Code']
|
||||
if error_code == '404':
|
||||
error_code = e.response["Error"]["Code"]
|
||||
if error_code == "404":
|
||||
exists = False
|
||||
except EndpointConnectionError as error:
|
||||
# log verbose error from s3, return short message for user
|
||||
_logger.exception('Error during connection on S3')
|
||||
_logger.exception("Error during connection on S3")
|
||||
raise exceptions.UserError(str(error))
|
||||
|
||||
if not exists:
|
||||
@@ -103,14 +105,13 @@ class IrAttachment(models.Model):
|
||||
else:
|
||||
bucket = s3.create_bucket(
|
||||
Bucket=bucket_name,
|
||||
CreateBucketConfiguration={
|
||||
'LocationConstraint': region_name
|
||||
})
|
||||
CreateBucketConfiguration={"LocationConstraint": region_name},
|
||||
)
|
||||
return bucket
|
||||
|
||||
@api.model
|
||||
def _store_file_read(self, fname, bin_size=False):
|
||||
if fname.startswith('s3://'):
|
||||
if fname.startswith("s3://"):
|
||||
s3uri = S3Uri(fname)
|
||||
try:
|
||||
bucket = self._get_s3_bucket(name=s3uri.bucket())
|
||||
@@ -118,12 +119,10 @@ class IrAttachment(models.Model):
|
||||
_logger.exception(
|
||||
"error reading attachment '%s' from object storage", fname
|
||||
)
|
||||
return ''
|
||||
return ""
|
||||
try:
|
||||
key = s3uri.item()
|
||||
bucket.meta.client.head_object(
|
||||
Bucket=bucket.name, Key=key
|
||||
)
|
||||
bucket.meta.client.head_object(Bucket=bucket.name, Key=key)
|
||||
if bin_size:
|
||||
return bucket.Object(key).content_length
|
||||
with io.BytesIO() as res:
|
||||
@@ -131,33 +130,29 @@ class IrAttachment(models.Model):
|
||||
res.seek(0)
|
||||
read = base64.b64encode(res.read())
|
||||
except ClientError:
|
||||
read = ''
|
||||
_logger.info(
|
||||
"attachment '%s' missing on object storage", fname
|
||||
)
|
||||
read = ""
|
||||
_logger.info("attachment '%s' missing on object storage", fname)
|
||||
return read
|
||||
else:
|
||||
return super()._store_file_read(fname, bin_size)
|
||||
|
||||
@api.model
|
||||
def _store_file_write(self, key, bin_data):
|
||||
location = self.env.context.get('storage_location') or self._storage()
|
||||
if location == 's3':
|
||||
location = self.env.context.get("storage_location") or self._storage()
|
||||
if location == "s3":
|
||||
bucket = self._get_s3_bucket()
|
||||
obj = bucket.Object(key=key)
|
||||
with io.BytesIO() as file:
|
||||
file.write(bin_data)
|
||||
file.seek(0)
|
||||
filename = 's3://%s/%s' % (bucket.name, key)
|
||||
filename = "s3://%s/%s" % (bucket.name, key)
|
||||
try:
|
||||
obj.upload_fileobj(file)
|
||||
except ClientError as error:
|
||||
# log verbose error from s3, return short message for user
|
||||
_logger.exception(
|
||||
'Error during storage of the file %s' % filename
|
||||
)
|
||||
_logger.exception("Error during storage of the file %s" % filename)
|
||||
raise exceptions.UserError(
|
||||
_('The file could not be stored: %s') % str(error)
|
||||
_("The file could not be stored: %s") % str(error)
|
||||
)
|
||||
else:
|
||||
_super = super()
|
||||
@@ -166,28 +161,22 @@ class IrAttachment(models.Model):
|
||||
|
||||
@api.model
|
||||
def _store_file_delete(self, fname):
|
||||
if fname.startswith('s3://'):
|
||||
if fname.startswith("s3://"):
|
||||
s3uri = S3Uri(fname)
|
||||
bucket_name = s3uri.bucket()
|
||||
item_name = s3uri.item()
|
||||
# delete the file only if it is on the current configured bucket
|
||||
# otherwise, we might delete files used on a different environment
|
||||
if bucket_name == os.environ.get('AWS_BUCKETNAME'):
|
||||
if bucket_name == os.environ.get("AWS_BUCKETNAME"):
|
||||
bucket = self._get_s3_bucket()
|
||||
obj = bucket.Object(key=item_name)
|
||||
try:
|
||||
bucket.meta.client.head_object(
|
||||
Bucket=bucket.name, Key=item_name
|
||||
)
|
||||
bucket.meta.client.head_object(Bucket=bucket.name, Key=item_name)
|
||||
obj.delete()
|
||||
_logger.info(
|
||||
'file %s deleted on the object storage' % (fname,)
|
||||
)
|
||||
_logger.info("file %s deleted on the object storage" % (fname,))
|
||||
except ClientError:
|
||||
# log verbose error from s3, return short message for
|
||||
# user
|
||||
_logger.exception(
|
||||
'Error during deletion of the file %s' % fname
|
||||
)
|
||||
_logger.exception("Error during deletion of the file %s" % fname)
|
||||
else:
|
||||
super()._store_file_delete(fname)
|
||||
|
||||
Reference in New Issue
Block a user