forked from oerp-odoo/oerp-odoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
139 lines (115 loc) · 5.13 KB
/
utils.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
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
from odoo.osv import expression
# TODO: move these to more general module.
def build_default_code(obj, default):
"""Set default_code when duplicating data.
Args:
obj: product.product or product.template record
default: dictionary to update for copy data.
"""
default_code = obj.default_code
# Leave original default_code (False) if it is not set.
if default_code:
return default_code + obj.env._(" (copy)")
return False
def search_multicompany(
model_obj, domain, offset=0, limit=None, order=None, options=None
):
"""Find multi-company friendly records by domain.
Intended to be used for fields where value uniqueness must be
preserved.
Uniqueness checked globally if objects are shared across
multiple companies, otherwise current object company is used.
Args:
model_obj: odoo Model to search on.
domain: base domain for search. Should not include
multi-company leaf.
offset (int): number of results to ignore (default: {0})
limit (int): maximum number of records to return (default:
{None})
order (str): sort string (default {None})
options (dict): extra options to modify search. Dict can
have such keys. (default: {None}):
multi_comp_rule_xml_id (str): multi company ir.rule
XMLID that is used to identify if multi-company is
used for that object. Optional (default: {None}).
company_id (int): company ID that is used for multi
company domain. If multi_company_rule_xml_id is not
used and company_id is specified, it will be force
used in domain. Optional (default: {False}).
active_test (bool): whether to include inactive records.
False value means include (default: {False}).
Returns:
recordset
"""
return __prepare_search_multicompany_method(
model_obj, domain, offset=offset, limit=limit, order=order, options=options
)()
def search_multicompany_count(model_obj, domain, options=None):
"""Find multi-company friendly records count by domain.
Intended to be used for fields where value uniqueness must be
preserved.
Uniqueness checked globally if objects are shared across
multiple companies, otherwise current object company is used.
Args:
model_obj: odoo Model to search on.
domain: base domain for search. Should not include
multi-company leaf.
{None})
options (dict): extra options to modify search. Dict can
have such keys. (default: {None}):
multi_comp_rule_xml_id (str): multi company ir.rule
XMLID that is used to identify if multi-company is
used for that object. Optional (default: {None}).
company_id (int): company ID that is used for multi
company domain. If multi_company_rule_xml_id is not
used and company_id is specified, it will be force
used in domain. Optional (default: {False}).
active_test (bool): whether to include inactive records.
False value means include (default: {False}).
Returns:
recordset
"""
return __prepare_search_multicompany_method(model_obj, domain, options=options)(
count=True
)
def __prepare_search_multicompany_method(
model_obj, domain, offset=0, limit=None, order=None, options=None
):
def is_multi_comp_used(multi_comp_rule_xml_id, company_id):
# Multi company is used, if there is explicit rule to
# enable/disable it or if company_id was passed as argument.
# Multi company rule takes priority.
if multi_comp_rule_xml_id:
# Rule that defines if multi-company rule is enabled (
# shared globally or per company)
return (
model_obj.sudo().env.ref(multi_comp_rule_xml_id).active
# Can filter per company, only if company is passed,
# otherwise would filter for partners that have no
# company set only.
and company_id
)
return bool(company_id)
def get_company_domain(multi_comp_rule_xml_id, company_id):
if is_multi_comp_used(multi_comp_rule_xml_id, company_id):
return [('company_id', 'in', [company_id, False])]
return []
def to_multicompany_domain(domain):
company_domain = get_company_domain(
options.get('multi_comp_rule_xml_id'),
options.get('company_id', False),
)
return expression.AND([domain, company_domain])
def _search_multicompany_method(count=False):
if count:
return model_obj.with_context(active_test=active_test).search_count(
domain, limit=limit
)
return model_obj.with_context(active_test=active_test).search(
domain, offset=offset, limit=limit, order=order
)
if not options:
options = {}
domain = to_multicompany_domain(domain)
active_test = options.get('active_test', False)
return _search_multicompany_method