Skip to content

Commit

Permalink
[FIX] crm_won_restrict_per_stage: Add ValidationError to onchange to …
Browse files Browse the repository at this point in the history
…prevent display incorrect UX changes

TT49558
  • Loading branch information
victoralmau committed Jun 10, 2024
1 parent dbdad60 commit 62dba28
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
19 changes: 18 additions & 1 deletion crm_won_restrict_per_stage/models/crm_lead.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2024 Tecnativa - Carolina Fernandez
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from odoo import _, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


Expand All @@ -10,6 +10,23 @@ class CrmStage(models.Model):

show_won_button = fields.Boolean(related="stage_id.show_won_button")

@api.onchange("stage_id")
def _onchange_stage_id(self):
"""Do it this way for avoiding a UI glitch if we let act
the exception in the write, as the stage is changed in the
interface, but the message appears and the record is kept
unsaved.
"""
for item in self:
if (
item != item._origin
and item.stage_id.is_won
and not item._origin.stage_id.show_won_button
):
raise ValidationError(
_("You can't change to this stage from the current stage.")
)

def write(self, vals):
for rec in self:
if vals.get("stage_id"):
Expand Down
9 changes: 8 additions & 1 deletion crm_won_restrict_per_stage/tests/test_crm_lead.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo.exceptions import ValidationError
from odoo.tests import Form
from odoo.tests.common import TransactionCase


Expand All @@ -13,11 +14,17 @@ def setUpClass(cls):
cls.stage_new = cls.env.ref("crm.stage_lead1")
cls.stage_won = cls.env.ref("crm.stage_lead4")

def test_change_crm_stage_won_without_show_button(self):
def test_change_crm_stage_won_without_show_button_01(self):
self.stage_new.show_won_button = False
with self.assertRaises(ValidationError):
self.crm_lead.stage_id = self.stage_won

def test_change_crm_stage_won_without_show_button_02(self):
self.stage_new.show_won_button = False
lead_form = Form(self.crm_lead)
with self.assertRaises(ValidationError):
lead_form.stage_id = self.stage_won

def test_change_crm_stage_to_won_with_show_button(self):
self.stage_new.show_won_button = True
self.crm_lead.stage_id = self.stage_won
Expand Down

0 comments on commit 62dba28

Please sign in to comment.