diff --git a/beesdoo_easy_my_coop/models/res_partner.py b/beesdoo_easy_my_coop/models/res_partner.py index 1088674..b5eaf72 100644 --- a/beesdoo_easy_my_coop/models/res_partner.py +++ b/beesdoo_easy_my_coop/models/res_partner.py @@ -80,31 +80,37 @@ class Partner(models.Model): if rec.cooperative_status_ids else False ) - @api.constrains('child_eater_ids', 'parent_eater_id') - def _check_number_of_eaters(self): + @api.constrains('parent_eater_id') + def _check_max_parent_eaters(self): + """ + Check that the parent_eater_id in parnter in self doesn't exceed + the maximum eater limit. + See also: _check_max_child_eaters() + """ + for rec in self: + if rec.parent_eater_id: + share_type = rec.parent_eater_id._cooperator_share_type() + if ( + share_type + and share_type.max_nb_eater_allowed >= 0 + and len( + rec.parent_eater_id.child_eater_ids + ) > share_type.max_nb_eater_allowed + ): + raise ValidationError( + _('You can only set %d additional eaters per worker') + % share_type.max_nb_eater_allowed + ) + + @api.constrains('child_eater_ids') + def _check_max_child_eaters(self): """ Check the maximum number of eaters that can be assigned to a share owner. + See also: _check_max_parent_eaters() """ for rec in self: - share_type = None - if rec.cooperator_type: - share_type = ( - self.env['product.template'] - .search([('default_code', '=', rec.cooperator_type)]) - )[0] - # If the current partner owns no share, check his parent. - if not share_type: - share_type = ( - self.env['product.template'] - .search([ - ( - 'default_code', - '=', - rec.parent_eater_id.cooperator_type - ) - ]) - )[0] + share_type = rec._cooperator_share_type() if ( share_type and share_type.max_nb_eater_allowed >= 0 diff --git a/beesdoo_easy_my_coop/tests/test_res_partner.py b/beesdoo_easy_my_coop/tests/test_res_partner.py index 66bc5d3..fd42b64 100644 --- a/beesdoo_easy_my_coop/tests/test_res_partner.py +++ b/beesdoo_easy_my_coop/tests/test_res_partner.py @@ -31,6 +31,19 @@ class TestResPartner(TransactionCase): with self.assertRaises(ValidationError) as econtext: coop1.write({"child_eater_ids": [(4, self.eater4.id)]}) self.assertIn("can only set", str(econtext.exception)) + # Reset + coop1.write({"child_eater_ids": [(5, None, None)]}) + self.assertEqual(len(coop1.child_eater_ids), 0) + # Test by editing parent_eater_id + self.eater1.write({"parent_eater_id": coop1.id}) + self.assertEqual(len(coop1.child_eater_ids), 1) + self.eater2.write({"parent_eater_id": coop1.id}) + self.assertEqual(len(coop1.child_eater_ids), 2) + self.eater3.write({"parent_eater_id": coop1.id}) + self.assertEqual(len(coop1.child_eater_ids), 3) + with self.assertRaises(ValidationError) as econtext: + self.eater4.write({"parent_eater_id": coop1.id}) + self.assertIn("can only set", str(econtext.exception)) def test_max_eater_assignment_share_b(self): """ @@ -47,6 +60,17 @@ class TestResPartner(TransactionCase): with self.assertRaises(ValidationError) as econtext: coop2.write({"child_eater_ids": [(4, self.eater3.id)]}) self.assertIn("can only set", str(econtext.exception)) + # Reset + coop2.write({"child_eater_ids": [(5, None, None)]}) + self.assertEqual(len(coop2.child_eater_ids), 0) + # Test by editing parent_eater_id + self.eater1.write({"parent_eater_id": coop2.id}) + self.assertEqual(len(coop2.child_eater_ids), 1) + self.eater2.write({"parent_eater_id": coop2.id}) + self.assertEqual(len(coop2.child_eater_ids), 2) + with self.assertRaises(ValidationError) as econtext: + self.eater3.write({"parent_eater_id": coop2.id}) + self.assertIn("can only set", str(econtext.exception)) def test_unlimited_eater_assignment_share_c(self): """ @@ -76,6 +100,9 @@ class TestResPartner(TransactionCase): with self.assertRaises(ValidationError) as econtext: coop3.write({"child_eater_ids": [(4, self.eater3.id)]}) self.assertIn("can only set", str(econtext.exception)) + with self.assertRaises(ValidationError) as econtext: + self.eater1.write({"parent_eater_id": coop3.id}) + self.assertIn("can only set", str(econtext.exception)) def test_multiple_eater_assignement_share_a(self): """