Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
034cc97
Split model in abstract & concrete.
yajo Nov 6, 2015
72edee6
Extend model example.
yajo Mar 3, 2016
b4246b5
Tenames and typos.
yajo Mar 3, 2016
d2d43cd
No default value for storage.
yajo Mar 3, 2016
b9efd99
Do not require a name.
yajo Mar 3, 2016
b661e8b
Add sequence to images.
yajo Mar 3, 2016
f18c876
Reduce methods.
yajo Mar 3, 2016
f56814c
Cache images' URL.
yajo Mar 3, 2016
5354e85
Remove wrong copyright.
yajo Mar 3, 2016
e10cf36
Add known issues section.
yajo Mar 9, 2016
ca84b3d
Fix bug that made previous images to disappear.
yajo Mar 11, 2016
3d52d71
[MIG][9.0] base_multi_image
atchuthan Mar 30, 2016
7e17b85
[FIX] base_multi_image - New storage backend 'Filestore' to link an e…
sebalix Jun 29, 2016
dfe256d
[IMP] base_multi_image - Select only images as attachments
sebalix Jun 29, 2016
ebece80
[FIX] base_multi_image - Clean up + updated comments
sebalix Jun 30, 2016
2f4a874
[FIX] base_multi_image - Make validation error messages translatable
sebalix Jun 30, 2016
39131a3
[FIX] base_multi_image - Fix method name
sebalix Jun 30, 2016
e87a64f
Use v9 ormcache.
yajo Jul 20, 2016
8270c38
Add a reference field to get to the owner with 1 click.
yajo Jul 20, 2016
93f8b14
Add uninstall hook to avoid garbage images hanging around after submo…
yajo Jul 20, 2016
76dd615
Raise version.
yajo Jul 20, 2016
764630f
Add ondelete attribute, required for Many2one fields in v9.
yajo Jul 21, 2016
d59988a
[FIX] base_multi_image: Correct README instructions
pedrobaeza Jul 29, 2016
3f83ff2
[FIX] remove en.po that was erroneously created by transbot
sbidoul Aug 15, 2016
b0662d0
[FIX] adapt to changes in binary attachment branch
hbrunn Sep 15, 2016
63ae28a
[MIG] Make modules uninstallable
pedrobaeza Oct 6, 2016
fee431c
[MIG] Rename manifest files
pedrobaeza Oct 6, 2016
f9125e7
base_multi_image: Fix hook example * Add uninstall hook import to Rea…
lasley Nov 4, 2016
04b6ef8
[MIG] base_multi_image: Upgrade to v10
lasley Nov 10, 2016
047e3a7
[FIX] base_multi_image: Adhere to image delete bypass
lasley Nov 30, 2016
2f4751b
[FIX] base_multi_image: Use env in uninstall_hook (#677)
lasley Jan 16, 2017
8b39319
[10.0][FIX] base_multi_image: Uninstall hook (#709)
lasley Jan 23, 2017
4df296e
[10.0] [IMP] base_multi_image: improve uninstall hook (#1286)
FFernandez-PlanetaTIC Jun 22, 2018
986cc3c
[MIG] base_multi_image: Migration to 12.0
Nov 8, 2019
1ab1c38
[FIX]base_multi_image: Fix image url in locales with thousand separator.
MPoch-PlanetaTIC Jun 11, 2020
dbb3ff1
[MIG][13.0]base_multi_image: Migrated to v13
javierjcf Sep 9, 2020
9cd6521
[IMP] base_multi_image: black, isort, prettier
hailangvn May 16, 2021
1046321
[MIG] base_multi_image: Migration to 14.0
hailangvn May 21, 2021
0b88e35
Translated using Weblate (Spanish (Argentina))
ibuioli Mar 19, 2022
476fdfc
[IMP] base_multi_image: black, isort
flachica May 6, 2022
265b006
[15.0][MIG] base_multi_image
flachica May 6, 2022
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
176 changes: 176 additions & 0 deletions base_multi_image/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
====================
Multiple images base
====================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |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/14.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-14-0/server-tools-14-0-base_multi_image
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/149/14.0
:alt: Try me on Runbot

|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 smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_multi_image%0Aversion:%2014.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>

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/14.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": "15.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