Skip to content

Commit c21bb32

Browse files
committed
[IMP] stachechat_mixin: make it possible to ignore some events in the has_allowed_event compute
1 parent c8a3f2a commit c21bb32

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

statechart/models/statechart_mixin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,10 @@ class can override the statechart with another one adding new events,
315315

316316
@api.model
317317
def _get_sc_event_allowed_field_names(self):
318+
ignore_for_has_allowed_events = self.env.context.get("ignore_for_has_allowed_events", [])
318319
event_names = self._statechart.events_for()
319320
return [
320-
_sc_make_event_allowed_field_name(event_name) for event_name in event_names
321+
_sc_make_event_allowed_field_name(event_name) for event_name in event_names if event_name not in ignore_for_has_allowed_events
321322
]
322323

323324
@api.depends("sc_state")

test_statechart/models/test_statechart.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ statechart:
88
transitions:
99
- event: confirm1
1010
target: confirmed1
11+
- target: canceled
12+
event: cancel
1113
- name: confirmed1
1214
transitions:
1315
- target: confirmed2
1416
guard: o.amount < 100
1517
- target: confirmed2
1618
event: confirm2
19+
- target: canceled
20+
event: cancel
1721
- name: confirmed2
22+
- target: canceled
23+
event: cancel
24+
- name: canceled

test_statechart/tests/test_basic.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,37 @@ def test_automatic_transition(self):
3737
record.confirm1()
3838
# small amount, step 2 done automatically
3939
self.assertScState(record.sc_state, ["confirmed2", "root"])
40+
41+
def test_get_sc_event_allowed_field_names(self):
42+
"""
43+
Test that we can ignore certain events based on a context
44+
key (ignore_for_has_allowed_events)
45+
"""
46+
model = self.env["scobidoo.test.model"]
47+
record = model.create({"amount": 200})
48+
record.confirm1()
49+
self.assertScState(record.sc_state, ["confirmed1", "root"])
50+
51+
52+
self.assertEqual(record.sc_has_allowed_events, True)
53+
54+
def test_get_sc_event_allowed_field_names(self):
55+
"""
56+
Test that we can ignore certain events based on a context
57+
key (ignore_for_has_allowed_events)
58+
"""
59+
model = self.env["scobidoo.test.model"]
60+
record = model.create({"amount": 200})
61+
record.confirm1()
62+
self.assertScState(record.sc_state, ["confirmed1", "root"])
63+
64+
# 2 events are allowed: confirmed2 and cancel
65+
self.assertEqual(record.sc_has_allowed_events, True)
66+
67+
# 1 event is allowed: confirmed2
68+
record.invalidate_cache()
69+
self.assertEqual(record.with_context(ignore_for_has_allowed_events=["cancel"]).sc_has_allowed_events, True)
70+
71+
# no event allowed
72+
record.invalidate_cache()
73+
self.assertEqual(record.with_context(ignore_for_has_allowed_events=["cancel", "confirm2"]).sc_has_allowed_events, False)

0 commit comments

Comments
 (0)