This page documents the internal mechanisms of django_xworkflows.
The mechanism to bind a django model to a xworkflows.Workflow relies on the WorkflowEnabled and StateField classes.
This class is a simple Django Field, specifically tuned for a Workflow.
It is internally backed by a CharField containing the name of the state.
Reading the value always returns a xworkflows.base.StateWrapper, writing checks that the value is a valid state or a valid state name.
Mandatory; holds the Workflow to which this StateField relates
The workflow states, as a list of (name, title) tuples, for use in forms.
The name of the inital state of the workflow
The length of the longest state name in the workflow.
Such a field cannot be blanked (otherwise, the workflow wouldn’t have a meaning).
Since the field cannot be empty, is cannot be null either.
This class inherits from Django’s Model class, performing some transformations on the subclass: each attr = StateField(SomeWorkflow, ...) attribute will enable XWorkflows’ transition detection and wrapping.
Most of this job is performed through WorkflowEnabledMeta.
This method overrides the default django one to retrieve the title from a StateField field.
Transitions mostly follow XWorkflows’ mechanism.
This specific wrapper runs all transition-related code (transition_check hooks, before_transition hooks, implementation, log_transition, after_transition hooks) in a single database transaction.
The TransactionalImplementationWrapper can be enabled by setting it to the implementation_class attribute of a xworkflows.Workflow or of a Workflow:
class MyWorkflow(models.Workflow):
implementation_class = models.TransactionalImplementationWrapper
This xworkflows.Workflow subclass performs a few customization:
This holds the model to use to log to the database. If empty, no database logging is performed.
Logs the transition into the database, saving the following elements:
The default BaseTransitionLog model is django_xworkflows.xworkflow_log.models.TransitionLog, but an alternative one can be specified in log_model.
Hint
Override this method to log to a custom BaseTransitionLog without generic foreign keys.
In addition to xworkflows.Workflow.log_transition(), additional actions are performed:
This abstract model describes which fields are expected when logging a transition to the database.
The default for all WorkflowEnabled subclasses will be to log to django_xworkflows.xworkflow_log.models.TransitionLog instances.
This behaviour can be altered by setting the log_model attribute of Workflow definition.
A GenericForeignKey to the WorkflowEnabled being modified.
The name of the transition performed
Type : | str |
---|
The name of the source state for the transition
Type : | str |
---|
The name of the destination state for the transition
Type : | str |
---|
The time at which the transition occurred.
Type : | datetime.datetime |
---|
An example BaseTransitionLog model is available in the django_xworkflows.xworkflow_log application. Including it to INSTALLED_APPS will enable database logging of transitions for all WorkflowEnabled subclasses.
This specific BaseTransitionLog also stores the user responsible for the transition, if provided.
The exact Model to use for that foreign key can be set in the XWORKFLOWS_USER_MODEL setting (defaults to 'auth.User', which uses django.contrib.auth.models.User).
Note
These classes are private API.
This metaclass is responsible for parsing a class definition, detecting all StateField and collecting/defining the associated TransactionalImplementationWrapper.
Collect all StateField from the given attrs (the default version collects Workflow subclasses instead)
Perform necessay actions to register the Workflow stored in a StateField defined at field_name into the given attributes dict.
It differs from the base implementation which adds a StateProperty instead of keeping the StateField.
Parameters: |
|
---|