Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][FIX] base_substate: track template on substate #735

Open
wants to merge 1 commit into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions base_substate/models/base_substate_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -116,6 +99,19 @@ def _update_before_write_create(self, values):
if values.get(state_field) and not values.get("substate_id"):
state_val = values.get(state_field)
values["substate_id"] = self._get_default_substate_id(state_val)
# Send mail if substate has mail template
if values.get("substate_id"):
substate = self.env["base.substate"].browse(values["substate_id"])
if (
hasattr(self, "message_post_with_template")
and substate.mail_template_id
):
self.message_post_with_template(
substate.mail_template_id.id,
subtype_id=self.env["ir.model.data"]._xmlid_to_res_id(
"mail.mt_note"
),
)
return values

def write(self, values):
Expand Down
2 changes: 1 addition & 1 deletion base_substate/tests/sale_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@


class SaleTest(models.Model, TestMixin):
_inherit = "base.substate.mixin"
_name = "base.substate.test.sale"
_inherit = ["base.substate.mixin", "mail.thread"]
_description = "Base substate Test Model"

name = fields.Char(required=True)
Expand Down
18 changes: 16 additions & 2 deletions base_substate/tests/test_base_substate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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,
}
)

Expand Down Expand Up @@ -106,12 +115,17 @@ def test_sale_order_substate(self):
)
self.assertTrue(so_test1.state == "draft")
self.assertTrue(so_test1.substate_id == self.substate_under_negotiation)

self.assertNotIn(
self.mail_template.subject, so_test1.message_ids.mapped("subject")
)
# Test that validation of sale order change substate_id
so_test1.button_confirm()
self.assertTrue(so_test1.state == "sale")
self.assertTrue(so_test1.substate_id == self.substate_wait_docs)

# Check some message_ids are created and sent email
self.assertIn(
self.mail_template.subject, so_test1.message_ids.mapped("subject")
)
# Test that substate_id is set to false if
# there is not substate corresponding to state
so_test1.button_cancel()
Expand Down
Loading