Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.0][MIG] base_multi_image: Migration to 17.0 #12

Merged
merged 59 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
d8f10e2
Split model in abstract & concrete.
yajo Nov 6, 2015
792da07
Extend model example.
yajo Mar 3, 2016
65269eb
Tenames and typos.
yajo Mar 3, 2016
c0c7a6b
No default value for storage.
yajo Mar 3, 2016
219d526
Do not require a name.
yajo Mar 3, 2016
e82911c
Add sequence to images.
yajo Mar 3, 2016
0fac767
Reduce methods.
yajo Mar 3, 2016
a6cf136
Cache images' URL.
yajo Mar 3, 2016
db9a1c6
Remove wrong copyright.
yajo Mar 3, 2016
9882b24
Add known issues section.
yajo Mar 9, 2016
d00da97
Fix bug that made previous images to disappear.
yajo Mar 11, 2016
c361a50
[MIG][9.0] base_multi_image
atchuthan Mar 30, 2016
a712268
[FIX] base_multi_image - New storage backend 'Filestore' to link an e…
sebalix Jun 29, 2016
e59c7af
[IMP] base_multi_image - Select only images as attachments
sebalix Jun 29, 2016
0714171
[FIX] base_multi_image - Clean up + updated comments
sebalix Jun 30, 2016
18019fc
[FIX] base_multi_image - Make validation error messages translatable
sebalix Jun 30, 2016
2ec9bb8
[FIX] base_multi_image - Fix method name
sebalix Jun 30, 2016
f20555d
Use v9 ormcache.
yajo Jul 20, 2016
30b769c
Add a reference field to get to the owner with 1 click.
yajo Jul 20, 2016
cbeb359
Add uninstall hook to avoid garbage images hanging around after submo…
yajo Jul 20, 2016
489f34d
Raise version.
yajo Jul 20, 2016
25b4794
Add ondelete attribute, required for Many2one fields in v9.
yajo Jul 21, 2016
d051cbe
[FIX] base_multi_image: Correct README instructions
pedrobaeza Jul 29, 2016
638628c
[FIX] remove en.po that was erroneously created by transbot
sbidoul Aug 15, 2016
f92fe51
[FIX] adapt to changes in binary attachment branch
hbrunn Sep 15, 2016
4fb7547
[MIG] Make modules uninstallable
pedrobaeza Oct 6, 2016
7724c74
[MIG] Rename manifest files
pedrobaeza Oct 6, 2016
3ba334f
base_multi_image: Fix hook example * Add uninstall hook import to Rea…
lasley Nov 4, 2016
b7f8364
[MIG] base_multi_image: Upgrade to v10
lasley Nov 10, 2016
a5dbd5a
[FIX] base_multi_image: Adhere to image delete bypass
lasley Nov 30, 2016
e1719f6
[FIX] base_multi_image: Use env in uninstall_hook (#677)
lasley Jan 16, 2017
7343516
[10.0][FIX] base_multi_image: Uninstall hook (#709)
lasley Jan 23, 2017
88cd9cb
[10.0] [IMP] base_multi_image: improve uninstall hook (#1286)
FFernandez-PlanetaTIC Jun 22, 2018
d202c9f
[MIG] base_multi_image: Migration to 12.0
Nov 8, 2019
c90841d
[FIX]base_multi_image: Fix image url in locales with thousand separator.
MPoch-PlanetaTIC Jun 11, 2020
7724ba5
[MIG][13.0]base_multi_image: Migrated to v13
javierjcf Sep 9, 2020
3e67a7f
[IMP] base_multi_image: black, isort, prettier
hailangvn May 16, 2021
55100e7
[MIG] base_multi_image: Migration to 14.0
hailangvn May 21, 2021
a6b716f
Translated using Weblate (Spanish (Argentina))
ibuioli Mar 19, 2022
d352b58
[15.0][MIG] base_multi_image
flachica May 6, 2022
04ad6a8
[UPD] Update base_multi_image.pot
Aug 8, 2022
f3181b6
[UPD] README.rst
OCA-git-bot Aug 8, 2022
a6df65a
Translated using Weblate (Italian)
mymage Mar 23, 2023
9b0a69a
Translated using Weblate (Slovenian)
sysadminmatmoz Mar 31, 2023
990d351
Translated using Weblate (Italian)
mymage Apr 10, 2023
06cbdb5
Translated using Weblate (Italian)
mymage Apr 17, 2023
42c7a9c
[UPD] README.rst
OCA-git-bot Sep 3, 2023
de57401
Update translation files
weblate Oct 9, 2023
9c40986
Translated using Weblate (Spanish)
Ivorra78 Oct 10, 2023
1904fce
Translated using Weblate (Spanish (Argentina))
ibuioli Oct 27, 2023
18698b2
Translated using Weblate (Italian)
mymage Dec 29, 2023
b3bd158
[MIG] base_multi_image: Migration to 16.0
IJOL Aug 9, 2024
870d466
[UPD] base_multi_image removed unused params, revamped post_init_hook…
IJOL Aug 9, 2024
9774790
[UPD] Update base_multi_image.pot
Sep 7, 2024
5b70e47
Update translation files
weblate Sep 7, 2024
a85061b
Translated using Weblate (Italian)
mymage Sep 9, 2024
1ec17dc
[17.0][MIG] base_multi_image: Migration to 17.0
psugne Oct 9, 2024
3db9d25
Merge remote-tracking branch 'origin/17.0' into 17.0-mig-base_multi_i…
psugne Oct 10, 2024
46088c6
[IMP] pre-commit adjustments
psugne Oct 10, 2024
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
185 changes: 185 additions & 0 deletions base_multi_image/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
====================
Multiple images base
====================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:60ac00ce7f687070232e6497bd0c97a20de9bcf45f9263960a3529ab63865636
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |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/17.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-17-0/server-tools-17-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=17.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_1920", 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.post_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 (
uninstall_hook_for_submodules,
)
except ImportError:
pass


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

|Try me on Runbot|

.. |Try me on Runbot| image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
: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:%2017.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
* OpenFire

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>
- Ugnė Sinkevičienė <ugne@versada.eu>

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/17.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": "17.0.1.0.0",
"author": "Tecnativa, "
"Antiun Ingeniería, S.L., Sodexis, "
"LasLabs, OpenFire, "
"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",
],
}
130 changes: 130 additions & 0 deletions base_multi_image/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# © 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 post_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.

