Skip to content

Commit 08c6a7b

Browse files
committed
[MIG] res_partner_operating_unit: Migration to 19.0
1 parent cd4dd54 commit 08c6a7b

File tree

7 files changed

+70
-68
lines changed

7 files changed

+70
-68
lines changed

res_partner_operating_unit/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ Contributors
8181

8282
- Cai Hoang Huynh
8383

84+
- `Camptocamp <https://www.camptocamp.com>`__:
85+
86+
- Maksym Yankin <maksym.yankin@camptocamp.com>
87+
8488
Other credits
8589
-------------
8690

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": "18.0.1.1.1",
6+
"version": "19.0.1.0.0",
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: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (C) 2019 Serpent Consulting Services
33
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
44
from odoo import api, fields, models
5+
from odoo.fields import Domain
56

67

78
class ResPartner(models.Model):
@@ -10,7 +11,7 @@ class ResPartner(models.Model):
1011
@api.model
1112
def operating_unit_default_get(self, uid2=False):
1213
if not uid2:
13-
uid2 = self._uid
14+
uid2 = self.env.uid
1415
user = self.env["res.users"].browse(uid2)
1516
return user.default_operating_unit_id
1617

@@ -23,34 +24,35 @@ def _default_operating_units(self):
2324
return self._default_operating_unit()
2425

2526
operating_unit_ids = fields.Many2many(
26-
"operating.unit",
27-
"operating_unit_partner_rel",
28-
"partner_id",
29-
"operating_unit_id",
30-
"Operating Units",
27+
comodel_name="operating.unit",
28+
relation="operating_unit_partner_rel",
29+
column1="partner_id",
30+
column2="operating_unit_id",
31+
string="Operating Units",
3132
default=lambda self: self._default_operating_units(),
3233
)
3334

35+
@api.model
36+
def _user_ous_domain(self):
37+
ou_ids = self.env.user.operating_unit_ids.ids
38+
domain = Domain.OR(
39+
[
40+
Domain("operating_unit_ids", "in", ou_ids),
41+
Domain("operating_unit_ids", "=", False),
42+
]
43+
)
44+
return domain
45+
3446
# Extending methods to replace a record rule.
3547
# Ref: https://github.com/OCA/operating-unit/issues/258
3648
@api.model
3749
def search(self, args, offset=0, limit=None, order=None):
3850
# Get the OUs of the user
39-
ou_ids = self.env.user.operating_unit_ids.ids
40-
domain = [
41-
"|",
42-
("operating_unit_ids", "in", ou_ids),
43-
("operating_unit_ids", "=", False),
44-
]
51+
domain = self._user_ous_domain()
4552
return super().search(domain + args, offset=offset, limit=limit, order=order)
4653

4754
@api.model
4855
def search_count(self, args, limit=None):
4956
# Get the OUs of the user
50-
ou_ids = self.env.user.operating_unit_ids.ids
51-
domain = [
52-
"|",
53-
("operating_unit_ids", "in", ou_ids),
54-
("operating_unit_ids", "=", False),
55-
]
57+
domain = self._user_ous_domain()
5658
return super().search_count(domain + args, limit=limit)

res_partner_operating_unit/models/res_users.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
55
from odoo import api, models
66
from odoo.exceptions import UserError
7+
from odoo.fields import Command
78

89

910
class ResUsers(models.Model):
@@ -15,7 +16,7 @@ def create(self, vals_list):
1516
for rec in res:
1617
if rec.default_operating_unit_id:
1718
rec.partner_id.operating_unit_ids = [
18-
(4, rec.default_operating_unit_id.id)
19+
Command.link(rec.default_operating_unit_id.id)
1920
]
2021
rec.check_partner_operating_unit()
2122
return res
@@ -26,7 +27,7 @@ def write(self, vals):
2627
if vals.get("default_operating_unit_id"):
2728
# Add the new OU
2829
user.partner_id.operating_unit_ids = [
29-
(4, user.default_operating_unit_id.id)
30+
Command.link(user.default_operating_unit_id.id)
3031
]
3132
user.check_partner_operating_unit()
3233
return res

