Skip to content

Commit

Permalink
[MIG] base_multi_image
Browse files Browse the repository at this point in the history
  • Loading branch information
IJOL committed Aug 9, 2024
1 parent 5577e14 commit de94425
Show file tree
Hide file tree
Showing 91 changed files with 21,228 additions and 0 deletions.
182 changes: 182 additions & 0 deletions base_multi_image/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
====================
Multiple images base
====================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:900ccdb66c4ef3d2824a8975ef824a3b94637d2653b157c7826e0d69f17bd3cf
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/16.0/base_multi_image
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-0-base_multi_image
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module extends the functionality of any model to support multiple images
(a gallery) attached to it and allow you to manage them.

**Table of contents**

.. contents::
:local:

Installation
============

This module adds abstract models to work on. Its sole purpose is to serve as
base for other modules that implement galleries, so if you install this one
manually you will notice no change. You should install any other module based
on this one and this will get installed automatically.

Configuration
=============

To manage all stored images, you need to:

* Go to *Settings > Technical > Multi images*.

... but you probably prefer to manage them from the forms supplied by
submodules that inherit this behavior.

Development
===========

To develop a module based on this one:

* See module ``product_multi_image`` as an example.

* You have to inherit model ``base_multi_image.owner`` to the model that needs
the gallery::

class MyOwner(models.Model):
_name = "my.model.name"
_inherit = ["my.model.name", "base_multi_image.owner"]

# If you need this, you will need ``pre_init_hook_for_submodules`` and
``uninstall_hook_for_submodules`` as detailed below.
old_image_field = fields.Binary(related="image_main", store=False)

* Somewhere in the owner view, add::

<field
name="image_ids"
nolabel="1"
context="{
'default_owner_model': 'my.model.name',
'default_owner_id': id,
}"
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 `~.hooks.pre_init_hook_for_submodules` and
`~.hooks.uninstall_hook_for_submodules`, like the
``product_multi_image`` module does::

try:
from odoo.addons.base_multi_image.hooks import (
pre_init_hook_for_submodules,
uninstall_hook_for_submodules,
)
except ImportError:
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/10.0

Known issues / Roadmap
======================

* *OS file* storage mode for images is meant to provide a path where Odoo has
read access and the image is already found, **not for making the module store
images there**. It would be nice to add that feature though.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_multi_image%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Tecnativa
* Antiun Ingeniería
* S.L.
* Sodexis
* LasLabs

Contributors
~~~~~~~~~~~~

* Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
* Rafael Blasco <rafabn@antiun.com>
* Jairo Llopis <yajo.sk8@gmail.com>
* Sodexis <dev@sodexis.com>
* Dave Lasley <dave@laslabs.com>
* Shepilov Vladislav <shepilov.v@protonmail.com>
* `Greenice <https://www.greenice.com>`_:

* Fernando La Chica <fernandolachica@gmail.com>

Other credits
~~~~~~~~~~~~~

Original implementation
-----------------------
This module is inspired in previous module *product_images* from OpenLabs
and Akretion.

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/16.0/base_multi_image>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
6 changes: 6 additions & 0 deletions base_multi_image/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# © 2014 Serv. Tecnol. Avanzados (http://www.serviciosbaeza.com)
# Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
# © 2015 Antiun Ingeniería S.L. - Jairo Llopis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import models
28 changes: 28 additions & 0 deletions base_multi_image/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# © 2014 Serv. Tecnol. Avanzados (http://www.serviciosbaeza.com)
# Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
# © 2015 Antiun Ingeniería S.L. - Jairo Llopis
# © 2016 Sodexis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Multiple images base",
"summary": "Allow multiple images for database objects",
"version": "16.0.1.0.0",
"author": "Tecnativa, "
"Antiun Ingeniería, S.L., Sodexis, "
"LasLabs, "
"Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/server-tools",
"category": "Tools",
"depends": ["base"],
"installable": True,
"data": [
"security/ir.model.access.csv",
"views/image_view.xml",
],
"images": [
"images/form.png",
"images/kanban.png",
],
}
179 changes: 179 additions & 0 deletions base_multi_image/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# © 2016 Antiun Ingeniería S.L. - Jairo Llopis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging

