Browse Source

Merge pull request #468 from osiell/9.0-base_multi_image_binary_attachment

[FIX] base_multi_image - New storage backend 'Filestore'
pull/445/merge
Daniel Reis 8 years ago
committed by GitHub
parent
commit
30f2075e32
  1. 43
      base_multi_image/hooks.py
  2. 28
      base_multi_image/models/image.py
  3. 6
      base_multi_image/views/image_view.xml

43
base_multi_image/hooks.py

@ -22,23 +22,42 @@ def pre_init_hook_for_submodules(cr, model, field):
"""
env = api.Environment(cr, SUPERUSER_ID, dict())
with cr.savepoint():
table = env[model]._table
column_exists = table_has_column(cr, table, field)
# fields.Binary(), extract the binary content directly from the table
if column_exists:
extract_query = """
SELECT id, %%s, 'db', %(field)s
FROM %(table)s
WHERE %(field)s IS NOT NULL
""" % {"table": table, "field": field}
image_field = 'file_db_store'
# fields.Binary(attachment=True), get the ir_attachment record ID
else:
extract_query = """
SELECT res_id, res_model, 'filestore', id
FROM ir_attachment
WHERE res_field='%(field)s' AND res_model='%(model)s'
""" % {"model": model, "field": field}
image_field = 'attachment_id'
cr.execute(
"""
INSERT INTO base_multi_image_image (
owner_id,
owner_model,
storage,
file_db_store
%s
)
SELECT
id,
%%s,
'db',
%(field)s
FROM
%(table)s
WHERE
%(field)s IS NOT NULL
""" % {"table": env[model]._table, "field": field},
(model,)
%s
""" % (image_field, extract_query)
)
def table_has_column(cr, table, field):
query = """
SELECT %(field)s
FROM information_schema.columns
WHERE table_name=%(table)s and column_name=%(field)s;
"""
cr.execute(query, {'table': table, 'field': field})
return bool(cr.fetchall())

28
base_multi_image/models/image.py

@ -28,7 +28,8 @@ class Image(models.Model):
owner_model = fields.Char(
required=True)
storage = fields.Selection(
[('url', 'URL'), ('file', 'OS file'), ('db', 'Database')],
[('url', 'URL'), ('file', 'OS file'), ('db', 'Database'),
('filestore', 'Filestore')],
required=True)
name = fields.Char(
'Image title',
@ -37,6 +38,10 @@ class Image(models.Model):
extension = fields.Char(
'File extension',
readonly=True)
attachment_id = fields.Many2one(
'ir.attachment',
string='Attachment',
domain="[('index_content', '=', 'image')]")
file_db_store = fields.Binary(
'Image stored in database',
filters='*.png,*.jpg,*.gif')
@ -84,6 +89,10 @@ class Image(models.Model):
"default_owner_%s" % f not in self.env.context
for f in ("id", "model"))
@api.multi
def _get_image_from_filestore(self):
return self.attachment_id.datas
@api.multi
def _get_image_from_db(self):
return self.file_db_store
@ -157,20 +166,31 @@ class Image(models.Model):
self.name, self.extension = os.path.splitext(self.filename)
self.name = self._make_name_pretty(self.name)
@api.onchange('attachment_id')
def _onchange_attachmend_id(self):
if self.attachment_id:
self.name = self.attachment_id.res_name
@api.constrains('storage', 'url')
def _check_url(self):
if self.storage == 'url' and not self.url:
raise exceptions.ValidationError(
'You must provide an URL for the image.')
_('You must provide an URL for the image.'))
@api.constrains('storage', 'path')
def _check_path(self):
if self.storage == 'file' and not self.path:
raise exceptions.ValidationError(
'You must provide a file path for the image.')
_('You must provide a file path for the image.'))
@api.constrains('storage', 'file_db_store')
def _check_store(self):
if self.storage == 'db' and not self.file_db_store:
raise exceptions.ValidationError(
'You must provide an attached file for the image.')
_('You must provide an attached file for the image.'))
@api.constrains('storage', 'attachment_id')
def _check_attachment_id(self):
if self.storage == 'filestore' and not self.attachment_id:
raise exceptions.ValidationError(
_('You must provide an attachment for the image.'))

6
base_multi_image/views/image_view.xml

@ -50,6 +50,12 @@
'required': [('storage', '=', 'db')],
}"
filename="filename"/>
<field
name="attachment_id"
attrs="{
'invisible': [('storage', '!=', 'filestore')],
'required': [('storage', '=', 'filestore')],
}"/>
</group>
<group string="Preview">
<field name="image_medium"

Loading…
Cancel
Save