Functions are the description in Python
of what you want to do on certain events happen. So you define them once and you can use them
with multiple hooking up. Just go to /admin/river/function/
admin page and create your functions there.``django-river`` function admin support
python code highlighting as well if you enable the codemirror2
app. Don't forget to collect statics for production deployments.
INSTALLED_APPS=[ ... codemirror2 river ... ]
Here is an example function;
from datetime import datetime def handle(context): print(datetime.now())
Important: YOUR FUNCTION SHOULD BE NAMED AS handle
. Otherwise django-river
won't execute your function.
django-river
will pass a context
down to your function in order for you to know why the function is triggered or for which object or so. And the context
will look different for different type of events. But it also has some common parts for all the events. Let's look at how it looks;
context.hook ->>
Key | Type | Format | Description |
---|---|---|---|
type | String | * on-approved
* on-transit
* on-complete
|
The event type that is hooked up. The payload will
likely differ according to this value
|
when | String | * BEFORE
* AFTER
|
Whether it is hooked right before the event happens
or right after
|
payload | dict | This is the context content that will differ for each
event type. The information that can be gotten from
payload is describe in the table below
|
Key | Type | Description |
---|---|---|
workflow | Workflow Model | The workflow that the transition currently happening |
workflow_object | Your Workflow
Object
|
The workflow object of the model that has the state
field in it
|
transition_approval | Transition
Approval
|
The approval object that is currently approved which
contains the information of the transition(meta) as
well as who approved it etc.
|
Key | Type | Description |
---|---|---|
workflow | Workflow Model | The workflow that the transition currently happening |
workflow_object | Your Workflow
Object
|
The workflow object of the model that has the state
field in it
|
transition_approval | Transition
Approval
|
The last transition approval object which contains
the information of the transition(meta) as well as
who last approved it etc.
|
Key | Type | Description |
---|---|---|
workflow | Workflow Model | The workflow that the transition currently happening |
workflow_object | Your Workflow
Object
|
The workflow object of the model that has the state
field in it
|
from river.models.hook import BEFORE, AFTER def _handle_my_transitions(hook): workflow = hook['payload']['workflow'] workflow_object = hook['payload']['workflow_object'] source_state = hook['payload']['transition_approval'].meta.source_state destination_state = hook['payload']['transition_approval'].meta.destination_state last_approved_by = hook['payload']['transition_approval'].transactioner if hook['when'] == BEFORE: print('A transition from %s to %s will soon happen on the object with id:%s and field_name:%s!' % (source_state.label, destination_state.label, workflow_object.pk, workflow.field_name)) elif hook['when'] == AFTER: print('A transition from %s to %s has just happened on the object with id:%s and field_name:%s!' % (source_state.label, destination_state.label, workflow_object.pk, workflow.field_name)) print('Who approved it lately is %s' % last_approved_by.username) def _handle_my_approvals(hook): workflow = hook['payload']['workflow'] workflow_object = hook['payload']['workflow_object'] approved_by = hook['payload']['transition_approval'].transactioner if hook['when'] == BEFORE: print('An approval will soon happen by %s on the object with id:%s and field_name:%s!' % ( approved_by.username, workflow_object.pk, workflow.field_name )) elif hook['when'] == AFTER: print('An approval has just happened by %s on the object with id:%s and field_name:%s!' % ( approved_by.username, workflow_object.pk, workflow.field_name )) def _handle_completions(hook): workflow = hook['payload']['workflow'] workflow_object = hook['payload']['workflow_object'] if hook['when'] == BEFORE: print('The workflow will soon be complete for the object with id:%s and field_name:%s!' % ( workflow_object.pk, workflow.field_name )) elif hook['when'] == AFTER: print('The workflow has just been complete for the object with id:%s and field_name:%s!' % ( workflow_object.pk, workflow.field_name )) def handle(context): hook = context['hook'] if hook['type'] == 'on-transit': _handle_my_transitions(hook) elif hook['type'] == 'on-approved': _handle_my_approvals(hook) elif hook['type'] == 'on-complete': _handle_completions(hook) else: print("Unknown event type %s" % hook['type'])