forked from OCA/multi-company
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.py
More file actions
63 lines (55 loc) · 1.82 KB
/
hooks.py
File metadata and controls
63 lines (55 loc) · 1.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
import logging
_logger = logging.getLogger(__name__)
def post_init_hook(env):
"""
Set access rule to support multi-company fields
"""
# Change access rule
rule = env.ref("base.res_partner_rule")
rule.write(
{
"domain_force": (
"['|', '|', ('partner_share', '=', False),"
"('company_ids', 'in', company_ids),"
"('company_ids', '=', False)]"
),
}
)
# Initialize m2m table for preserving old restrictions
env.cr.execute(
"""
INSERT INTO res_company_res_partner_rel
(res_partner_id, res_company_id)
SELECT id, company_id
FROM res_partner
WHERE company_id IS NOT NULL
"""
)
fix_user_partner_companies(env)
def fix_user_partner_companies(env):
for user in env["res.users"].search([]):
user_company_ids = set(user.company_ids.ids)
partner_company_ids = set(user.partner_id.company_ids.ids)
if not user_company_ids.issubset(partner_company_ids) and partner_company_ids:
missing_company_ids = list(user_company_ids - partner_company_ids)
user.partner_id.write(
{"company_ids": [(4, company_id) for company_id in missing_company_ids]}
)
def uninstall_hook(env):
"""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.
"""
# Change access rule
rule = env.ref("base.res_partner_rule")
rule.write(
{
"domain_force": (
"['|', '|', ('partner_share', '=', False),"
"('company_id', 'in', company_ids),"
"('company_id', '=', False)]"
),
}
)