Browse Source

[FIX] b_worker_status: is_worker computation in b_emc

pull/134/head
Rémy Taymans 4 years ago
parent
commit
3c9e2afe4a
  1. 37
      beesdoo_easy_my_coop/models/res_partner.py
  2. 43
      beesdoo_easy_my_coop/tests/test_res_partner.py
  3. 19
      beesdoo_worker_status/models/cooperative_status.py

37
beesdoo_easy_my_coop/models/res_partner.py

@ -1,3 +1,6 @@
# Copyright 2019-2020 Coop IT Easy SCRLfs
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
@ -9,6 +12,40 @@ class Partner(models.Model):
string="Confirmed presence to info session",
default=False,
)
is_worker = fields.Boolean(
compute="_is_worker",
search="_search_worker",
readonly=True,
related=""
)
@api.depends(
'share_ids',
'share_ids.share_product_id',
'share_ids.share_product_id.default_code',
'share_ids.share_number',
)
def _is_worker(self):
"""
Return True if the partner can participate tho the shift system.
This is defined on the share type.
"""
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 share_type:
rec.is_worker = share_type.allow_working
rec.worker_store = share_type.allow_working
else:
rec.is_worker = False
rec.worker_store = False
def _search_worker(self, operator, value):
return [('worker_store', operator, value)]
@api.constrains('child_eater_ids', 'parent_eater_id')
def _check_number_of_eaters(self):

43
beesdoo_easy_my_coop/tests/test_res_partner.py

@ -76,3 +76,46 @@ 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))
def test_is_worker_share_a(self):
"""
Test that a cooperator is a worker based on his share type.
"""
coop1 = self.env.ref(
"beesdoo_base.res_partner_cooperator_1_demo"
)
# Run computed field
coop1._is_worker()
self.assertEqual(coop1.is_worker, True)
def test_is_worker_share_b(self):
"""
Test that a cooperator is a worker based on his share type.
"""
coop2 = self.env.ref(
"beesdoo_base.res_partner_cooperator_2_demo"
)
# Run computed field
coop2._is_worker()
self.assertEqual(coop2.is_worker, False)
def test_search_worker(self):
"""
Test that the search function returns worker based on the
'is_worker' field.
"""
coop1 = self.env.ref(
"beesdoo_base.res_partner_cooperator_1_demo"
)
coop2 = self.env.ref(
"beesdoo_base.res_partner_cooperator_2_demo"
)
# Run computed field
coop1._is_worker()
coop2._is_worker()
workers = self.env["res.partner"].search([("is_worker", "=", True)])
self.assertIn(coop1, workers)
self.assertNotIn(coop2, workers)
workers = self.env["res.partner"].search([("is_worker", "=", False)])
self.assertNotIn(coop1, workers)
self.assertIn(coop2, workers)

19
beesdoo_worker_status/models/cooperative_status.py

@ -263,22 +263,3 @@ class CooperativeStatus(models.Model):
if not delta % self._period:
return today
return add_days_delta(today, self._period - (delta % self._period))
class ResPartner(models.Model):
_inherit = 'res.partner'
"""
Override is_worker definition
You need have subscribe to a A Share
"""
is_worker = fields.Boolean(compute="_is_worker", search="_search_worker", readonly=True, related="")
def _is_worker(self):
for rec in self:
rec.is_worker = rec.cooperator_type == 'share_a'
def _search_worker(self, operator, value):
if (operator == '=' and value) or (operator == '!=' and not value):
return [('cooperator_type', '=', 'share_a')]
else:
return [('cooperator_type', '!=', 'share_a')]
Loading…
Cancel
Save