Skip to content

Commit cf2f689

Browse files
[MIG] base_multi_image: Migration to 18.0
1 parent ed768d6 commit cf2f689

81 files changed

Lines changed: 8707 additions & 3771 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

base_multi_image/README.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Multiple images base
77
!! This file is generated by oca-gen-addon-readme !!
88
!! changes will be overwritten. !!
99
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-
!! source digest: sha256:3a6e6f20a1f94157e9fafd8edf8c8ce1603661bb638930571085941fa47a3ed0
10+
!! source digest: sha256:900ccdb66c4ef3d2824a8975ef824a3b94637d2653b157c7826e0d69f17bd3cf
1111
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1212
1313
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
@@ -73,7 +73,7 @@ To develop a module based on this one:
7373

7474
# If you need this, you will need ``pre_init_hook_for_submodules`` and
7575
``uninstall_hook_for_submodules`` as detailed below.
76-
old_image_field = fields.Binary(related="image_1920", store=False)
76+
old_image_field = fields.Binary(related="image_main", store=False)
7777

7878
- Somewhere in the owner view, add:
7979

@@ -90,20 +90,27 @@ To develop a module based on this one:
9090

9191
- If the model you are extending already had an image field, and you
9292
want to trick Odoo to make those images to multi-image mode, you will
93-
need to make use of the provided ~.hooks.post_init_hook_for_submodules
93+
need to make use of the provided ~.hooks.pre_init_hook_for_submodules
9494
and ~.hooks.uninstall_hook_for_submodules, like the
9595
``product_multi_image`` module does:
9696

9797
::
9898

9999
try:
100100
from odoo.addons.base_multi_image.hooks import (
101+
pre_init_hook_for_submodules,
101102
uninstall_hook_for_submodules,
102103
)
103104
except ImportError:
104105
pass
105106

106107

108+
def pre_init_hook(cr):
109+
"""Transform single into multi images."""
110+
pre_init_hook_for_submodules(cr, "product.template", "image")
111+
pre_init_hook_for_submodules(cr, "product.product", "image_variant")
112+
113+
107114
def uninstall_hook(cr, registry):
108115
"""Remove multi images for models that no longer use them."""
109116
uninstall_hook_for_submodules(cr, registry, "product.template")
@@ -143,7 +150,7 @@ Authors
143150
* S.L.
144151
* Sodexis
145152
* LasLabs
146-
* OpenFire
153+
* Omal Bastin (O4ODOO)
147154

148155
Contributors
149156
------------
@@ -158,6 +165,10 @@ Contributors
158165

159166
- Fernando La Chica <fernandolachica@gmail.com>
160167

168+
- `Heliconia Solutions Pvt. Ltd. <https://www.heliconia.io>`__
169+
170+
- Bhavesh Heliconia
171+
161172
Other credits
162173
-------------
163174

base_multi_image/__manifest__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
# Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
33
# © 2015 Antiun Ingeniería S.L. - Jairo Llopis
44
# © 2016 Sodexis
5+
# © 2024 Omal bastin <omalbastin@gmail.com>
56
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
67

