Skip to content
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

add(attachment_s3): partition fn #3

Open
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion attachment_s3/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
'python': ['boto3'],
},
'website': 'https://www.camptocamp.com',
'data': [],
'data': ['data/res_config_settings_data.xml'],
'installable': True,
}
9 changes: 9 additions & 0 deletions attachment_s3/data/res_config_settings_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version='1.0' encoding='utf-8'?>
<odoo noupdate="1">

<record id="ir_attachment_storage_partition" model="ir.config_parameter">
<field name="key">ir_attachment.storage.partition</field>
<field name="value">%Y-%m</field>
</record>

</odoo>
9 changes: 9 additions & 0 deletions attachment_s3/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import io
from urllib.parse import urlsplit
from datetime import datetime

from odoo import _, api, exceptions, models
from ..s3uri import S3Uri
Expand Down Expand Up @@ -143,8 +144,16 @@ def _store_file_read(self, fname):
@api.model
def _store_file_write(self, key, bin_data):
location = self.env.context.get('storage_location') or self._storage()
partition = self.env['ir.config_parameter'].sudo().get_param(
'ir_attachment.storage.partition',
)
if location == 's3':
bucket = self._get_s3_bucket()
if partition:
key = '%s/%s' % (
datetime.now().strftime(partition),
key
)
obj = bucket.Object(key=key)
with io.BytesIO() as file:
file.write(bin_data)
Expand Down