diff --git a/base_multi_image/README.rst b/base_multi_image/README.rst
index 3674b6dd013..8e05f046ed3 100644
--- a/base_multi_image/README.rst
+++ b/base_multi_image/README.rst
@@ -57,18 +57,29 @@ To develop a module based on this one:
* If the model you are extending already had an image field, and you want to
trick Odoo to make those images to multi-image mode, you will need to make
- use of the provided :meth:`~.hooks.pre_init_hook_for_submodules`, like
- the ``product_multi_image`` module does::
+ use of the provided :meth:`~.hooks.pre_init_hook_for_submodules` and
+ :meth:`~.hooks.uninstall_hook_for_submodules`, like the
+ ``product_multi_image`` module does::
- from openerp.addons.base_multi_image.hooks import \
- pre_init_hook_for_submodules
+ try:
+ from openerp.addons.base_multi_image.hooks import \
+ pre_init_hook_for_submodules
+ except:
+ pass
def pre_init_hook(cr):
+ """Transform single into multi images."""
pre_init_hook_for_submodules(cr, "product.template", "image")
pre_init_hook_for_submodules(cr, "product.product", "image_variant")
+ def uninstall_hook(cr, registry):
+ """Remove multi images for models that no longer use them."""
+ uninstall_hook_for_submodules(cr, registry, "product.template")
+ uninstall_hook_for_submodules(cr, registry, "product.product")
+
+
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/149/9.0
diff --git a/base_multi_image/__openerp__.py b/base_multi_image/__openerp__.py
index 7fc905cb8f4..77159b11f80 100644
--- a/base_multi_image/__openerp__.py
+++ b/base_multi_image/__openerp__.py
@@ -8,7 +8,7 @@
{
"name": "Multiple images base",
"summary": "Allow multiple images for database objects",
- "version": "9.0.1.0.0",
+ "version": "9.0.1.1.0",
"author": "Serv. Tecnol. Avanzados - Pedro M. Baeza, "
"Antiun IngenierĂa, S.L., Sodexis, "
"Odoo Community Association (OCA)",
diff --git a/base_multi_image/hooks.py b/base_multi_image/hooks.py
index 55496df407d..17e7a9618fc 100644
--- a/base_multi_image/hooks.py
+++ b/base_multi_image/hooks.py
@@ -35,7 +35,12 @@ def pre_init_hook_for_submodules(cr, model, field):
# fields.Binary(attachment=True), get the ir_attachment record ID
else:
extract_query = """
- SELECT res_id, res_model, 'filestore', id
+ SELECT
+ res_id,
+ res_model,
+ CONCAT_WS(',', res_model, res_id),
+ 'filestore',
+ id
FROM ir_attachment
WHERE res_field='%(field)s' AND res_model='%(model)s'
""" % {"model": model, "field": field}
@@ -45,6 +50,7 @@ def pre_init_hook_for_submodules(cr, model, field):
INSERT INTO base_multi_image_image (
owner_id,
owner_model,
+ owner_ref_id,
storage,
%s
)
@@ -53,6 +59,24 @@ def pre_init_hook_for_submodules(cr, model, field):
)
+def uninstall_hook_for_submodules(cr, registry, model):
+ """Remove multi-images for a given model.
+
+ :param openerp.sql_db.Cursor cr:
+ Database cursor.
+
+ :param openerp.modules.registry.RegistryManager registry:
+ Database registry, using v7 api.
+
+ :param str model:
+ Model technical name, like "res.partner". All multi-images for that
+ model will be deleted
+ """
+ Image = registry["base_multi_image.image"]
+ ids = Image.search(cr, SUPERUSER_ID, [("owner_model", "=", model)])
+ Image.unlink(cr, SUPERUSER_ID, ids)
+
+
def table_has_column(cr, table, field):
query = """
SELECT %(field)s
diff --git a/base_multi_image/models/image.py b/base_multi_image/models/image.py
index dfb5501d327..cf8a228ef5c 100644
--- a/base_multi_image/models/image.py
+++ b/base_multi_image/models/image.py
@@ -24,9 +24,17 @@ class Image(models.Model):
owner_id = fields.Integer(
"Owner",
- required=True)
+ required=True,
+ ondelete="cascade", # This Integer is really a split Many2one
+ )
owner_model = fields.Char(
required=True)
+ owner_ref_id = fields.Reference(
+ selection="_selection_owner_ref_id",
+ string="Referenced Owner",
+ compute="_compute_owner_ref_id",
+ store=True,
+ )
storage = fields.Selection(
[('url', 'URL'), ('file', 'OS file'), ('db', 'Database'),
('filestore', 'Filestore')],
@@ -74,6 +82,19 @@ class Image(models.Model):
show_technical = fields.Boolean(
compute="_show_technical")
+ @api.model
+ @tools.ormcache("self")
+ def _selection_owner_ref_id(self):
+ """Allow any model; after all, this field is readonly."""
+ return [(r.model, r.name) for r in self.env["ir.model"].search([])]
+
+ @api.multi
+ @api.depends("owner_model", "owner_id")
+ def _compute_owner_ref_id(self):
+ """Get a reference field based on the split model and id fields."""
+ for s in self:
+ s.owner_ref_id = "{0.owner_model},{0.owner_id}".format(s)
+
@api.multi
@api.depends('storage', 'path', 'file_db_store', 'url')
def _get_image(self):
@@ -116,7 +137,7 @@ def _get_image_from_url(self):
return self._get_image_from_url_cached(self.url)
@api.model
- @tools.ormcache(skiparg=1)
+ @tools.ormcache("url")
def _get_image_from_url_cached(self, url):
"""Allow to download an image and cache it by its URL."""
if url:
diff --git a/base_multi_image/views/image_view.xml b/base_multi_image/views/image_view.xml
index e7bd342d210..8b41025ef23 100644
--- a/base_multi_image/views/image_view.xml
+++ b/base_multi_image/views/image_view.xml
@@ -19,6 +19,7 @@
+