Finite state machine workflows is the declarative way to describe consecutive operation through set of states and transitions between them.
viewflow.fsm
can help you manage rules and restrictions around moving
from one state to another. The package could be used to get low level
db-independent fsm implementation, or to wrap existing database model, and
implement simple persistent workflow process with quickly bootstrapped UI.
All things are buit around viewflow.fsm.State
. It is the special class
slot, that can take a value only from a specific python enum or django
enumeration type and that value can’t be changed with simple assignement.
from enum import Enum
from viewflow.fsm import State
class Stage(Enum):
NEW = 1
DONE = 2
HIDDEN = 3
class MyFlow(object):
state = State(Stage, default=Stage.NEW)
@state.transition(source=Stage.NEW, target=Stage.DONE)
def complete():
pass
@state.transition(source=State.ANY, target=Stage.HIDDEN)
def hide():
pass
flow = MyFlow()
flow.state == Stage.NEW # True
flow.state = Stage.DONE # Raises AttributeError
flow.complete()
flow.state == Stage.DONE # True
flow.complete() # Now raises TransitionNotAllowed
Finite state machine could be seen as sort of dynamic typing. The
transition()
decorator augments the code with
runtime checks to prevent methods to be executed in a wrong order.