Skip to content

Commit

Permalink
add(attachment_s3): partition fn
Browse files Browse the repository at this point in the history
  • Loading branch information
dnplkndll committed Aug 3, 2022
1 parent dc83bd7 commit c007cd1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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

0 comments on commit c007cd1

Please sign in to comment.