Skip to content
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
7 changes: 4 additions & 3 deletions statechart/models/statechart_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,16 @@ class can override the statechart with another one adding new events,
return res

@api.model
def _get_sc_event_allowed_field_names(self):
event_names = self._statechart.events_for()
def _get_sc_event_allowed_field_names(self, events_to_ignore=None):
event_names = set(self._statechart.events_for()) - set(events_to_ignore or [])
return [
_sc_make_event_allowed_field_name(event_name) for event_name in event_names
]

@api.depends("sc_state")
def _compute_sc_has_allowed_events(self):
sc_fields = self._get_sc_event_allowed_field_names()
events_to_ignore = self.env.context.get("ignore_for_has_allowed_events")
sc_fields = self._get_sc_event_allowed_field_names(events_to_ignore)
for rec in self:
rec.sc_has_allowed_events = any([rec[f] for f in sc_fields])

Expand Down
8 changes: 8 additions & 0 deletions test_statechart/models/test_statechart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ statechart:
target: confirmed1
- target: confirmed1
guard: o.amount < 1
- target: canceled
event: cancel
- name: confirmed1
transitions:
- target: confirmed2
guard: o.amount < 100
- target: confirmed2
event: confirm2
- target: canceled
event: cancel
- name: confirmed2
transitions:
- target: canceled
event: cancel
- name: canceled
31 changes: 31 additions & 0 deletions test_statechart/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,34 @@ def test_automatic_transition_on_create(self):
record = model.create({"amount": 0.5})
# very small amount, step 1 and 2 done automatically
self.assertScState(record.sc_state, ["confirmed2", "root"])

def test_get_sc_sc_has_allowed_events(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_recordset()
self.assertEqual(
record.with_context(
ignore_for_has_allowed_events=["cancel"]
).sc_has_allowed_events,
True,
)

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