-
-
Notifications
You must be signed in to change notification settings - Fork 824
Expand file tree
/
Copy pathpost-migration.py
More file actions
142 lines (130 loc) · 5.82 KB
/
Copy pathpost-migration.py
File metadata and controls
142 lines (130 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright 2020 ForgeFlow <http://www.forgeflow.com>
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade
from odoo.tools import sql
import logging
_logger = logging.getLogger(__name__)
def convert_image_attachments(env):
mapping = {
'product.product': "image_variant",
'product.template': "image",
}
attachment_model = env['ir.attachment']
for model, field in mapping.items():
Model = env[model]
attachment_ids = attachment_model.search([
('res_model', '=', model),
('res_field', '=', field),
('res_id', '!=', False),
]).ids
# a MemoryError can happen if too many attachments are loaded in
# memory. therefore, we only load one record at the time and flush the
# cache at each iteration.
for attachment_id in attachment_ids:
# load attachments one by one to avoid prefetching
attachment = attachment_model.browse(attachment_id)
try:
Model.browse(attachment.res_id).image_1920 = attachment.datas
except Exception as e:
_logger.error(
"Error while recovering %s>%s for %s: %s",
model,
field,
attachment.res_id,
repr(e),
)
# flush and clear cache to avoid to fill up memory and force the
# computation of the computed fields (smaller size images)
Model.flush()
env.cache.invalidate()
@openupgrade.logging()
def empty_template_pricelist_company(env):
"""On v13, there's no default company associated with the template nor the
pricelist, on contrary than on v12. We need to empty the company_id field in
case of having only one company for not having problems later when creating
new pricelists and not being able to select old products due to the
difference on the company_id field (on a pricelist without company, you can
only select templates without company, and vice versa, but not a mix of
both).
We need to empty the company of pricelists as well.
If there are more than one company in the DB, then everything is
preserved as it is.
"""
env.cr.execute("SELECT COUNT(*) FROM res_company")
if env.cr.fetchone()[0] == 1:
openupgrade.logged_query(
env.cr, "UPDATE product_template SET company_id = NULL WHERE company_id is NOT NULL",
)
openupgrade.logged_query(
env.cr, "UPDATE product_pricelist SET company_id = NULL WHERE company_id is NOT NULL",
)
def adapt_pricelist_settings(env):
"""In v12, if pricelist setting was marked it implied group_sale_pricelist,
and if the setting was 'percentage' it also implied group_product_pricelist.
In v13, if pricelist setting was marked it implies group_product_pricelist,
and if the setting is 'advanced', then it has also group_sale_pricelist.
And about the options, 'percentage' has become 'basic' and 'formula' has become 'advanced'."""
group_user = env.ref("base.group_user").sudo()
group_product_pricelist = env.ref("product.group_product_pricelist").sudo()
group_sale_pricelist = env.ref("product.group_sale_pricelist").sudo()
ir_config_param = env["ir.config_parameter"].sudo()
sale_installed = openupgrade.table_exists(env.cr, "sale_order")
pos_installed = openupgrade.table_exists(env.cr, "pos_order")
sale_pricelist_setting = ir_config_param.get_param(
"sale.sale_pricelist_setting", ""
)
pos_pricelist_setting = ir_config_param.get_param(
"point_of_sale.pos_pricelist_setting", ""
)
# multi_sales_price = sale_pricelist_setting and sale_pricelist_setting in ['percentage', 'formula']
pos_sales_price = ir_config_param.get_param("point_of_sale.pos_sales_price")
with env.norecompute():
if (
(
group_product_pricelist in group_user.implied_ids
and group_sale_pricelist in group_user.implied_ids
and not sale_installed
and not pos_installed
)
or (
sale_installed
and sale_pricelist_setting == "percentage"
and pos_pricelist_setting != "formula"
)
or (
pos_installed
and pos_sales_price
and pos_pricelist_setting == "percentage"
and sale_pricelist_setting != "formula"
)
):
# 'basic' case
group_user.write({"implied_ids": [(3, group_sale_pricelist.id)]})
group_sale_pricelist.write({"users": [(5,)]})
ir_config_param.set_param("product.product_pricelist_setting", "basic")
elif (
(
group_product_pricelist not in group_user.implied_ids
and group_sale_pricelist in group_user.implied_ids
and not sale_installed
and not pos_installed
)
or (sale_installed and sale_pricelist_setting == "formula")
or (
pos_installed and pos_sales_price and pos_pricelist_setting == "formula"
)
):
# 'advanced' case
group_user.write({"implied_ids": [(4, group_product_pricelist.id)]})
ir_config_param.set_param("product.product_pricelist_setting", "advanced")
@openupgrade.migrate()
def migrate(env, version):
openupgrade.load_data(
env.cr, "product", "migrations/13.0.1.2/noupdate_changes.xml")
convert_image_attachments(env)
empty_template_pricelist_company(env)
adapt_pricelist_settings(env)
sql.drop_index(
env.cr, 'product_template_attribute_value_ou_migration_idx',
"product_template_attribute_value")