Browse Source

Fix bug that made previous images to disappear.

See https://github.com/OCA/product-attribute/pull/135#issuecomment-191358505.
pull/402/head
Jairo Llopis 9 years ago
parent
commit
61b401beb8
  1. 14
      base_multi_image/README.rst
  2. 2
      base_multi_image/__openerp__.py
  3. 37
      base_multi_image/hooks.py

14
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

2
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)",

37
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,)
)
Loading…
Cancel
Save