From 225435135a5ffd53c71ab9252335bf59d463755a Mon Sep 17 00:00:00 2001 From: Saran440 Date: Tue, 24 Oct 2023 12:01:07 +0700 Subject: [PATCH] [FIX] base_substate: track template on substate --- base_substate/models/__init__.py | 1 + base_substate/models/base_substate_mixin.py | 17 -------------- base_substate/models/mail_thread.py | 25 +++++++++++++++++++++ base_substate/tests/sale_test.py | 2 +- base_substate/tests/test_base_substate.py | 12 ++++++++++ 5 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 base_substate/models/mail_thread.py diff --git a/base_substate/models/__init__.py b/base_substate/models/__init__.py index 6ac2cf86a3..da826df400 100644 --- a/base_substate/models/__init__.py +++ b/base_substate/models/__init__.py @@ -1,2 +1,3 @@ from . import base_substate from . import base_substate_mixin +from . import mail_thread diff --git a/base_substate/models/base_substate_mixin.py b/base_substate/models/base_substate_mixin.py index e3b98c0415..c823cf076f 100644 --- a/base_substate/models/base_substate_mixin.py +++ b/base_substate/models/base_substate_mixin.py @@ -28,23 +28,6 @@ def check_substate_id_value(self): } ) - def _track_template(self, tracking): - res = super()._track_template(tracking) - first_rec = self[0] - changes, tracking_value_ids = tracking[first_rec.id] - if "substate_id" in changes and first_rec.substate_id.mail_template_id: - res["substate_id"] = ( - first_rec.substate_id.mail_template_id, - { - "auto_delete_message": True, - "subtype_id": self.env["ir.model.data"].xmlid_to_res_id( - "mail.mt_note" - ), - "notif_layout": "mail.mail_notification_light", - }, - ) - return res - def _get_default_substate_id(self, state_val=False): """Gives default substate_id""" search_domain = self._get_default_substate_domain(state_val) diff --git a/base_substate/models/mail_thread.py b/base_substate/models/mail_thread.py new file mode 100644 index 0000000000..017a9bb4cc --- /dev/null +++ b/base_substate/models/mail_thread.py @@ -0,0 +1,25 @@ +# Copyright 2023 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + def _track_template(self, changes): + res = super()._track_template(changes) + first_rec = self[0] + if "substate_id" in changes and first_rec.substate_id.mail_template_id: + res["substate_id"] = ( + first_rec.substate_id.mail_template_id, + { + "composition_mode": "comment", + "auto_delete_message": True, + "subtype_id": self.env["ir.model.data"]._xmlid_to_res_id( + "mail.mt_note" + ), + "email_layout_xmlid": "mail.mail_notification_light", + }, + ) + return res diff --git a/base_substate/tests/sale_test.py b/base_substate/tests/sale_test.py index c3d80cc07d..85f05829ad 100644 --- a/base_substate/tests/sale_test.py +++ b/base_substate/tests/sale_test.py @@ -6,8 +6,8 @@ class SaleTest(models.Model, TestMixin): - _inherit = "base.substate.mixin" _name = "base.substate.test.sale" + _inherit = ["mail.thread", "base.substate.mixin"] _description = "Base substate Test Model" name = fields.Char(required=True) diff --git a/base_substate/tests/test_base_substate.py b/base_substate/tests/test_base_substate.py index 57d23316bd..c1c2069cb8 100644 --- a/base_substate/tests/test_base_substate.py +++ b/base_substate/tests/test_base_substate.py @@ -17,6 +17,14 @@ def setUpClass(cls): cls.substate_test_sale = cls.env["base.substate.test.sale"] cls.substate_test_sale_line = cls.env["base.substate.test.sale.line"] + cls.mail_template = cls.env["mail.template"].create( + { + "name": "Waiting for legal documents", + "model_id": cls.substate_test_sale.id, + "subject": "Test Email Substate", + } + ) + cls.base_substate = cls.env["base.substate.mixin"] cls.substate_type = cls.env["base.substate.type"] @@ -68,6 +76,7 @@ def setUpClass(cls): "name": "Waiting for legal documents", "sequence": 2, "target_state_value_id": cls.substate_val_sale.id, + "mail_template_id": cls.mail_template.id, } ) @@ -111,6 +120,9 @@ def test_sale_order_substate(self): so_test1.button_confirm() self.assertTrue(so_test1.state == "sale") self.assertTrue(so_test1.substate_id == self.substate_wait_docs) + # Test track template + res = so_test1._track_template({"substate_id"}) + self.assertEqual(res["substate_id"][0], self.mail_template) # Test that substate_id is set to false if # there is not substate corresponding to state