|
| 1 | +""" |
| 2 | +User workflow machine |
| 3 | +===================== |
| 4 | +
|
| 5 | +This machine binds the events to the User model, the StateMachine is wrapped internally |
| 6 | +in the `User` class. |
| 7 | +
|
| 8 | +Demonstrates that multiple state machines can be used in the same model. |
| 9 | +
|
| 10 | +And that logic can be reused with listeners. |
| 11 | +
|
| 12 | +""" |
| 13 | + |
| 14 | +from dataclasses import dataclass |
| 15 | +from enum import Enum |
| 16 | + |
| 17 | +from statemachine import State |
| 18 | +from statemachine import StateMachine |
| 19 | +from statemachine.states import States |
| 20 | + |
| 21 | + |
| 22 | +class UserStatus(str, Enum): |
| 23 | + signup_incomplete = "SIGNUP_INCOMPLETE" |
| 24 | + signup_complete = "SIGNUP_COMPLETE" |
| 25 | + signup_rejected = "SIGNUP_REJECTED" |
| 26 | + operational_enabled = "OPERATIONAL_ENABLED" |
| 27 | + operational_disabled = "OPERATIONAL_DISABLED" |
| 28 | + operational_rescinded = "OPERATIONAL_RESCINDED" |
| 29 | + |
| 30 | + |
| 31 | +class UserExperience(str, Enum): |
| 32 | + basic = "BASIC" |
| 33 | + premium = "PREMIUM" |
| 34 | + |
| 35 | + |
| 36 | +@dataclass |
| 37 | +class User: |
| 38 | + name: str |
| 39 | + email: str |
| 40 | + status: UserStatus = UserStatus.signup_incomplete |
| 41 | + experience: UserExperience = UserExperience.basic |
| 42 | + |
| 43 | + verified: bool = False |
| 44 | + |
| 45 | + def __post_init__(self): |
| 46 | + self._status_sm = UserStatusMachine( |
| 47 | + self, state_field="status", listeners=[MachineChangeListenter()] |
| 48 | + ) |
| 49 | + self._status_sm.bind_events_to(self) |
| 50 | + |
| 51 | + self._experience_sm = UserExperienceMachine( |
| 52 | + self, state_field="experience", listeners=[MachineChangeListenter()] |
| 53 | + ) |
| 54 | + self._experience_sm.bind_events_to(self) |
| 55 | + |
| 56 | + |
| 57 | +class MachineChangeListenter: |
| 58 | + def before_transition(self, event: str, state: State): |
| 59 | + print(f"Before {event} in {state}") |
| 60 | + |
| 61 | + def on_enter_state(self, state: State, event: str): |
| 62 | + print(f"Entering {state} from {event}") |
| 63 | + |
| 64 | + |
| 65 | +class UserStatusMachine(StateMachine): |
| 66 | + _states = States.from_enum( |
| 67 | + UserStatus, |
| 68 | + initial=UserStatus.signup_incomplete, |
| 69 | + final=[ |
| 70 | + UserStatus.operational_rescinded, |
| 71 | + UserStatus.signup_rejected, |
| 72 | + ], |
| 73 | + ) |
| 74 | + |
| 75 | + signup = _states.signup_incomplete.to(_states.signup_complete) |
| 76 | + reject = _states.signup_rejected.from_( |
| 77 | + _states.signup_incomplete, |
| 78 | + _states.signup_complete, |
| 79 | + ) |
| 80 | + enable = _states.signup_complete.to(_states.operational_enabled) |
| 81 | + disable = _states.operational_enabled.to(_states.operational_disabled) |
| 82 | + rescind = _states.operational_rescinded.from_( |
| 83 | + _states.operational_enabled, |
| 84 | + _states.operational_disabled, |
| 85 | + ) |
| 86 | + |
| 87 | + def on_signup(self, token: str): |
| 88 | + if token == "": |
| 89 | + raise ValueError("Token is required") |
| 90 | + self.model.verified = True |
| 91 | + |
| 92 | + |
| 93 | +class UserExperienceMachine(StateMachine): |
| 94 | + _states = States.from_enum( |
| 95 | + UserExperience, |
| 96 | + initial=UserExperience.basic, |
| 97 | + ) |
| 98 | + |
| 99 | + upgrade = _states.basic.to(_states.premium) |
| 100 | + downgrade = _states.premium.to(_states.basic) |
| 101 | + |
| 102 | + |
| 103 | +# %% |
| 104 | +# Executing |
| 105 | + |
| 106 | + |
| 107 | +def main(): # type: ignore[attr-defined] |
| 108 | + # By binding the events to the User model, the events can be fired directly from the model |
| 109 | + user = User(name="Frodo", email="frodo@lor.com") |
| 110 | + |
| 111 | + try: |
| 112 | + # Trying to signup with an empty token should raise an exception |
| 113 | + user.signup("") |
| 114 | + except Exception as e: |
| 115 | + print(e) |
| 116 | + |
| 117 | + assert user.verified is False |
| 118 | + |
| 119 | + user.signup("1234") |
| 120 | + |
| 121 | + assert user.status == UserStatus.signup_complete |
| 122 | + assert user.verified is True |
| 123 | + |
| 124 | + print(user.experience) |
| 125 | + user.upgrade() |
| 126 | + print(user.experience) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + main() |
0 commit comments