Skip to content

Commit 7df46c3

Browse files
committed
[17.0][IMP] res_partner_operating_unit:
Backport access issue fix and improvements from v19 (#817)
1 parent b309481 commit 7df46c3

4 files changed

Lines changed: 56 additions & 62 deletions

File tree

res_partner_operating_unit/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"category": "Generic",
1414
"depends": ["operating_unit"],
1515
"license": "LGPL-3",
16-
"data": ["security/res_partner_security.xml", "views/res_partner_view.xml"],
16+
"data": ["views/res_partner_view.xml"],
1717
"installable": True,
1818
"pre_init_hook": "pre_init_hook",
1919
}

res_partner_operating_unit/models/res_partner.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,34 @@ class ResPartner(models.Model):
88
_inherit = "res.partner"
99
_check_company_auto = True
1010

11-
@api.model
12-
def _default_operating_unit(self):
13-
user = self.env["res.users"].browse(self.env.user.id)
14-
return user.default_operating_unit_id
15-
1611
operating_unit_ids = fields.Many2many(
17-
"operating.unit",
18-
"operating_unit_partner_rel",
19-
"partner_id",
20-
"operating_unit_id",
21-
"Operating Units",
22-
required=True,
23-
default=lambda self: self._default_operating_unit(),
12+
comodel_name="operating.unit",
13+
relation="operating_unit_partner_rel",
14+
column1="partner_id",
15+
column2="operating_unit_id",
16+
string="Operating Units",
2417
)
18+
19+
@api.model
20+
def _user_ous_domain(self):
21+
ou_ids = self.env.user.operating_unit_ids.ids
22+
domain = [
23+
"|",
24+
("operating_unit_ids", "in", ou_ids),
25+
("operating_unit_ids", "=", False),
26+
]
27+
return domain
28+
29+
# Extending methods to replace a record rule.
30+
# Ref: https://github.com/OCA/operating-unit/issues/258
31+
@api.model
32+
def search(self, args, offset=0, limit=None, order=None):
33+
# Get the OUs of the user
34+
domain = self._user_ous_domain()
35+
return super().search(domain + args, offset=offset, limit=limit, order=order)
36+
37+
@api.model
38+
def search_count(self, args, limit=None):
39+
# Get the OUs of the user
40+
domain = self._user_ous_domain()
41+
return super().search_count(domain + args, limit=limit)

res_partner_operating_unit/models/res_users.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,39 @@
99
class ResUsers(models.Model):
1010
_inherit = "res.users"
1111

12+
def _sync_partner_default_operating_unit(self):
13+
for user in self:
14+
if user.default_operating_unit_id:
15+
user.partner_id.operating_unit_ids = [
16+
Command.link(user.default_operating_unit_id.id)
17+
]
18+
user.check_partner_operating_unit()
19+
1220
@api.model_create_multi
1321
def create(self, vals_list):
1422
users = super().create(vals_list)
1523
for user in users:
16-
user_ou = user.default_operating_unit_id or user._default_operating_unit()
17-
if not user_ou:
18-
user.check_partner_operating_unit()
19-
continue
20-
user.partner_id.operating_unit_ids = [Command.link(user_ou.id)]
21-
user.check_partner_operating_unit()
24+
user._sync_partner_default_operating_unit()
2225
return users
2326

2427
def write(self, vals):
25-
for user in self:
26-
res = super().write(vals)
27-
if vals.get("default_operating_unit_id"):
28-
# Add the new OU
29-
user.partner_id.operating_unit_ids = [
30-
Command.link(user.default_operating_unit_id.id)
31-
]
32-
user.check_partner_operating_unit()
33-
return res
28+
res = super().write(vals)
29+
if vals.get("default_operating_unit_id"):
30+
for user in self:
31+
user._sync_partner_default_operating_unit()
32+
return res
3433

3534
def check_partner_operating_unit(self):
36-
self.ensure_one()
37-
if (
38-
self.partner_id.operating_unit_ids
39-
and self.default_operating_unit_id
40-
and (
41-
self.default_operating_unit_id.id
42-
not in self.partner_id.operating_unit_ids.ids
43-
)
44-
):
45-
raise UserError(
46-
_(
47-
"The operating units of the partner must include the default "
48-
"one of the user."
35+
for user in self:
36+
if (
37+
user.partner_id.operating_unit_ids
38+
and user.default_operating_unit_id
39+
and user.default_operating_unit_id.id
40+
not in user.partner_id.operating_unit_ids.ids
41+
):
42+
raise UserError(
43+
_(
44+
"The operating units of the partner must include the default "
45+
"one of the user."
46+
)
4947
)
50-
)

res_partner_operating_unit/security/res_partner_security.xml

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)