-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
hooks.py
46 lines (41 loc) · 1.66 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
# -*- coding: utf-8 -*-
# © 2015 Oihane Crucelaegui
# © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from openerp import api, SUPERUSER_ID
def post_init_hook(cr, registry):
"""Put domain in partner access rule and copy company_id as the default
value in new field company_ids."""
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
# Change access rule
rule = env.ref('base.res_partner_rule')
rule.write({
'active': True,
'domain_force': (
"['|', ('company_ids', 'in', user.company_id.ids),"
" ('company_id', '=', False)]"
),
})
# Copy company values
partner_model = env['res.partner'].with_context(active_test=False)
groups = partner_model.read_group([], ['company_id'], ['company_id'])
for group in groups:
if group['company_id']:
partners = partner_model.search(group['__domain'])
partners.write(
{'company_ids': [(6, 0, [group['company_id'][0]])]})
def uninstall_hook(cr, registry):
"""Restore partner rule to base value."""
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
rule = env.ref('base.res_partner_rule')
rule.write({
'active': False,
'domain_force': (
"['|','|',"
"('company_id.child_ids','child_of',[user.company_id.id]),"
"('company_id','child_of',[user.company_id.id]),"
"('company_id','=',False)]"
),
})