Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions base_multi_image/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion base_multi_image/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
26 changes: 25 additions & 1 deletion base_multi_image/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
)
Expand All @@ -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
Expand Down
25 changes: 23 additions & 2 deletions base_multi_image/models/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions base_multi_image/views/image_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<field name="show_technical" invisible="True"/>
<field name="owner_model"/>
<field name="owner_id"/>
<field name="owner_ref_id"/>
<field name="sequence"/>
</group>
<group string="Name">
Expand Down