Skip to content

Commit 4266bd4

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 28a8d28 commit 4266bd4

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

operating_unit/models/res_users.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def operating_unit_default_get(self, uid2=False):
1919
return user.default_operating_unit_id
2020
else:
2121
# find an OU of the main active company
22-
for ou in user.assigned_operating_unit_ids:
22+
for ou in user.operating_unit_ids:
2323
if ou.sudo().company_id in self.env.company:
2424
return ou
2525
# find an OU of any active company
26-
for ou in user.assigned_operating_unit_ids:
26+
for ou in user.operating_unit_ids:
2727
if ou.sudo().company_id in self.env.companies:
2828
return ou
2929
return False
@@ -51,6 +51,8 @@ def _default_operating_units(self):
5151
column2="operating_unit_id",
5252
string="Operating Units",
5353
default=lambda self: self._default_operating_units(),
54+
help="Technical field. Refer to `operating_unit_ids` if you need to "
55+
"check if an OU is assigned to a user.",
5456
)
5557

5658
default_operating_unit_id = fields.Many2one(
@@ -59,6 +61,7 @@ def _default_operating_units(self):
5961
default=lambda self: self._default_operating_unit(),
6062
domain="[('company_id', '=', current_company_id)]",
6163
)
64+
operating_unit_readonly = fields.Boolean(compute="_compute_operating_unit_readonly")
6265

6366
@api.onchange("operating_unit_ids")
6467
def _onchange_operating_unit_ids(self):
@@ -87,6 +90,13 @@ def _compute_operating_unit_ids(self):
8790
else:
8891
user.operating_unit_ids = user.assigned_operating_unit_ids
8992

93+
@api.depends("groups_id")
94+
def _compute_operating_unit_readonly(self):
95+
for user in self:
96+
user.operating_unit_readonly = user.has_group(
97+
"operating_unit.group_manager_operating_unit"
98+
)
99+
90100
@api.model
91101
def default_get(self, fields):
92102
vals = super(ResUsers, self).default_get(fields)

operating_unit/tests/test_operating_unit.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,28 @@ def test_02_operating_unit(self):
140140
limit=1,
141141
)
142142
partner = self.env["res.partner"].search([], limit=1)
143+
143144
with Form(self.env["res.users"], view="base.view_users_form") as user_form:
144145
user_form.default_operating_unit_id = nou[0]
146+
user_form.name = "Test Customer"
147+
user_form.login = "test2"
148+
149+
# Initially operating_unit_ids is not editable
150+
with self.assertRaises(AssertionError):
151+
user_form.operating_unit_ids._assert_editable()
152+
153+
# The field is only editable after saving
154+
user_form.save()
145155
with user_form.operating_unit_ids.new() as line:
146156
line.partner_id = partner
147157
line.name = "Test Unit"
148158
line.code = "007"
149-
user_form.name = "Test Customer"
150-
user_form.login = "test2"
159+
160+
self.env["res.users"].browse(user_form.id).groups_id += self.grp_ou_mngr
161+
user_form = Form(self.env["res.users"], view="base.view_users_form")
162+
# operating_unit_ids is pre-filled and readonly if the user is OU manager
163+
with self.assertRaises(AssertionError):
164+
user_form.operating_unit_ids._assert_editable()
151165

152166
def test_03_operating_unit(self):
153167
"""
@@ -169,7 +183,7 @@ def test_03_operating_unit(self):
169183
ou_company_2 = self._create_operating_unit(
170184
self.user1.id, "Test Company", "TESTC", self.company_2
171185
)
172-
self.user1.assigned_operating_unit_ids += ou_company_2
186+
self.user1.operating_unit_ids += ou_company_2
173187
self.assertEqual(
174188
self.res_users_model.with_company(
175189
self.company_2

operating_unit/view/res_users_view.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@
77
<field name="arch" type="xml">
88
<xpath expr="//page[@name='access_rights']/group[1]" position="after">
99
<group string="Operating Units">
10+
<field name="operating_unit_readonly" invisible="1" />
11+
<span
12+
class="fa fa-warning"
13+
colspan="2"
14+
attrs="{'invisible': [('operating_unit_readonly', '=', False)]}"
15+
>
16+
If the user is Manager of Operating Units
17+
they will always have all OUs assigned
18+
</span>
1019
<field
1120
name="operating_unit_ids"
1221
groups="operating_unit.group_multi_operating_unit"
1322
widget="many2many_tags"
23+
attrs="{'readonly': [('operating_unit_readonly', '=', True)]}"
1424
/>
1525

1626
<field

0 commit comments

Comments
 (0)