Anonymous avatar Anonymous committed c2b0b48

FSM.state returns a State instance now rather than the name of one. Some changes required to make that not break everything. Sorry

Comments (0)

Files changed (3)

statemachine/fields.py

     def to_python(self, value):
         self.machine = self.__machine_class()
         try:
-            name = value.state
+            name = value.state.name
         except AttributeError:
             name = value
-
         self.machine.set_initial_state(name)
         if self.default_choices_all:
             self._choices = self.all_state_choices()
 
     def get_db_prep_value(self, value, connection, prepared=False):
         try:
-            name = value.state
+            name = value.state.name
         except AttributeError:
             name = value
         return name
         state_choices = []
         if self.machine.state:
             state_choices.append((self.machine.state,self.machine.state))
-            state_choices.extend([(name,name) for name in self.machine.states[self.machine.state].exit_states])
+            state_choices.extend([(name,name) for name in self.machine.state.exit_states])
 
         return state_choices
 

statemachine/fsm.py

             
 
     def __unicode__(self):
+        if self.label:
+            return self.label
         return self.name
 
     def exit(self, target_state, *args, **kwargs):
 
     def __init__(self, verify_on_execute=True):
         self.states = deepcopy(self.base_states)
-        self.__state = self.states[self.states.keys()[0]].name
+        self.__state = self.states.keys()[0]
         self.dbg = None
         self.verify_on_execute = verify_on_execute
 
         return self.__state
 
     def getstate(self):
-        return self.__state
+        if self.__state and self.__state in self.states:
+            return self.states[self.__state]
+
     def setstate(self, value):
         raise FSM_NotAllowed("State is read only, please use change() to change state")
     state = property(getstate, setstate)
         restoring a FSM into a state that has been previously attained
         from the inital state.
         """
-        self.__state = state
+        if state in self.states:
+            self.__state = state
+        else:
+            self.__state = self.states.keys()[0]
 
     def available_states(self):
         """
         Returns a list containing the available exit states from the current state
         """
-        return [self.states[s] for s in self.states[self.__state].exit_states]
+        return [self.states[s] for s in self.state.exit_states]
         
     def verify(self):
         """
         if not new_state in self.states:
             raise FSM_StateDoesNotExist(new_state)
 
-        exiting_state = self.states[self.state]
+        exiting_state = self.state
 
         if not new_state in exiting_state.exit_states:
             raise FSM_TransitionNotAllowed(new_state)

statemachine/tests.py

         testmachine.status.change("step2")
         testmachine.save(None)
         #reload to check state is saved and reinstated
-        self.assertEqual(testmachine.status.state, "step2")
+        self.assertEqual(testmachine.status.state.name, "step2")
         fsm_id = testmachine.id
-        self.assertEqual(testmachine.status.state, "step2")
+        self.assertEqual(testmachine.status.state.name, "step2")
         testmachine = TestStateMachine.objects.get(pk=fsm_id)
         testmachine.status.change("step3")
         testmachine.save(None)
         testmachine = TestStateMachine(field1=100, field2="LALALALALA")
         testmachine.save(None)
         #check we are starting at "start"
-        self.assertEqual(testmachine.status.state, "start")
+        self.assertEqual(testmachine.status.state.name, "start")
         
         with self.assertRaises(fsm.FSM_TransitionNotAllowed):
             testmachine.status.change("step3")
 
         #check we are still at "start" after a failed transition
-        self.assertEqual(testmachine.status.state, "start")
+        self.assertEqual(testmachine.status.state.name, "start")
 
     def test_transition_history(self):
         """ Checking that history is being saved for each transition """
         testmachine = TestStateMachine(field1=100, field2="LALALALALA")
-        self.assertEqual(testmachine.status.state, "start")
+        self.assertEqual(testmachine.status.state.name, "start")
         testmachine.status.change("step1a")
         testmachine.save(None)
         testmachine.status.change("step2")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.