Skip to content

Commit

Permalink
additional properties for state transitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Slenders committed Mar 17, 2011
1 parent eadc2db commit 330f325
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/states2/models.py
Expand Up @@ -70,6 +70,18 @@ def has_transition(self, transition_name):
def get_transitions(self, transition_name): def get_transitions(self, transition_name):
return self.transitions[transition_name] return self.transitions[transition_name]


def has_state(self, state_name):
return state_name in self.states

def get_state(self, state_name):
return self.states[state_name]

def get_transition_from_states(self, from_state, to_state):
for t in self.transitions.values():
if t.from_state == from_state and t.to_state == to_state:
return t
raise Exception("Transition not found")



class StateDefinitionMeta(type): class StateDefinitionMeta(type):
def __new__(c, name, bases, attrs): def __new__(c, name, bases, attrs):
Expand Down Expand Up @@ -126,6 +138,10 @@ class StateTransition(object):
""" Base class for a state transitions """ """ Base class for a state transitions """
__metaclass__ = StateTransitionMeta __metaclass__ = StateTransitionMeta


# When a transition has been defined as public, is meant to be seen
# by the end-user.
public = False

def has_permission(cls, instance, user): def has_permission(cls, instance, user):
""" Override this method for special checking. """ """ Override this method for special checking. """
return True return True
Expand Down Expand Up @@ -220,6 +236,17 @@ def transitions(self):
else: else:
raise Exception('This model does not log state transitions. please enable it by setting log_transitions=True') raise Exception('This model does not log state transitions. please enable it by setting log_transitions=True')


@property
def public_transitions(self):
"""
Return the transitions which are meant to be seen by the customer. (The
admin on the other hand should be able to see everything.)
"""
if self._log:
return filter(lambda t: t.is_public and t.state.completed, self.transitions.all())
else:
return []

@classmethod @classmethod
def get_admin_actions(cls): def get_admin_actions(cls):
""" """
Expand Down Expand Up @@ -370,7 +397,7 @@ class fail(StateTransition):


@property @property
def completed(self): def completed(self):
return self.value == 'complete' return self.value == 'transition_completed'


def __unicode__(self): def __unicode__(self):
return '<State transition state on %s : "%s">' % (cls.__name__, self.value) return '<State transition state on %s : "%s">' % (cls.__name__, self.value)
Expand Down Expand Up @@ -401,6 +428,33 @@ class _StateTransition(models.Model):
state = StateField(machine=state_transition_state_model) state = StateField(machine=state_transition_state_model)
on = models.ForeignKey(cls, related_name='all_transitions') on = models.ForeignKey(cls, related_name='all_transitions')


@property
def state_transition_definition(self):
return cls.Machine.get_transition_from_states(self.from_state, self.to_state)

@property
def state_definiton(self):
return cls.Machine.get_state(self.to_state)

@property
def state_description(self):
return unicode(self.state_definiton.description)

@property
def is_public(self):
"""
Return True when this state transition is defined public in the machine.
"""
return self.state_transition_definition.public

@property
def transition_description(self):
"""
Return the description for this transition as defined in the
StateTransition declaration of the machine.
"""
return unicode(self.state_transition_definition.description)

def __unicode__(self): def __unicode__(self):
return '<State transition on %s at %s from "%s" to "%s">' % ( return '<State transition on %s at %s from "%s" to "%s">' % (
cls.__name__, self.start_time, self.from_state, self.to_state) cls.__name__, self.start_time, self.from_state, self.to_state)
Expand Down

0 comments on commit 330f325

Please sign in to comment.