Skip to content

Instantly share code, notes, and snippets.

@frutik
Created October 15, 2012 20:39
Show Gist options
  • Save frutik/3895289 to your computer and use it in GitHub Desktop.
Save frutik/3895289 to your computer and use it in GitHub Desktop.
from django.db import models
from django.db import models
import xworkflows
from django_xworkflows import models as dxmodels
class TransitionLog(dxmodels.BaseTransitionLog):
# This is where we'll store the modified object
my = models.ForeignKey('My')
# Extra data to keep about transitions
ip = models.CharField(max_length=24, blank=True)
source = models.CharField(max_length=24, blank=True)
# Set the name of the field where the modified object goes
MODIFIED_OBJECT_FIELD = 'my'
# Define extra logging attributes
EXTRA_LOG_ATTRIBUTES = (
('ip', 'ip', ''), # Transitions are called with 'ip' kwarg
('source', 'source', ''), # Transitions are called with 'ip' kwarg
)
class MyWorkflowImplWrapper(xworkflows.base.ImplementationWrapper):
def _post_transition(self, result, *args, **kwargs):
super(MyWorkflowImplWrapper, self)._post_transition(self, result, *args, **kwargs)
#print self, result, args, kwargs
#print type(self.instance), self.instance.id, self.field_name, self.transition.name, self.workflow
print self.instance.__module__, self.instance.__class__.__name__, 'entered state', self.transition.name
class MyWorkflow(dxmodels.Workflow):
log_model_class = TransitionLog
states = (
('foo', u"Foo"),
('bar', u"Bar"),
('baz', u"Baz"),
)
transitions = (
('foobar', 'foo', 'bar'),
('gobaz', ('foo', 'bar'), 'baz'),
('bazbar', 'baz', 'bar'),
)
initial_state = 'foo'
implementation_class = MyWorkflowImplWrapper
class My(dxmodels.WorkflowEnabled, models.Model):
state = dxmodels.StateField(MyWorkflow)
other = models.CharField(max_length=4)
@frutik
Copy link
Author

frutik commented Oct 15, 2012

from order.models import My
m = My()
m.save()
m.foobar()
order.models.My with id 20 entered state bar via foobar
m.state.workflow.transitions
TransitionList([Transition('gobaz', [<State: 'foo'>, <State: 'bar'>], <State: 'baz'>), Transition('foobar', [<State: 'foo'>], <State: 'bar'>), Transition('bazbar', [<State: 'baz'>], <State: 'bar'>)])
m.bazbar()
Traceback (most recent call last):
File "", line 1, in
File "/home/frutik/virtualenv/xworkflows/local/lib/python2.7/site-packages/xworkflows/base.py", line 380, in call
self._pre_transition_checks()
File "/home/frutik/virtualenv/xworkflows/local/lib/python2.7/site-packages/xworkflows/base.py", line 347, in _pre_transition_checks
(self.transition.name, current_state.name))
InvalidTransitionError: Transition 'bazbar' isn't available from state 'bar'.
m.gobaz()
order.models.My with id 20 entered state baz via gobaz
m.bazbar()
order.models.My with id 20 entered state bar via bazbar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment