forked from OCA/multi-company
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
77 lines (65 loc) · 2.28 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
# -*- coding: utf-8 -*-
# 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 api, SUPERUSER_ID
__all__ = [
'create_company_assignment_view',
'post_init_hook',
'uninstall_hook',
]
def create_company_assignment_view(cr):
cr.execute("""
CREATE OR REPLACE VIEW res_company_assignment
AS SELECT id, name, parent_id
FROM res_company;
""")
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, {})
# Change access rule
rule = env.ref(rule_ref)
rule.write({
'active': True,
'domain_force': (
"['|', ('company_ids', 'in', user.company_id.ids),"
" ('company_id', '=', False)]"
),
})
# 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 %s
(%s, %s)
SELECT id, company_id FROM %s
""" % (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)]"
),
})