from odoo import SUPERUSER_ID, api

_logger = logging.getLogger(__name__)


def pre_init_hook_for_submodules(cr, model, field):
"""Moves images from single to multi mode.
Feel free to use this as a ``pre_init_hook`` for submodules.
:param str model:
Model name, like ``product.template``.
:param str field:
Binary field that had the images in that :param:`model`, like
``image``.
"""
env = api.Environment(cr, SUPERUSER_ID, {})
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, '%(model)s', '%(model)s,' || id, 'db', %(field)s
FROM %(table)s
WHERE %(field)s IS NOT NULL
""" % {
"table": table,
"field": field,
"model": model,
}
image_field = "file_db_store"
# fields.Binary(attachment=True), get the ir_attachment record ID
else:
extract_query = """
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,
}
image_field = "attachment_id"
cr.execute( # pylint: disable=sql-injection
"""
INSERT INTO base_multi_image_image (
owner_id,
owner_model,
owner_ref_id,
storage,
%s
)
%s
"""
% (image_field, extract_query)
)


def uninstall_hook_for_submodules(
cr, registry, model, field=None, field_medium=None, field_small=None
):
"""Moves images from multi to single mode and remove multi-images for a
given model.
:param odoo.sql_db.Cursor cr:
Database cursor.
:param odoo.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
:param str field:
Binary field that had the images in that :param:`model`, like
``image``.
:param str field_medium:
Binary field that had the medium-sized images in that :param:`model`,
like ``image_medium``.
:param str field_small:
Binary field that had the small-sized images in that :param:`model`,
like ``image_small``.
"""
env = api.Environment(cr, SUPERUSER_ID, {})
with cr.savepoint():
Image = env["base_multi_image.image"]
images = Image.search([("owner_model", "=", model)], order="sequence, id")
if images and (field or field_medium or field_small):
main_images = {}
for image in images:
if image.owner_id not in main_images:
main_images[image.owner_id] = image
main_images = main_images.values()
Model = env[model]
Field = field and Model._fields[field]
FieldMedium = field_medium and Model._fields[field_medium]
FieldSmall = field_small and Model._fields[field_small]

# fields.Binary(), save the binary content directly to the table
if (
(field and not Field.attachment)
or (field_medium and not FieldMedium.attachment)
or (field_small and not FieldSmall.attachment)
):
save_directly_to_table(
cr,
Model,
(field, field_medium, field_small),
(Field, FieldMedium, FieldSmall),
main_images,
)
# fields.Binary(attachment=True), save the ir_attachment record ID
if (
(field and Field.attachment)
or (field_medium and FieldMedium.attachment)
or (field_small and FieldSmall.attachment)
):
for main_image in main_images:
owner = Model.browse(main_image.owner_id)
if field and Field.attachment:
Field.write(owner, main_image.image_main)
if field_medium and FieldMedium.attachment:
FieldMedium.write(owner, main_image.image_medium)
if field_small and FieldSmall.attachment:
FieldSmall.write(owner, main_image.image_small)
images.unlink()


def save_directly_to_table(cr, Model, fields, Fields, main_images):
field, field_medium, field_small = fields
Field, FieldMedium, FieldSmall = Fields
fields = []
if field and not Field.attachment:
fields.append(field + " = " + "%(image)s")
if field_medium and not FieldMedium.attachment:
fields.append(field_medium + " = " + "%(image_medium)s")
if field_small and not FieldSmall.attachment:
fields.append(field_small + " = " + "%(image_small)s")
query = """
UPDATE %(table)s
SET %(fields)s
WHERE id = %%(id)s
""" % {
"table": Model._table,
"fields": ", ".join(fields),
}
for main_image in main_images:
params = {"id": main_image.owner_id}
if field and not Field.attachment:
params["image"] = main_image.image_main
if field_medium and not FieldMedium.attachment:
params["image_medium"] = main_image.image_medium
if field_small and not FieldSmall.attachment:
params["image_small"] = main_image.image_small
cr.execute(query, params) # pylint: disable=sql-injection


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())
Loading

0 comments on commit de94425

Please sign in to comment.