Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RE-[#105] Update documentation on hooks and functions
- Loading branch information
Showing
11 changed files
with
224 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
.. _hooking_function_guide: | ||
|
||
Functions | ||
========= | ||
|
||
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. | ||
|
||
|
||
.. code:: python | ||
INSTALLED_APPS=[ | ||
... | ||
codemirror2 | ||
river | ||
... | ||
] | ||
Here is an example function; | ||
|
||
.. code:: python | ||
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. | ||
|
||
Context Parameter | ||
----------------- | ||
|
||
`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 | | The event type that is hooked up. The payload will | | ||
| | | | * on-transit | | likely differ according to this value | | ||
| | | | * on-complete | | | ||
+---------------------+--------+--------------------+---------------------------------------------------------+ | ||
| when | String | | * BEFORE | | Whether it is hooked right before the event happens | | ||
| | | | * AFTER | | 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 | | ||
+---------------------+--------+--------------------+---------------------------------------------------------+ | ||
|
||
Context Payload | ||
--------------- | ||
|
||
On-Approved Event Payload | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
| Key | Type | Description | | ||
+=====================+==================+=========================================================+ | ||
| workflow | Workflow Model | The workflow that the transition currently happening | | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
| workflow_object | | Your Workflow | | The workflow object of the model that has the state | | ||
| | | Object | | field in it | | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
| transition_approval | | Transition | | The approval object that is currently approved which | | ||
| | | Approval | | contains the information of the transition(meta) as | | ||
| | | | well as who approved it etc. | | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
|
||
On-Transit Event Payload | ||
^^^^^^^^^^^^^^^^^^^^^^^^ | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
| Key | Type | Description | | ||
+=====================+==================+=========================================================+ | ||
| workflow | Workflow Model | The workflow that the transition currently happening | | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
| workflow_object | | Your Workflow | | The workflow object of the model that has the state | | ||
| | | Object | | field in it | | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
| transition_approval | | Transition | | The last transition approval object which contains | | ||
| | | Approval | | the information of the transition(meta) as well as | | ||
| | | | who last approved it etc. | | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
|
||
|
||
On-Complete Event Payload | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
| Key | Type | Description | | ||
+=====================+==================+=========================================================+ | ||
| workflow | Workflow Model | The workflow that the transition currently happening | | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
| workflow_object | | Your Workflow | | The workflow object of the model that has the state | | ||
| | | Object | | field in it | | ||
+---------------------+------------------+---------------------------------------------------------+ | ||
|
||
|
||
|
||
|
||
Example Function | ||
^^^^^^^^^^^^^^^^ | ||
|
||
.. code:: python | ||
from river.models.hook import BEFORE, AFTER | ||
def handle_transition(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_approval(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_complete(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_transition(hook) | ||
elif hook['type'] == 'on-approved': | ||
handle_approval(hook) | ||
elif hook['type'] == 'on-complete': | ||
handle_complete(hook) | ||
else: | ||
print("Unknown event type %s" % hook['type']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.. _hooking_guide: | ||
|
||
Hook it Up | ||
========== | ||
|
||
The hookings in ``django-river`` can be created both specifically for a workflow object or for a whole workflow. ``django-river`` comes with some model objects and admin interfaces which you can use | ||
to create the hooks. | ||
|
||
* To create one for whole workflow regardless of what the workflow object is, go to | ||
* ``/admin/river/onapprovedhook/`` to hook up to an approval | ||
* ``/admin/river/ontransithook/`` to hook up to a transition | ||
* ``/admin/river/oncompletehook/`` to hook up to the completion of the workflow | ||
|
||
* To create one for a specific workflow object you should use the admin interface for the workflow object itself. One amazing feature of ``django-river`` is now that it creates a default admin interface | ||
with the hookings for your workflow model class. If you have already defined one, ``django-river`` enriches your already defined admin with the hooking section. It is default enabled. To disable it | ||
just define ``RIVER_INJECT_MODEL_ADMIN`` to be ``False`` in the ``settings.py``. | ||
|
||
|
||
**Note: ** They can programmatically be created as well since they are model objects. If it is needed to be at workflow level, just don't provide the workflow object column. If it is needed | ||
to be for a specific workflow object then provide it. | ||
Here are the list of hook model object | ||
|
||
* OnApprovedHook | ||
* OnTransitHook | ||
* OnCompleteHook |
Oops, something went wrong.