Skip to content

Commit 9224eec

Browse files
committed
[MIG] res_partner_operating_unit: Migration to 18.0
1 parent 5ba08cc commit 9224eec

File tree

7 files changed

+90
-22
lines changed

7 files changed

+90
-22
lines changed

res_partner_operating_unit/README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Contributors
7373
- Edi Santoso <repodevs@gmail.com>
7474
- Maxime Chambreuil <mchambreuil@opensourceintegrators.com>
7575
- Hiren Dangar <hiren.dangar.serpentcs@gmail.com>
76+
`Komit <https://komit-consulting.com>`__:
77+
- Cai Hoang Huynh
7678

7779
Other credits
7880
-------------

res_partner_operating_unit/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"name": "Partner with Operating Unit",
55
"summary": "Introduces Operating Unit fields in Partner",
6-
"version": "12.0.1.1.1",
6+
"version": "18.0.1.1.1",
77
"author": "Edi Santoso, " "Niaga Solution, " "Odoo Community Association (OCA)",
88
"website": "https://github.com/OCA/operating-unit",
99
"category": "Generic",

res_partner_operating_unit/models/res_partner.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,15 @@ def _default_operating_units(self):
3434
# Extending methods to replace a record rule.
3535
# Ref: https://github.com/OCA/operating-unit/issues/258
3636
@api.model
37-
def search(self, args, offset=0, limit=None, order=None, count=False):
37+
def search(self, args, offset=0, limit=None, order=None):
3838
# Get the OUs of the user
3939
ou_ids = self.env.user.operating_unit_ids.ids
4040
domain = [
4141
"|",
4242
("operating_unit_ids", "in", ou_ids),
4343
("operating_unit_ids", "=", False),
4444
]
45-
return super().search(
46-
domain + args, offset=offset, limit=limit, order=order, count=count
47-
)
45+
return super().search(domain + args, offset=offset, limit=limit, order=order)
4846

4947
@api.model
5048
def search_count(self, args):

res_partner_operating_unit/models/res_users.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,43 @@
22
# Copyright (C) 2019 Serpent Consulting Services
33
# Copyright (C) 2019 Open Source Integrators
44
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
5-
from odoo import _, api, models
5+
from odoo import api, models
66
from odoo.exceptions import UserError
77

88

99
class ResUsers(models.Model):
1010
_inherit = "res.users"
1111

12-
@api.model
13-
def create(self, vals):
14-
res = super().create(vals)
15-
res.partner_id.operating_unit_ids = [(4, res.default_operating_unit_id.id)]
12+
@api.model_create_multi
13+
def create(self, vals_list):
14+
res = super().create(vals_list)
15+
for rec in res:
16+
if rec.default_operating_unit_id:
17+
rec.partner_id.operating_unit_ids = [
18+
(4, rec.default_operating_unit_id.id)
19+
]
20+
rec.check_partner_operating_unit()
1621
return res
1722

18-
@api.multi
1923
def write(self, vals):
20-
res = super().write(vals)
21-
if vals.get("default_operating_unit_id"):
22-
# Add the new OU
23-
self.partner_id.operating_unit_ids = [
24-
(4, vals["default_operating_unit_id"])
25-
]
26-
return res
24+
for user in self:
25+
res = super().write(vals)
26+
if vals.get("default_operating_unit_id"):
27+
# Add the new OU
28+
user.partner_id.operating_unit_ids = [
29+
(4, user.default_operating_unit_id.id)
30+
]
31+
user.check_partner_operating_unit()
32+
return res
2733