:param odoo.sql_db.Cursor cr:
Database cursor.

: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, {})
table = env[model]._table
image_obj = env["base_multi_image.image"]
with cr.savepoint():
column_exists = table_has_column(cr, table, field)
if column_exists:
cr.execute("SELECT id FROM %(table)s WHERE %(field)s IS NOT NULL")
else:
cr.execute(
"""
SELECT res_id
FROM ir_attachment
WHERE
res_field=%(field)s
AND res_model=%(model)s
""",
{
"field": field,
"model": model,
},
)
record_ids = [row[0] for row in cr.fetchall()]
for record in env[model].browse(record_ids):
image_obj.create(
{
"owner_id": record.id,
"owner_model": model,
"owner_ref_id": f"{model},{record.id}",
"image_1920": record[field],
},
)


def uninstall_hook_for_submodules(cr, model, field=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``.
"""
env = api.Environment(cr, SUPERUSER_ID, {})
with cr.savepoint():
image_obj = env["base_multi_image.image"]
images = image_obj.search([("owner_model", "=", model)], order="sequence, id")
if images and field:
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_obj = env[model]
field_field = model_obj._fields[field]

# fields.Binary(), save the binary content directly to the table
if not field_field.attachment:
save_directly_to_table(
cr,
model_obj,
field,
field_field,
main_images,
)
# fields.Binary(attachment=True), save the ir_attachment record ID
if field and field_field.attachment:
for main_image in main_images:
owner = model_obj.browse(main_image.owner_id)
field_field.write(owner, main_image.image_1920)
images.unlink()


def save_directly_to_table(cr, Model, field, Field, main_images):
fields = []
if field and not Field.attachment:
fields.append(field + " = " + "%(image)s")
query = """
UPDATE {table}
SET {fields}
WHERE id = %(id)s
""".format(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_1920
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
Loading