Skip to content

Commit

Permalink
[FIX] delete_record_translations: adapt to Odoo v16
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow committed Jun 9, 2023
1 parent a5b702b commit cec7deb
Showing 1 changed file with 73 additions and 14 deletions.
87 changes: 73 additions & 14 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -2592,7 +2592,7 @@ def delete_record_translations(cr, module, xml_ids):
:param xml_ids: a tuple or list of xml record IDs
"""
if not isinstance(xml_ids, (list, tuple)):
do_raise("XML IDs %s must be a tuple or list!" % (xml_ids))
do_raise("XML IDs %s must be a tuple or list!" % (xml_ids,))

cr.execute(
"""
Expand All @@ -2606,19 +2606,78 @@ def delete_record_translations(cr, module, xml_ids):
),
)
for row in cr.fetchall():
query = """
DELETE FROM ir_translation
WHERE module = %s AND name LIKE %s AND res_id = %s;
"""
logged_query(
cr,
query,
(
module,
row[0] + ",%",
row[1],
),
)
model = row[0]
record_id = row[1]
if version_info[0] < 16:
query = """
DELETE FROM ir_translation
WHERE module = %s AND name LIKE %s AND res_id = %s;
"""
logged_query(
cr,
query,
(
module,
model + ",%",
record_id,
),
)
else:
from psycopg2.extras import Json

table = model.replace(".", "_")
cr.execute(
"""
SELECT isc.column_name
FROM information_schema.columns isc
JOIN ir_model_fields imf ON (
imf.name = isc.column_name AND imf.model = %s)
JOIN ir_model_data imd ON (
imd.model = 'ir.model.fields' AND imd.res_id = imf.id)
WHERE isc.table_name = % AND imd.module = %s
AND imf.translate""",
(model, table, module),
)
list_columns = [x[0] for x in cr.fetchall()]
if not list_columns:
continue
columns = ", ".join(list_columns)
query = """
SELECT {columns}
FROM {table}
WHERE id = %s""".format(
table=table,
columns=columns,
)
cr.execute(
query,
(record_id,),
)
list_translations = [x[0] for x in cr.fetchall()]
new_translations = []
for translations in list_translations:
field_translations = translations or {}
if "en_US" in field_translations:
clean_value = {"en_US": field_translations["en_US"]}
else:
clean_value = {}
new_translations += [Json(clean_value)]
query = """
UPDATE %s
SET (%s) = %s
WHERE id = %s;
""".format(
table=table,
columns=columns,
)
logged_query(
cr,
query,
(
tuple(new_translations),
record_id,
),
)


# flake8: noqa: C901
Expand Down

0 comments on commit cec7deb

Please sign in to comment.