28-
@api.constrains("partner_id.operating_unit_ids", "default_operating_unit_id")
2934
def check_partner_operating_unit(self):
3035
if (
3136
self.partner_id.operating_unit_ids
3237
and self.default_operating_unit_id.id
3338
not in self.partner_id.operating_unit_ids.ids
3439
):
3540
raise UserError(
36-
_(
41+
self.env._(
3742
"The operating units of the partner must include the default "
3843
"one of the user."
3944
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
- Edi Santoso \<repodevs@gmail.com\>
22
- Maxime Chambreuil \<mchambreuil@opensourceintegrators.com\>
33
- Hiren Dangar \<hiren.dangar.serpentcs@gmail.com\>
4+
[Komit](https://komit-consulting.com):
5+
- Cai Hoang Huynh

res_partner_operating_unit/static/description/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
422422
<ul class="simple">
423423
<li>Edi Santoso &lt;<a class="reference external" href="mailto:repodevs&#64;gmail.com">repodevs&#64;gmail.com</a>&gt;</li>
424424
<li>Maxime Chambreuil &lt;<a class="reference external" href="mailto:mchambreuil&#64;opensourceintegrators.com">mchambreuil&#64;opensourceintegrators.com</a>&gt;</li>
425-
<li>Hiren Dangar &lt;<a class="reference external" href="mailto:hiren.dangar.serpentcs&#64;gmail.com">hiren.dangar.serpentcs&#64;gmail.com</a>&gt;</li>
425+
<li>Hiren Dangar &lt;<a class="reference external" href="mailto:hiren.dangar.serpentcs&#64;gmail.com">hiren.dangar.serpentcs&#64;gmail.com</a>&gt;
426+
<a class="reference external" href="https://komit-consulting.com">Komit</a>:</li>
427+
<li>Cai Hoang Huynh</li>
426428
</ul>
427429
</div>
428430
<div class="section" id="other-credits">

res_partner_operating_unit/tests/test_res_partner_operating_unit.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _create_partner(self, name, operating_unit, context=None):
3232
partner = self.res_partner_model.create(
3333
{
3434
"name": name,
35-
"operating_unit_id": operating_unit.id,
35+
"operating_unit_ids": [(4, operating_unit.id)],
3636
}
3737
)
3838
return partner
@@ -51,3 +51,62 @@ def _create_user(self, login, company, operating_units, context=None):
5151
}
5252
)
5353
return user
54+
55+
def test_create_user_adds_ou_to_partner(self):
56+
new_user = self._create_user("new_user_ou", self.company, [self.ou1])
57+
self.assertIn(self.ou1, new_user.partner_id.operating_unit_ids)
58+
59+
def test_write_user_adds_new_ou_to_partner(self):
60+
new_user = self._create_user("edit_user_ou", self.company, [self.ou1])
61+
self.assertIn(self.ou1, new_user.partner_id.operating_unit_ids)
62+
63+
new_user.write(
64+
{
65+
"default_operating_unit_id": self.b2c.id,
66+
"operating_unit_ids": [(4, self.b2c.id)],
67+
}
68+
)
69+
70+
self.assertIn(self.b2c, new_user.partner_id.operating_unit_ids)
71+
72+
def test_search_partner_with_operating_unit(self):
73+
partners = self.res_partner_model.with_user(self.user1).search(
74+
[("name", "!=", "")]
75+
)
76+
self.assertIn(self.partner1, partners)
77+
self.assertNotIn(self.partner2, partners)
78+
79+
def test_search_count_partner_with_operating_unit(self):
80+
count = self.res_partner_model.with_user(self.user2).search_count(
81+
[("name", "!=", "")]
82+
)
83+
self.assertGreaterEqual(count, 1)
84+
85+
def test_create_user_with_default_operating_unit(self):
86+
new_user = self.res_users_model.create(
87+
{
88+
"name": "Create With Default OU",
89+
"login": "user_with_default_ou",
90+
"password": "demo",
91+
"email": "user@default.com",
92+
"company_id": self.company.id,
93+
"company_ids": [(4, self.company.id)],
94+
"operating_unit_ids": [(4, self.ou1.id)],
95+
"default_operating_unit_id": self.ou1.id,
96+
}
97+
)
98+
99+
self.assertIn(self.ou1, new_user.partner_id.operating_unit_ids)
100+
101+
def test_write_user_sets_default_operating_unit(self):
102+
user = self._create_user("write_user_default_ou", self.company, [self.ou1])
103+
self.assertIn(self.ou1, user.partner_id.operating_unit_ids)
104+
105+
user.write(
106+
{
107+
"default_operating_unit_id": self.b2c.id,
108+
"operating_unit_ids": [(4, self.ou1.id), (4, self.b2c.id)],
109+
}
110+
)
111+
112+
self.assertIn(self.b2c, user.partner_id.operating_unit_ids)

0 commit comments

Comments
 (0)