res_partner_operating_unit/readme/CONTRIBUTORS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
- Hiren Dangar \<hiren.dangar.serpentcs@gmail.com\>
44
- [Komit](https://komit-consulting.com):
55
- Cai Hoang Huynh
6+
- [Camptocamp](https://www.camptocamp.com):
7+
- Maksym Yankin \<maksym.yankin@camptocamp.com\>

res_partner_operating_unit/static/description/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ <h3><a class="toc-backref" href="#toc-entry-5">Contributors</a></h3>
432432
<li>Cai Hoang Huynh</li>
433433
</ul>
434434
</li>
435+
<li><a class="reference external" href="https://www.camptocamp.com">Camptocamp</a>:<ul>
436+
<li>Maksym Yankin &lt;<a class="reference external" href="mailto:maksym.yankin&#64;camptocamp.com">maksym.yankin&#64;camptocamp.com</a>&gt;</li>
437+
</ul>
438+
</li>
435439
</ul>
436440
</div>
437441
<div class="section" id="other-credits">

res_partner_operating_unit/tests/test_res_partner_operating_unit.py

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,68 @@
11
# © 2017 Niaga Solution - Edi Santoso <repodevs@gmail.com>
22
# Copyright (C) 2019 Serpent Consulting Services
33
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
4-
from odoo.tests import common
4+
from odoo.fields import Command, Domain
55

6+
from odoo.addons.operating_unit.tests.common import OperatingUnitCommon
67

7-
class TestResPartnerOperatingUnit(common.TransactionCase):
8-
def setUp(self):
9-
super().setUp()
10-
self.res_partner_model = self.env["res.partner"]
11-
self.res_users_model = self.env["res.users"]
12-
# Company
13-
self.company = self.env.ref("base.main_company")
14-
# Main Operating Unit
15-
self.ou1 = self.env.ref("operating_unit.main_operating_unit")
16-
# B2C Operating Unit
17-
self.b2c = self.env.ref("operating_unit.b2c_operating_unit")
18-
19-
# Create User 1 with Main OU
20-
self.user1 = self._create_user("user_1", self.company, [self.ou1])
21-
# Create User 2 with B2C OU
22-
self.user2 = self._create_user("user_2", self.company, [self.b2c])
238

9+
class TestResPartnerOperatingUnit(OperatingUnitCommon):
10+
@classmethod
11+
def setUpClass(cls):
12+
super().setUpClass()
13+
cls.res_partner_model = cls.env["res.partner"]
14+
cls.res_users_model = cls.env["res.users"]
15+
cls.grp_user = cls.env.ref("base.group_user")
16+
# Define User groups
17+
cls.user1.group_ids = [Command.set([cls.grp_user.id])]
18+
cls.user2.group_ids = [Command.set([cls.grp_user.id])]
2419
# Create Partner 1 with Main OU
25-
self.partner1 = self._create_partner("Test Partner 1", self.ou1)
26-
20+
cls.partner1 = cls._create_partner("Test Partner 1", cls.ou1)
2721
# Create Partner 2 with B2C OU
28-
self.partner2 = self._create_partner("Test Partner 2", self.b2c)
22+
cls.partner2 = cls._create_partner("Test Partner 2", cls.b2c)
2923

30-
def _create_partner(self, name, operating_unit, context=None):
24+
@classmethod
25+
def _create_partner(cls, name, operating_unit, context=None):
3126
"""Create a partner."""
32-
partner = self.res_partner_model.create(
27+
partner = cls.res_partner_model.create(
3328
{
3429
"name": name,
35-
"operating_unit_ids": [(4, operating_unit.id)],
30+
"operating_unit_ids": [Command.link(operating_unit.id)],
3631
}
3732
)
3833
return partner
3934

40-
def _create_user(self, login, company, operating_units, context=None):
41-
"""Create a user."""
42-
user = self.res_users_model.create(
43-
{
44-
"name": "Test User",
45-
"login": login,
46-
"password": "demo",
47-
"email": "test@yourcompany.com",
48-
"company_id": company.id,
49-
"company_ids": [(4, company.id)],
50-
"operating_unit_ids": [(4, ou.id) for ou in operating_units],
51-
}
52-
)
53-
return user
54-
5535
def test_create_user_adds_ou_to_partner(self):
56-
new_user = self._create_user("new_user_ou", self.company, [self.ou1])
36+
new_user = self._create_user(
37+
"new_user_ou", self.grp_ou_mngr, self.company, [self.ou1]
38+
)
5739
self.assertIn(self.ou1, new_user.partner_id.operating_unit_ids)
5840

5941
def test_write_user_adds_new_ou_to_partner(self):
60-
new_user = self._create_user("edit_user_ou", self.company, [self.ou1])
42+
new_user = self._create_user(
43+
"edit_user_ou", self.grp_ou_mngr, self.company, [self.ou1]
44+
)
6145
self.assertIn(self.ou1, new_user.partner_id.operating_unit_ids)
6246

6347
new_user.write(
6448
{
6549
"default_operating_unit_id": self.b2c.id,
66-
"operating_unit_ids": [(4, self.b2c.id)],
50+
"operating_unit_ids": [Command.link(self.b2c.id)],
6751
}
6852
)
6953

7054
self.assertIn(self.b2c, new_user.partner_id.operating_unit_ids)
7155

7256
def test_search_partner_with_operating_unit(self):
7357
partners = self.res_partner_model.with_user(self.user1).search(
74-
[("name", "!=", "")]
58+
Domain("name", "!=", "")
7559
)
7660
self.assertIn(self.partner1, partners)
7761
self.assertNotIn(self.partner2, partners)
7862

7963
def test_search_count_partner_with_operating_unit(self):
8064
count = self.res_partner_model.with_user(self.user2).search_count(
81-
[("name", "!=", "")]
65+
Domain("name", "!=", "")
8266
)
8367
self.assertGreaterEqual(count, 1)
8468

@@ -90,22 +74,27 @@ def test_create_user_with_default_operating_unit(self):
9074
"password": "demo",
9175
"email": "user@default.com",
9276
"company_id": self.company.id,
93-
"company_ids": [(4, self.company.id)],
94-
"operating_unit_ids": [(4, self.ou1.id)],
77+
"company_ids": [Command.link(self.company.id)],
78+
"operating_unit_ids": [Command.link(self.ou1.id)],
9579
"default_operating_unit_id": self.ou1.id,
9680
}
9781
)
9882

9983
self.assertIn(self.ou1, new_user.partner_id.operating_unit_ids)
10084

10185
def test_write_user_sets_default_operating_unit(self):
102-
user = self._create_user("write_user_default_ou", self.company, [self.ou1])
86+
user = self._create_user(
87+
"write_user_default_ou", self.grp_ou_mngr, self.company, [self.ou1]
88+
)
10389
self.assertIn(self.ou1, user.partner_id.operating_unit_ids)
10490

10591
user.write(
10692
{
10793
"default_operating_unit_id": self.b2c.id,
108-
"operating_unit_ids": [(4, self.ou1.id), (4, self.b2c.id)],
94+
"operating_unit_ids": [
95+
Command.link(self.ou1.id),
96+
Command.link(self.b2c.id),
97+
],
10998
}
11099
)
111100

0 commit comments

Comments
 (0)