Skip to content

Commit 7471648

Browse files
committed
[FIX] operating_unit: get default OU from correct field
This commit also clarifies the usage of the two OU-related fields in the user, both for devs and for end users.
1 parent 29e7acd commit 7471648

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

operating_unit/models/res_users.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class ResUsers(models.Model):
2323
column2="operating_unit_id",
2424
string="Operating Units",
2525
default=lambda self: self._default_operating_unit(),
26+
help="Technical field. Refer to `operating_unit_ids` if you need to "
27+
"check if an OU is assigned to a user.",
2628
)
2729

2830
default_operating_unit_id = fields.Many2one(
@@ -31,6 +33,7 @@ class ResUsers(models.Model):
3133
default=lambda self: self._default_operating_unit(),
3234
domain="[('company_id', '=', current_company_id)]",
3335
)
36+
operating_unit_readonly = fields.Boolean(compute="_compute_operating_unit_readonly")
3437

3538
@api.model
3639
def _get_default_operating_unit(self, uid2=False):
@@ -42,11 +45,11 @@ def _get_default_operating_unit(self, uid2=False):
4245
return user.default_operating_unit_id
4346
else:
4447
# find an OU of the main active company
45-
for ou in user.assigned_operating_unit_ids:
48+
for ou in user.operating_unit_ids:
4649
if ou.sudo().company_id in self.env.company:
4750
return ou
4851
# find an OU of any active company
49-
for ou in user.assigned_operating_unit_ids:
52+
for ou in user.operating_unit_ids:
5053
if ou.sudo().company_id in self.env.companies:
5154
return ou
5255
return False
@@ -71,6 +74,13 @@ def default_get(self, fields):
7174
vals["operating_unit_ids"] = [(6, 0, default_user.operating_unit_ids.ids)]
7275
return vals
7376

77+
@api.depends("groups_id")
78+
def _compute_operating_unit_readonly(self):
79+
for user in self:
80+
user.operating_unit_readonly = user.has_group(
81+
"operating_unit.group_manager_operating_unit"
82+
)
83+
7484
@api.depends("groups_id", "assigned_operating_unit_ids")
7585
def _compute_operating_unit_ids(self):
7686
for user in self:

operating_unit/tests/test_operating_unit.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,28 @@ def test_02_operating_unit(self):
8181
limit=1,
8282
)
8383
partner = self.env["res.partner"].search([], limit=1)
84+
8485
with Form(self.env["res.users"], view="base.view_users_form") as user_form:
8586
user_form.default_operating_unit_id = nou[0]
87+
user_form.name = "Test Customer"
88+
user_form.login = "test2"
89+
90+
# Initially operating_unit_ids is not editable
91+
with self.assertRaises(AssertionError):
92+
user_form.operating_unit_ids._assert_editable()
93+
94+
# The field is only editable after saving
95+
user_form.save()
8696
with user_form.operating_unit_ids.new() as line:
8797
line.partner_id = partner
8898
line.name = "Test Unit"
8999
line.code = "007"
90-
user_form.name = "Test Customer"
91-
user_form.login = "test2"
100+
101+
self.env["res.users"].browse(user_form.id).groups_id += self.grp_ou_mngr
102+
user_form = Form(self.env["res.users"], view="base.view_users_form")
103+
# operating_unit_ids is pre-filled and readonly if the user is OU manager
104+
with self.assertRaises(AssertionError):
105+
user_form.operating_unit_ids._assert_editable()
92106

93107
def test_find_operating_unit_by_name_or_code(self):
94108
ou = self._create_operating_unit(self.user1.id, "name", "code")
@@ -117,7 +131,7 @@ def test_03_operating_unit(self):
117131
ou_company_2 = self._create_operating_unit(
118132
self.user1.id, "Test Company", "TESTC", self.company_2
119133
)
120-
self.user1.assigned_operating_unit_ids += ou_company_2
134+
self.user1.operating_unit_ids += ou_company_2
121135
self.assertEqual(
122136
self.res_users_model.with_company(
123137
self.company_2

operating_unit/view/res_users_view.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@
1010
string="Operating Units"
1111
groups="operating_unit.group_multi_operating_unit"
1212
>
13-
<field name="operating_unit_ids" widget="many2many_tags" />
13+
<field name="operating_unit_readonly" invisible="1" />
14+
<span
15+
class="fa fa-warning"
16+
colspan="2"
17+
invisible="[('operating_unit_readonly', '=', False)]"
18+
>
19+
If the user is Manager of Operating Units
20+
they will always have all OUs assigned
21+
</span>
22+
<field
23+
name="operating_unit_ids"
24+
widget="many2many_tags"
25+
readonly="[('operating_unit_readonly', '=', True)]"
26+
/>
1427

1528
<field
1629
name="default_operating_unit_id"

0 commit comments

Comments
 (0)