From 61b401beb8389727020bc029e406a14019c3f506 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Fri, 11 Mar 2016 17:06:41 +0100 Subject: [PATCH] Fix bug that made previous images to disappear. See https://github.com/OCA/product-attribute/pull/135#issuecomment-191358505. --- base_multi_image/README.rst | 14 +++++++++++++ base_multi_image/__openerp__.py | 2 +- base_multi_image/hooks.py | 37 +++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/base_multi_image/README.rst b/base_multi_image/README.rst index 35934e6a1..d33a0c264 100644 --- a/base_multi_image/README.rst +++ b/base_multi_image/README.rst @@ -55,6 +55,20 @@ To develop a module based on this one: }" mode="kanban"/> +* 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:: + + from openerp.addons.base_multi_image.hooks import \ + pre_init_hook_for_submodules + + + def pre_init_hook(cr): + pre_init_hook_for_submodules(cr, "product.template", "image") + pre_init_hook_for_submodules(cr, "product.product", "image_variant") + + .. 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/8.0 diff --git a/base_multi_image/__openerp__.py b/base_multi_image/__openerp__.py index e7fefd8e8..524b8ddcf 100644 --- a/base_multi_image/__openerp__.py +++ b/base_multi_image/__openerp__.py @@ -7,7 +7,7 @@ { "name": "Multiple images base", "summary": "Allow multiple images for database objects", - "version": "8.0.1.0.0", + "version": "8.0.2.0.0", "author": "Serv. Tecnol. Avanzados - Pedro M. Baeza, " "Antiun Ingeniería, S.L., " "Odoo Community Association (OCA)", diff --git a/base_multi_image/hooks.py b/base_multi_image/hooks.py index 8d11fefe6..74d29ed55 100644 --- a/base_multi_image/hooks.py +++ b/base_multi_image/hooks.py @@ -2,16 +2,16 @@ # © 2016 Antiun Ingeniería S.L. - Jairo Llopis # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from openerp import SUPERUSER_ID +from openerp import api, SUPERUSER_ID import logging _logger = logging.getLogger(__name__) -def post_init_hook_for_submodules(cr, registry, model, field): +def pre_init_hook_for_submodules(cr, model, field): """Moves images from single to multi mode. - Feel free to use this as a ``post_init_hook`` for submodules. + Feel free to use this as a ``pre_init_hook`` for submodules. :param str model: Model name, like ``product.template``. @@ -20,14 +20,25 @@ def post_init_hook_for_submodules(cr, registry, model, field): Binary field that had the images in that :param:`model`, like ``image``. """ + env = api.Environment(cr, SUPERUSER_ID, dict()) with cr.savepoint(): - records = registry[model].search( - cr, - SUPERUSER_ID, - [(field, "!=", False)], - context=dict()) - - _logger.info("Moving images from %s to multi image mode.", model) - for r in registry[model].browse(cr, SUPERUSER_ID, records): - _logger.debug("Setting up multi image for record %d.", r.id) - r.image_main = r[field] + cr.execute( + """ + INSERT INTO base_multi_image_image ( + owner_id, + owner_model, + storage, + file_db_store + ) + SELECT + id, + %%s, + 'db', + %(field)s + FROM + %(table)s + WHERE + %(field)s IS NOT NULL + """ % {"table": env[model]._table, "field": field}, + (model,) + )