Skip to content

Commit

Permalink
Merge PR #516 into 12.0
Browse files Browse the repository at this point in the history
Signed-off-by carmenbianca
  • Loading branch information
github-grap-bot committed Oct 9, 2023
2 parents 8a5f3da + ef91d91 commit 58cc6e5
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 8 deletions.
4 changes: 4 additions & 0 deletions cooperator_eater/tests/test_eaters.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def setUpClass(cls):
{"name": "Eater 4", "customer": True, "eater": "eater"}
)

cls.cooperator_x.eater = "worker_eater"
cls.cooperator_y.eater = "worker_eater"
cls.cooperator_z.eater = "worker_eater"

cls.worker_share = ptemplate_obj.create(
{
"name": "Worker Share",
Expand Down
2 changes: 1 addition & 1 deletion eater/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "BEES coop - Cellule IT, Coop IT Easy SC",
"website": "https://github.com/beescoop/Obeesdoo",
"category": "Sales",
"version": "12.0.1.0.0",
"version": "12.0.2.0.0",
"depends": ["base", "partner_firstname"],
"data": [
"wizard/new_eater_wizard_views.xml",
Expand Down
17 changes: 17 additions & 0 deletions eater/migrations/12.0.2.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
# This is an impossible scenario. It causes an endless loop bug that I'm not
# able to debug exactly, but it's easier to just get rid of this scenario.
sql = """
UPDATE res_partner
SET parent_eater_id = null
WHERE eater = 'worker_eater'
"""
openupgrade.logged_query(env.cr, sql)
33 changes: 30 additions & 3 deletions eater/models/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,39 @@ class Partner(models.Model):
child_eater_ids = fields.One2many(
"res.partner",
"parent_eater_id",
domain=[("customer", "=", True), ("eater", "=", "eater")],
domain=[("eater", "=", "eater")],
)
parent_eater_id = fields.Many2one(
"res.partner", string="Parent Worker", readonly=True
"res.partner",
string="Parent Worker",
readonly=True,
domain=[("eater", "=", "worker_eater")],
)

@api.constrains("eater", "parent_eater_id")
def _check_parent_is_worker(self):
"""The parent of an eater must be a worker_eater, and worker_eaters
cannot have parents.
"""
for partner in self:
parent = partner.parent_eater_id
if partner.eater == "eater" and parent:
if parent.eater != "worker_eater":
raise ValidationError(
_(
"{0} cannot be the parent of {1} because the parent"
" must be a worker."
).format(parent.name, partner.name)
)
if partner.eater == "worker_eater" and parent:
raise ValidationError(
_(
"%s cannot have a parent worker because they are"
" themselves a worker."
)
% partner.name
)

@api.multi
def write(self, values):
for partner in self:
Expand All @@ -42,7 +69,7 @@ def write(self, values):
for command in values["child_eater_ids"]:
if command[0] == 2:
command[0] = 3
return super(Partner, self).write(values)
return super().write(values)

@api.multi
def _new_eater(self, surname, name, email):
Expand Down
1 change: 1 addition & 0 deletions eater/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_partner
55 changes: 55 additions & 0 deletions eater/tests/test_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from odoo.exceptions import ValidationError
from odoo.tests.common import SavepointCase


class TestPartner(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.worker_1 = cls.env["res.partner"].create(
{
"name": "Worker 1",
"eater": "worker_eater",
}
)
cls.worker_2 = cls.env["res.partner"].create(
{
"name": "Worker 2",
"eater": "worker_eater",
}
)
cls.eater_1 = cls.env["res.partner"].create(
{
"name": "Eater 1",
"eater": "eater",
}
)
cls.eater_2 = cls.env["res.partner"].create(
{
"name": "Eater 2",
"eater": "eater",
}
)

def test_eater_not_parent_of_worker_eater(self):
with self.assertRaises(ValidationError):
self.worker_1.parent_eater_id = self.eater_1

def test_worker_eater_not_child_of_eater(self):
with self.assertRaises(ValidationError):
self.eater_1.child_eater_ids = self.worker_1

def test_eater_not_parent_of_eater(self):
with self.assertRaises(ValidationError):
self.eater_1.parent_eater_id = self.eater_2

def test_worker_eater_no_parent(self):
with self.assertRaises(ValidationError):
self.worker_2.parent_eater_id = self.worker_1

def test_no_parent_of_self(self):
with self.assertRaises(ValidationError):
self.worker_2.parent_eater_id = self.worker_2

def test_worker_eater_parent_of_eater(self):
self.eater_1.parent_eater_id = self.worker_1
12 changes: 8 additions & 4 deletions eater/views/partner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
<field name="arch" type="xml">

<xpath expr="//notebook" position="inside">
<page
string="Eaters"
attrs="{'invisible': ['|', ('customer', '=', False), ('eater', 'not in', ('worker_eater'))]}"
>
<page string="Eaters">
<group>
<group>
<field
Expand All @@ -20,6 +17,12 @@
widget="many2many_tags"
options="{'no_create': True}"
context="{'default_eater' : 'eater', 'default_customer' : True}"
attrs="{'invisible': [('eater', '=', 'eater')]}"
/>
<field
name="parent_eater_id"
options="{'no_create': True}"
attrs="{'invisible': [('eater', '=', 'worker_eater')]}"
/>
</group>
<group>
Expand All @@ -28,6 +31,7 @@
name="%(new_eater_wizard_action)d"
type="action"
class="btn btn-primary"
attrs="{'invisible': [('eater', '=', 'eater')]}"
/>
</group>
</group>
Expand Down

0 comments on commit 58cc6e5

Please sign in to comment.