Browse Source

[FIX] b_base, b_emc: writing several eater at once

pull/134/head
Rémy Taymans 5 years ago
parent
commit
d6b45f1fa2
  1. 9
      beesdoo_base/models/partner.py
  2. 14
      beesdoo_easy_my_coop/models/res_partner.py
  3. 31
      beesdoo_easy_my_coop/tests/test_res_partner.py

9
beesdoo_base/models/partner.py

@ -28,8 +28,13 @@ class Partner(models.Model):
@api.multi @api.multi
def write(self, values): def write(self, values):
if values.get('parent_eater_id') and self.parent_eater_id:
raise ValidationError(_('You try to assign a eater to a worker but this easer is alread assign to %s please remove it before') % self.parent_eater_id.name)
for rec in self:
if (
values.get('parent_eater_id')
and rec.parent_eater_id
and rec.parent_eater_id.id != values.get("parent_eater_id")
):
raise ValidationError(_('You try to assign a eater to a worker but this eater is already assign to %s please remove it before') % rec.parent_eater_id.name)
# replace many2many command when writing on child_eater_ids to just remove the link # replace many2many command when writing on child_eater_ids to just remove the link
if 'child_eater_ids' in values: if 'child_eater_ids' in values:
for command in values['child_eater_ids']: for command in values['child_eater_ids']:

14
beesdoo_easy_my_coop/models/res_partner.py

@ -53,25 +53,29 @@ class Partner(models.Model):
Check the maximum number of eaters that can be assigned to a Check the maximum number of eaters that can be assigned to a
share owner. share owner.
""" """
self.ensure_one()
for rec in self:
share_type = None share_type = None
if self.cooperator_type:
if rec.cooperator_type:
share_type = ( share_type = (
self.env['product.template'] self.env['product.template']
.search([('default_code', '=', self.cooperator_type)])
.search([('default_code', '=', rec.cooperator_type)])
)[0] )[0]
# If the current partner owns no share, check his parent. # If the current partner owns no share, check his parent.
if not share_type: if not share_type:
share_type = ( share_type = (
self.env['product.template'] self.env['product.template']
.search([ .search([
('default_code', '=', self.parent_eater_id.cooperator_type)
(
'default_code',
'=',
rec.parent_eater_id.cooperator_type
)
]) ])
)[0] )[0]
if ( if (
share_type share_type
and share_type.max_nb_eater_allowed >= 0 and share_type.max_nb_eater_allowed >= 0
and len(self.child_eater_ids) > share_type.max_nb_eater_allowed
and len(rec.child_eater_ids) > share_type.max_nb_eater_allowed
): ):
raise ValidationError( raise ValidationError(
_('You can only set %d additional eaters per worker') _('You can only set %d additional eaters per worker')

31
beesdoo_easy_my_coop/tests/test_res_partner.py

@ -77,6 +77,37 @@ class TestResPartner(TransactionCase):
coop3.write({"child_eater_ids": [(4, self.eater3.id)]}) coop3.write({"child_eater_ids": [(4, self.eater3.id)]})
self.assertIn("can only set", str(econtext.exception)) self.assertIn("can only set", str(econtext.exception))
def test_multiple_eater_assignement_share_a(self):
"""
Test adding multiple eater in one write.
"""
coop1 = self.env.ref(
"beesdoo_base.res_partner_cooperator_1_demo"
)
coop1.write(
{
"child_eater_ids": [
(4, self.eater1.id),
(4, self.eater2.id),
(4, self.eater3.id),
]
}
)
self.assertEqual(len(coop1.child_eater_ids), 3)
def test_parent_assignement_to_eater(self):
"""
Test adding a parent to multiple eater in one write from the eater.
"""
coop1 = self.env.ref(
"beesdoo_base.res_partner_cooperator_1_demo"
)
eaters = self.eater1
eaters |= self.eater2
eaters |= self.eater3
eaters.write({"parent_eater_id": coop1.id})
self.assertEqual(len(coop1.child_eater_ids), 3)
def test_is_worker_share_a(self): def test_is_worker_share_a(self):
""" """
Test that a cooperator is a worker based on his share type. Test that a cooperator is a worker based on his share type.

Loading…
Cancel
Save