78
{
89
"name": "Multiple images base",
910
"summary": "Allow multiple images for database objects",
10-
"version": "16.0.1.0.1",
11+
"version": "18.0.1.0.0",
1112
"author": "Tecnativa, "
1213
"Antiun Ingeniería, S.L., Sodexis, "
13-
"LasLabs, OpenFire, "
14+
"LasLabs, "
15+
"Omal Bastin (O4ODOO), "
1416
"Odoo Community Association (OCA)",
1517
"license": "AGPL-3",
1618
"website": "https://github.com/OCA/server-tools",

base_multi_image/hooks.py

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33

44
import logging
55

6-
from odoo import SUPERUSER_ID, api
7-
86
_logger = logging.getLogger(__name__)
97

108

11-
def post_init_hook_for_submodules(cr, model, field):
9+
def pre_init_hook_for_submodules(env, model, field):
1210
"""Moves images from single to multi mode.
1311
14-
Feel free to use this as a ``post_init_hook`` for submodules.
15-
16-
:param odoo.sql_db.Cursor cr:
17-
Database cursor.
12+
Feel free to use this as a ``pre_init_hook`` for submodules.
1813
1914
:param str model:
2015
Model name, like ``product.template``.
@@ -23,48 +18,48 @@ def post_init_hook_for_submodules(cr, model, field):
2318
Binary field that had the images in that :param:`model`, like
2419
``image``.
2520
"""
26-
env = api.Environment(cr, SUPERUSER_ID, {})
27-
table = env[model]._table
28-
image_obj = env["base_multi_image.image"]
21+
cr = env.cr
2922
with cr.savepoint():
23+
table = env[model]._table
3024
column_exists = table_has_column(cr, table, field)
25+
# fields.Binary(), extract the binary content directly from the table
3126
if column_exists:
32-
cr.execute("SELECT id FROM %(table)s WHERE %(field)s IS NOT NULL") # pylint: disable=sql-injection
27+
extract_query = f"""
28+
SELECT id, '{model}', '{model},' || id, 'db', {field}
29+
FROM {table}
30+
WHERE {field} IS NOT NULL
31+
"""
32+
image_field = "file_db_store"
33+
# fields.Binary(attachment=True), get the ir_attachment record ID
3334
else:
34-
cr.execute(
35-
"""
36-
SELECT res_id
37-
FROM ir_attachment
38-
WHERE
39-
res_field=%(field)s
40-
AND res_model=%(model)s
41-
""",
42-
{
43-
"field": field,
44-
"model": model,
45-
},
46-
)
47-
record_ids = [row[0] for row in cr.fetchall()]
48-
for record in env[model].browse(record_ids):
49-
image_obj.create(
50-
{
51-
"owner_id": record.id,
52-
"owner_model": model,
53-
"owner_ref_id": f"{model},{record.id}",
54-
"image_1920": record[field],
55-
},
56-
)
57-
58-
59-
def uninstall_hook_for_submodules(cr, model, field=None):
60-
"""Moves images from multi to single mode and remove multi-images for a
61-
given model.
35+
extract_query = f"""
36+
SELECT
37+
res_id,
38+
res_model,
39+
CONCAT_WS(',', res_model, res_id),
40+
'attachment',
41+
id
42+
FROM ir_attachment
43+
WHERE res_field='{field}' AND res_model='{model}'
44+
"""
45+
image_field = "attachment_id"
46+
cr.execute( # pylint: disable=sql-injection
47+
f"""
48+
INSERT INTO base_multi_image_image (
49+
owner_id,
50+
owner_model,
51+
owner_ref_id,
52+
storage,
53+
{image_field}
54+
)
55+
{extract_query}
56+
"""
57+
)
6258

63-
:param odoo.sql_db.Cursor cr:
64-
Database cursor.
6559

66-
:param odoo.modules.registry.RegistryManager registry:
67-
Database registry, using v7 api.
60+
def uninstall_hook_for_submodules(env, model, field=None):
61+
"""Moves images from multi to single mode and remove multi-images for a
62+
given model.
6863
6964
:param str model:
7065
Model technical name, like "res.partner". All multi-images for that
@@ -73,49 +68,48 @@ def uninstall_hook_for_submodules(cr, model, field=None):
7368
:param str field:
7469
Binary field that had the images in that :param:`model`, like
7570
``image``.
71+
7672
"""
77-
env = api.Environment(cr, SUPERUSER_ID, {})
73+
cr = env.cr
7874
with cr.savepoint():
79-
image_obj = env["base_multi_image.image"]
80-
images = image_obj.search([("owner_model", "=", model)], order="sequence, id")
75+
Image = env["base_multi_image.image"]
76+
images = Image.search([("owner_model", "=", model)], order="sequence, id")
8177
if images and field:
8278
main_images = {}
8379
for image in images:
8480
if image.owner_id not in main_images:
8581
main_images[image.owner_id] = image
8682
main_images = main_images.values()
87-
model_obj = env[model]
88-
field_field = model_obj._fields[field]
83+
Model = env[model]
84+
Field = Model._fields[field]
8985

9086
# fields.Binary(), save the binary content directly to the table
91-
if not field_field.attachment:
87+
if not Field.attachment:
9288
save_directly_to_table(
9389
cr,
94-
model_obj,
90+
Model,
9591
field,
96-
field_field,
92+
Field,
9793
main_images,
9894
)
9995
# fields.Binary(attachment=True), save the ir_attachment record ID
100-
if field and field_field.attachment:
96+
if Field.attachment:
10197
for main_image in main_images:
102-
owner = model_obj.browse(main_image.owner_id)
103-
field_field.write(owner, main_image.image_1920)
98+
owner = Model.browse(main_image.owner_id)
99+
Field.write(owner, main_image.image_1920)
104100
images.unlink()
105101

106102

107103
def save_directly_to_table(cr, Model, field, Field, main_images):
108104
fields = []
109105
if field and not Field.attachment:
110106
fields.append(field + " = " + "%(image)s")
107+
111108
query = """
112109
UPDATE {table}
113110
SET {fields}
114-
WHERE id = %(id)s
115-
""".format(
116-
table=Model._table,
117-
fields=", ".join(fields),
118-
)
111+
WHERE id = %%(id)s
112+
""".format(table=Model._table, fields=", ".join(fields))
119113
for main_image in main_images:
120114
params = {"id": main_image.owner_id}
121115
if field and not Field.attachment:

0 commit comments

Comments
 (0)