Pure python workflow/transition system.
Project description
This package allows to define workflows that consist of a set of states, transitions between the states. Actions that activate the transitions can be guarded by constraints.
Example
from roughrider.predicate import ConstraintError, Validator, Or
from roughrider.workflow.components import Action, Transition, Transitions
from roughrider.workflow.workflow import (
WorkflowItem, WorkflowState, Workflow)
class Document:
state = None
body = ""
class RoleValidator(Validator):
def __init__(self, role):
self.role = role
def __call__(self, item, role=None, **namespace):
if role != self.role:
raise ConstraintError(
message=f'Unauthorized. Missing the `{role}` role.')
class PublicationWorkflow(Workflow):
class wrapper(WorkflowItem):
@property
def state(self):
return self.workflow.get(self.item.state)
@state.setter
def state(self, state):
self.item.state = state.name
class states(WorkflowState):
draft = 'Draft'
published = 'Published'
submitted = 'Submitted'
transitions = Transitions((
Transition(
origin=states.draft,
target=states.published,
action=Action(
'Publish',
constraints=[RoleValidator('publisher')]
)
),
Transition(
origin=states.published,
target=states.draft,
action=Action(
'Retract',
constraints=[
Or((RoleValidator('owner'),
RoleValidator('publisher')))
]
)
),
Transition(
origin=states.draft,
target=states.submitted,
action=Action(
'Submit',
constraints=[RoleValidator('owner')],
)
),
Transition(
origin=states.submitted,
target=states.published,
action=Action(
'Publish',
constraints=[RoleValidator('publisher')],
)
)
))
workflow = PublicationWorkflow('draft') # initial state
item = Document()
workflow_item = workflow(item, role='owner')
workflow_item.transition_to(PublicationWorkflow.states.submitted)
CHANGES
0.2 (2021-10-21)
Updating to use roughrider.predicate >= 0.3.1
0.1 (2021-10-09)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
roughrider.workflow-0.2.tar.gz
(13.0 kB
view details)
File details
Details for the file roughrider.workflow-0.2.tar.gz
.
File metadata
- Download URL: roughrider.workflow-0.2.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 585765d091b7cad7872bb8dafb73beb9e5c83b415c7b62808ef734094a709a53 |
|
MD5 | 4d3ef99029975c327e7a1674bcf2b576 |
|
BLAKE2b-256 | bd7735806ce8be47cd78fdaf302757eee1fd6a7046c8a003b4d31aae1da73a83 |