Skip to content

Commit

Permalink
[FIX] fieldservice_stage_server_action: run server action on stage ch…
Browse files Browse the repository at this point in the history
…ange
  • Loading branch information
yankinmax committed Sep 18, 2024
1 parent bb8cdeb commit 82ebfa5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions fieldservice_stage_server_action/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import fsm_stage
from . import fsm_order
31 changes: 31 additions & 0 deletions fieldservice_stage_server_action/models/fsm_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, models


class FSMOrder(models.Model):
_inherit = "fsm.order"

@api.model_create_multi
def create(self, vals_list):
orders = super().create(vals_list)
orders._run_stage_server_action()
return orders

def write(self, vals):
res = super().write(vals)
if "stage_id" in vals:
self._run_stage_server_action()
return res

def _run_stage_server_action(self):
for order in self:
action_id = order.stage_id.action_id
if not action_id:
continue
ctx = {
"active_model": self._name,
"active_id": order.id,
}
action_id.with_context(**ctx).run()
1 change: 1 addition & 0 deletions fieldservice_stage_server_action/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_fsm_order_run_action
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.tests.common import TransactionCase, RecordCapturer


class TestFSMOrderRunAction(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.Order = cls.env["fsm.order"]
cls.Tag = cls.env["fsm.tag"]
cls.test_location = cls.env.ref("fieldservice.test_location")
cls.stage1 = cls.env.ref("fieldservice.fsm_stage_completed")
cls.stage2 = cls.env.ref("fieldservice.fsm_stage_cancelled")
cls.location_1 = cls.env.ref("fieldservice.location_1")
# cls.tag = cls.env["fsm.tag"].create({"name": "Test Tag"})
# cls.tag2 = cls.env["fsm.tag"].create({"name": "Test Tag2"})
cls.create_action = cls.env["ir.actions.server"].create({
"model_id": cls.env["ir.model"]._get_id("fsm.tag"),
"crud_model_id": cls.env["ir.model"]._get_id("fsm.tag"),
"name": "Create new tag",
"value": "New test tag",
"state": "object_create",
})
cls.stage2.action_id = cls.create_action

def test_fsm_order_run_action(self):
order = self.Order.create(
{
"location_id": self.test_location.id,
"stage_id": self.stage1.id,
}
)
self.assertFalse(self.Tag.search([("name", "=", "New test tag")]).exists())
with RecordCapturer(self.Tag, []) as capture:
order.write({"stage_id": self.stage2.id})
tag = capture.records
self.assertEqual(1, len(tag))
self.assertEqual("New test tag", tag.name)

0 comments on commit 82ebfa5

Please sign in to comment.