-
-
Notifications
You must be signed in to change notification settings - Fork 286
/
hooks.py
84 lines (74 loc) · 2.46 KB
/
hooks.py
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
# Copyright 2015-2016 Pedro M. Baeza <[email protected]>
# Copyright 2017 LasLabs Inc.
# License LGPL-3 - See http://www.gnu.org/licenses/lgpl-3.0.html
from odoo import SUPERUSER_ID, api
__all__ = [
"post_init_hook",
"uninstall_hook",
]
def set_security_rule(env, rule_ref):
"""Set the condition for multi-company in the security rule.
:param: env: Environment
:param: rule_ref: XML-ID of the security rule to change.
"""
rule = env.ref(rule_ref)
if not rule: # safeguard if it's deleted
return
rule.write(
{
"active": True,
"domain_force": (
"['|', ('company_ids', '=', False), ('company_ids', "
"'in', company_ids)]"
),
}
)
def post_init_hook(cr, rule_ref, model_name):
"""Set the `domain_force` and default `company_ids` to `company_id`.
Args:
cr (Cursor): Database cursor to use for operation.
rule_ref (string): XML ID of security rule to write the
`domain_force` from.
model_name (string): Name of Odoo model object to search for
existing records.
"""
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
set_security_rule(env, rule_ref)
# Copy company values
model = env[model_name]
table_name = model._fields["company_ids"].relation
column1 = model._fields["company_ids"].column1
column2 = model._fields["company_ids"].column2
SQL = """
INSERT INTO {}
({}, {})
SELECT id, company_id FROM {} WHERE company_id IS NOT NULL
ON CONFLICT DO NOTHING
""".format(
table_name,
column1,
column2,
model._table,
)
env.cr.execute(SQL)
def uninstall_hook(cr, rule_ref):
"""Restore product rule to base value.
Args:
cr (Cursor): Database cursor to use for operation.
rule_ref (string): XML ID of security rule to remove the
`domain_force` from.
"""
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
# Change access rule
rule = env.ref(rule_ref)
rule.write(
{
"active": False,
"domain_force": (
" ['|', ('company_id', '=', user.company_id.id),"
" ('company_id', '=', False)]"
),
}
)