Skip to content

Commit

Permalink
[IMP] stachechat_mixin: make it possible to ignore some events in the…
Browse files Browse the repository at this point in the history
… has_allowed_event compute
  • Loading branch information
jdoutreloux committed Apr 24, 2024
1 parent c8a3f2a commit c21bb32
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion statechart/models/statechart_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,10 @@ class can override the statechart with another one adding new events,

@api.model
def _get_sc_event_allowed_field_names(self):
ignore_for_has_allowed_events = self.env.context.get("ignore_for_has_allowed_events", [])
event_names = self._statechart.events_for()
return [
_sc_make_event_allowed_field_name(event_name) for event_name in event_names
_sc_make_event_allowed_field_name(event_name) for event_name in event_names if event_name not in ignore_for_has_allowed_events
]

@api.depends("sc_state")
Expand Down
7 changes: 7 additions & 0 deletions test_statechart/models/test_statechart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ statechart:
transitions:
- event: confirm1
target: confirmed1
- target: canceled
event: cancel
- name: confirmed1
transitions:
- target: confirmed2
guard: o.amount < 100
- target: confirmed2
event: confirm2
- target: canceled
event: cancel
- name: confirmed2
- target: canceled
event: cancel
- name: canceled
34 changes: 34 additions & 0 deletions test_statechart/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,37 @@ def test_automatic_transition(self):
record.confirm1()
# small amount, step 2 done automatically
self.assertScState(record.sc_state, ["confirmed2", "root"])

def test_get_sc_event_allowed_field_names(self):
"""
Test that we can ignore certain events based on a context
key (ignore_for_has_allowed_events)
"""
model = self.env["scobidoo.test.model"]
record = model.create({"amount": 200})
record.confirm1()
self.assertScState(record.sc_state, ["confirmed1", "root"])


self.assertEqual(record.sc_has_allowed_events, True)

def test_get_sc_event_allowed_field_names(self):
"""
Test that we can ignore certain events based on a context
key (ignore_for_has_allowed_events)
"""
model = self.env["scobidoo.test.model"]
record = model.create({"amount": 200})
record.confirm1()
self.assertScState(record.sc_state, ["confirmed1", "root"])

# 2 events are allowed: confirmed2 and cancel
self.assertEqual(record.sc_has_allowed_events, True)

# 1 event is allowed: confirmed2
record.invalidate_cache()
self.assertEqual(record.with_context(ignore_for_has_allowed_events=["cancel"]).sc_has_allowed_events, True)

# no event allowed
record.invalidate_cache()
self.assertEqual(record.with_context(ignore_for_has_allowed_events=["cancel", "confirm2"]).sc_has_allowed_events, False)

0 comments on commit c21bb32

Please sign in to comment.