Skip to content

Commit

Permalink
[IMP] shopinvader_unit_management: Use fields for members/units
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Mar 12, 2024
1 parent 350b3d5 commit 0f56e58
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 123 deletions.
4 changes: 3 additions & 1 deletion shopinvader_api_unit_member/security/res_partner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
name="groups"
eval="[(4, ref('shopinvader_api_unit_member.shopinvader_unit_management_user_group'))]"
/>
<field name="domain_force">[('parent_id','=',authenticated_partner_id)]</field>
<field
name="domain_force"
>['|', ('unit_id.manager_ids','=',authenticated_partner_id), ('manager_ids','=',authenticated_partner_id)]</field>

</record>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,21 @@ def test_get_manager_unit_members_wrong_type(self):
response: Response = test_client.get(f"/unit/members/{self.unit_1.id}")
self.assertEqual(
response.status_code,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
)
with self._rollback_called_test_client() as test_client:
response: Response = test_client.get(f"/unit/members/{self.unit_2.id}")
self.assertEqual(
response.status_code,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
)

def test_create_unit_member(self):
"""
Test to create a new unit member
"""
self.assertEqual(
self.unit_1._get_unit_members(),
self.unit_1.member_ids,
self.manager_1_1
| self.collaborator_1_1
| self.collaborator_1_2
Expand All @@ -206,10 +206,10 @@ def test_create_unit_member(self):
member = response.json()
new_member = self.env["res.partner"].browse(member["id"])
self.assertEqual(member["name"], "New Unit Member")
self.assertEqual(new_member._get_unit(), self.unit_1)
self.assertEqual(new_member.unit_id, self.unit_1)
self.assertEqual(new_member.unit_profile, "collaborator")
self.assertEqual(
self.unit_1._get_unit_members(),
self.unit_1.member_ids,
self.manager_1_1
| self.collaborator_1_1
| self.collaborator_1_2
Expand All @@ -221,7 +221,7 @@ def test_create_unit_member(self):

def test_create_unit_manager(self):
self.assertEqual(
self.unit_1._get_unit_members(),
self.unit_1.member_ids,
self.manager_1_1
| self.collaborator_1_1
| self.collaborator_1_2
Expand All @@ -243,10 +243,10 @@ def test_create_unit_manager(self):
member = response.json()
new_member = self.env["res.partner"].browse(member["id"])
self.assertEqual(member["name"], "New Unit Manager")
self.assertEqual(new_member._get_unit(), self.unit_1)
self.assertEqual(new_member.unit_id, self.unit_1)
self.assertEqual(new_member.unit_profile, "manager")
self.assertEqual(
self.unit_1._get_unit_members(),
self.unit_1.member_ids,
self.manager_1_1
| self.collaborator_1_1
| self.collaborator_1_2
Expand Down Expand Up @@ -294,7 +294,7 @@ def test_update_unit_member(self):
Test to update a specific unit member
"""
self.assertEqual(
set(self.unit_1._get_unit_members().mapped("name")),
set(self.unit_1.member_ids.mapped("name")),
{
"Manager 1.1",
"Collaborator 1.1",
Expand Down Expand Up @@ -326,7 +326,7 @@ def test_update_unit_member(self):
self.assertEqual(updated_member.name, "Updated Unit Member")

self.assertEqual(
set(self.unit_1._get_unit_members().mapped("name")),
set(self.unit_1.member_ids.mapped("name")),
{
"Manager 1.1",
"Collaborator 1.1",
Expand All @@ -342,7 +342,7 @@ def test_update_unit_manager(self):
Test to update a specific unit manager
"""
self.assertEqual(
set(self.unit_1._get_unit_members().mapped("name")),
set(self.unit_1.member_ids.mapped("name")),
{
"Manager 1.1",
"Collaborator 1.1",
Expand Down Expand Up @@ -374,7 +374,7 @@ def test_update_unit_manager(self):
self.assertEqual(updated_member.name, "Updated Unit Manager")

self.assertEqual(
set(self.unit_1._get_unit_members().mapped("name")),
set(self.unit_1.member_ids.mapped("name")),
{
"Updated Unit Manager",
"Collaborator 1.1",
Expand Down Expand Up @@ -402,7 +402,7 @@ def test_delete_unit_membere(self):
Test to delete a specific unit member
"""
self.assertEqual(
set(self.unit_1._get_unit_members().mapped("name")),
set(self.unit_1.member_ids.mapped("name")),
{
"Manager 1.1",
"Collaborator 1.1",
Expand All @@ -424,7 +424,7 @@ def test_delete_unit_membere(self):
)
self.assertFalse(self.collaborator_1_4.active)
self.assertEqual(
set(self.unit_1._get_unit_members().mapped("name")),
set(self.unit_1.member_ids.mapped("name")),
{
"Manager 1.1",
"Collaborator 1.1",
Expand All @@ -439,7 +439,7 @@ def test_delete_unit_manager(self):
Test to delete a specific unit manager
"""
self.assertEqual(
set(self.unit_1._get_unit_members().mapped("name")),
set(self.unit_1.member_ids.mapped("name")),
{
"Manager 1.1",
"Collaborator 1.1",
Expand All @@ -462,7 +462,7 @@ def test_delete_unit_manager(self):

self.assertFalse(self.manager_1_1.active)
self.assertEqual(
set(self.unit_1._get_unit_members().mapped("name")),
set(self.unit_1.member_ids.mapped("name")),
{
"Collaborator 1.1",
"Collaborator 1.2",
Expand Down
2 changes: 1 addition & 1 deletion shopinvader_api_unit_request/routers/unit_request_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _get_domain_adapter(self):
(
"request_partner_id",
"in",
self.partner._get_unit()._get_unit_collaborators().ids,
self.partner.unit_id.collaborator_ids.ids,
),
("request_order_id", "=", False),
]
Expand Down
68 changes: 17 additions & 51 deletions shopinvader_unit_management/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,19 @@ class ResPartner(models.Model):
required=False,
)

@api.constrains("unit_profile", "parent_id")
def _check_unit_profile(self):
if self.unit_profile:
if self.unit_profile == "unit" and self.parent_id:
raise ValidationError(_("A unit can't have a parent."))
if self.unit_profile in ["manager", "collaborator"] and not self.parent_id:
raise ValidationError(
_("A manager or a collaborator must have a parent unit.")
)
unit_id = fields.Many2one("res.partner", related="parent_id", string="Unit")

@api.model
def _get_unit_members(self):
self.ensure_one()
if self.unit_profile != "unit":
raise AccessError(_("This method is only available for units."))
return self.search(
[
("parent_id", "=", self.id),
("unit_profile", "in", ["manager", "collaborator"]),
]
)

@api.model
def _get_unit_managers(self):
self.ensure_one()
if self.unit_profile != "unit":
raise AccessError(_("This method is only available for units."))
return self.search(
[("parent_id", "=", self.id), ("unit_profile", "=", "manager")]
)

@api.model
def _get_unit_collaborators(self):
self.ensure_one()
if self.unit_profile != "unit":
raise AccessError(_("This method is only available for units."))
return self.search(
[("parent_id", "=", self.id), ("unit_profile", "=", "collaborator")]
)

@api.model
def _get_unit(self):
self.ensure_one()
if self.unit_profile not in ["manager", "collaborator"]:
raise AccessError(
_("This method is only available for managers and collaborators.")
)
return self.parent_id
manager_ids = fields.One2many(
"res.partner", "unit_id", domain=[("unit_profile", "=", "manager")]
)
collaborator_ids = fields.One2many(
"res.partner", "unit_id", domain=[("unit_profile", "=", "collaborator")]
)
member_ids = fields.One2many(
"res.partner",
"unit_id",
domain=[("unit_profile", "in", ["manager", "collaborator"])],
)

def _ensure_manager(self):
"""Ensure the partner is a manager."""
Expand All @@ -75,14 +40,13 @@ def _ensure_manager(self):

def _ensure_same_unit(self, member):
"""Ensure the member is in the same unit."""
if not member or member._get_unit() != self._get_unit():
if not member or member.unit_id != self.unit_id:
raise MissingError(_("Member not found"))

@api.model
def _get_shopinvader_unit_members(self):
self._ensure_manager()
unit = self._get_unit()
return unit._get_unit_members()
return self.unit_id.member_ids

@api.model
def _get_shopinvader_unit_member(self, id):
Expand All @@ -94,7 +58,9 @@ def _get_shopinvader_unit_member(self, id):
@api.model
def _create_shopinvader_unit_member(self, vals):
self._ensure_manager()
vals["parent_id"] = self._get_unit().id
# FIXME:
vals[self._fields["unit_id"].related[0]] = self.unit_id.id

if "unit_profile" not in vals:
vals["unit_profile"] = "collaborator"
if vals["unit_profile"] not in dict(self._fields["unit_profile"].selection):
Expand Down
86 changes: 32 additions & 54 deletions shopinvader_unit_management/tests/test_unit_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,110 +11,88 @@ class TestUnitManagement(TestUnitManagementCommon):
def test_unit_management_units(self):
self.assertEqual(self.unit_1.unit_profile, "unit")
self.assertEqual(
self.unit_1._get_unit_members(),
self.unit_1.member_ids,
self.manager_1_1
| self.collaborator_1_1
| self.collaborator_1_2
| self.collaborator_1_3
| self.collaborator_1_4
| self.collaborator_1_5,
)
self.assertEqual(self.unit_1._get_unit_managers(), self.manager_1_1)
self.assertEqual(self.unit_1.manager_ids, self.manager_1_1)
self.assertEqual(
self.unit_1._get_unit_collaborators(),
self.unit_1.collaborator_ids,
self.collaborator_1_1
| self.collaborator_1_2
| self.collaborator_1_3
| self.collaborator_1_4
| self.collaborator_1_5,
)
with self.assertRaises(AccessError):
self.unit_1._get_unit()
self.assertFalse(self.unit_1.unit_id)

self.assertEqual(self.unit_2.unit_profile, "unit")
self.assertEqual(
self.unit_2._get_unit_members(),
self.unit_2.member_ids,
self.manager_2_1
| self.manager_2_2
| self.collaborator_2_1
| self.collaborator_2_2
| self.collaborator_2_3,
)
self.assertEqual(self.unit_2.manager_ids, self.manager_2_1 | self.manager_2_2)
self.assertEqual(
self.unit_2._get_unit_managers(), self.manager_2_1 | self.manager_2_2
)
self.assertEqual(
self.unit_2._get_unit_collaborators(),
self.unit_2.collaborator_ids,
self.collaborator_2_1 | self.collaborator_2_2 | self.collaborator_2_3,
)
with self.assertRaises(AccessError):
self.unit_2._get_unit()
self.assertFalse(self.unit_2.unit_id)

self.assertEqual(self.unit_3.unit_profile, "unit")
self.assertEqual(
self.unit_3._get_unit_members(),
self.unit_3.member_ids,
self.collaborator_3_1 | self.collaborator_3_2 | self.collaborator_3_3,
)
self.assertEqual(self.unit_3._get_unit_managers(), self.env["res.partner"])
self.assertEqual(self.unit_3.manager_ids, self.env["res.partner"])
self.assertEqual(
self.unit_3._get_unit_collaborators(),
self.unit_3.collaborator_ids,
self.collaborator_3_1 | self.collaborator_3_2 | self.collaborator_3_3,
)
with self.assertRaises(AccessError):
self.unit_3._get_unit()
self.assertFalse(self.unit_3.unit_id)

self.assertEqual(self.unit_4.unit_profile, "unit")
self.assertEqual(self.unit_4.member_ids, self.manager_4_1 | self.manager_4_2)
self.assertEqual(self.unit_4.manager_ids, self.manager_4_1 | self.manager_4_2)
self.assertEqual(
self.unit_4._get_unit_members(), self.manager_4_1 | self.manager_4_2
)
self.assertEqual(
self.unit_4._get_unit_managers(), self.manager_4_1 | self.manager_4_2
)
self.assertEqual(
self.unit_4._get_unit_collaborators(),
self.unit_4.collaborator_ids,
self.env["res.partner"],
)
with self.assertRaises(AccessError):
self.unit_4._get_unit()
self.assertFalse(self.unit_4.unit_id)

def test_unit_management_managers(self):
self.assertEqual(self.manager_1_1.unit_profile, "manager")
self.assertEqual(self.manager_1_1._get_unit(), self.unit_1)
self.assertEqual(self.manager_1_1.unit_id, self.unit_1)

with self.assertRaises(AccessError):
self.manager_1_1._get_unit_members()
with self.assertRaises(AccessError):
self.manager_1_1._get_unit_managers()
with self.assertRaises(AccessError):
self.manager_1_1._get_unit_collaborators()
self.assertFalse(self.manager_1_1.member_ids)
self.assertFalse(self.manager_1_1.manager_ids)
self.assertFalse(self.manager_1_1.collaborator_ids)

self.assertEqual(self.manager_4_2.unit_profile, "manager")
self.assertEqual(self.manager_4_2._get_unit(), self.unit_4)
self.assertEqual(self.manager_4_2.unit_id, self.unit_4)

with self.assertRaises(AccessError):
self.manager_4_2._get_unit_members()
with self.assertRaises(AccessError):
self.manager_4_2._get_unit_managers()
with self.assertRaises(AccessError):
self.manager_4_2._get_unit_collaborators()
self.assertFalse(self.manager_4_2.member_ids)
self.assertFalse(self.manager_4_2.manager_ids)
self.assertFalse(self.manager_4_2.collaborator_ids)

def test_unit_management_collaborators(self):
self.assertEqual(self.collaborator_1_1.unit_profile, "collaborator")
self.assertEqual(self.collaborator_1_1._get_unit(), self.unit_1)
self.assertEqual(self.collaborator_1_1.unit_id, self.unit_1)

with self.assertRaises(AccessError):
self.collaborator_1_1._get_unit_members()
with self.assertRaises(AccessError):
self.collaborator_1_1._get_unit_managers()
with self.assertRaises(AccessError):
self.collaborator_1_1._get_unit_collaborators()
self.assertFalse(self.collaborator_1_1.member_ids)
self.assertFalse(self.collaborator_1_1.manager_ids)
self.assertFalse(self.collaborator_1_1.collaborator_ids)

self.assertEqual(self.collaborator_3_3.unit_profile, "collaborator")
self.assertEqual(self.collaborator_3_3._get_unit(), self.unit_3)
self.assertEqual(self.collaborator_3_3.unit_id, self.unit_3)

with self.assertRaises(AccessError):
self.collaborator_3_3._get_unit_members()
with self.assertRaises(AccessError):
self.collaborator_3_3._get_unit_managers()
with self.assertRaises(AccessError):
self.collaborator_3_3._get_unit_collaborators()
self.assertFalse(self.collaborator_3_3.member_ids)
self.assertFalse(self.collaborator_3_3.manager_ids)
self.assertFalse(self.collaborator_3_3.collaborator_ids)

0 comments on commit 0f56e58

Please sign in to comment.