Commits

Ian George committed 81f10b4

Tests working, admin / form fields now in place

  • Participants
  • Parent commits 453059c

Comments (0)

Files changed (4)

statemachine/fields.py

 from copy import deepcopy
 from django.db import models
 from fsm import FSM_State, FSM
+from forms import FSM_StateFormField
 
 class FSM_StateField(models.Field):
     __metaclass__ = models.SubfieldBase
+    _machine = None
 
     def __init__(self, machine, *args, **kwargs):
         """
         machine must be a class, not an instance
         """
         self.__base_machine = machine
-        kwargs['max_length'] = 50
-        kwargs['default'] = "start"
-        super(FSM_StateField, self).__init__(self, *args, **kwargs)
+        defaults = {'max_length': 50, 'default' : "start"}
+        defaults.update(kwargs)
+        super(FSM_StateField, self).__init__(self, **defaults)
 
     def db_type(self, connection):
         return "char(50)"
 
     def to_python(self, value):
+        try:
+            name = value.state
+        except AttributeError:
+            name = value
         self._machine = deepcopy(self.__base_machine)
-        self._machine.set_initial_state(value)
+        self._machine.set_initial_state(name)
         return self._machine
 
     def get_db_prep_value(self, value, connection, prepared=False):
         return self._machine.state
+
+    def formfield(self, **kwargs):
+        choices = tuple(self._machine.get_django_choices())
+        defaults = {'form_class': FSM_StateFormField, 'label':self.name, 'choices':choices}
+        defaults.update(kwargs)
+
+        return super(FSM_StateField, self).formfield(**defaults)

statemachine/fsm.py

 
     def get_django_choices(self):
         state_choices = []
-        if self.__state:
+        if self.state:
             state_choices.append((self.state,self.state))
             state_choices.extend([(name,name) for name in self.states[self.state].exit_states])
 

statemachine/models.py

 
 from fsm import FSM_Exception, FSM
 from fields import FSM_StateField
-from widgets import FSM_StateChoice
 
 ##################################################
 # Models

statemachine/tests.py

     field2 = models.CharField(max_length=25)
 
 class StateMachineTests(TestCase):
-    def txest_verify(self):
+    def test_verify(self):
         """ Should raise a FSM_VerificationError """
         testmachine = TestStateMachine(field1=100, field2="LALALALALA")
         testmachine.status.add(FSM_State("random", ["doesntexist", "alsodoesnexist"]))
         with self.assertRaises(FSM_VerificationError):
             testmachine.status.verify()
 
-    def txest_save_fail(self):
+    def test_save_fail(self):
         """Should raise a StateMachineNotAllowed, messing with the state outside of the machine."""
         testmachine = TestStateMachine(field1=100, field2="LALALALALA")
         testmachine.save(None)
             testmachine.status.state = "Testing"    
             testmachine.save(None)
 
-    def txest_full_transition(self):
+    def test_full_transition(self):
         """ End to end smoke test of the transitions """
         testmachine = TestStateMachine(field1=100, field2="LALALALALA")
         testmachine.save(None)
         testmachine.status.change("end")
         testmachine.save(None)
 
-    def txest_invalid_transition(self):
+    def test_invalid_transition(self):
         """ Testing invalid transitions """
         testmachine = TestStateMachine(field1=100, field2="LALALALALA")
         testmachine.save(None)
         #check we are still at "start" after a failed transition
         self.assertEqual(testmachine.status.state, "start")
 
-    def txest_transition_history(self):
+    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.state_history.count(), 3)
         self.assertEqual(testmachine.state_history.all()[1].notes, "Some notes!")
 
-    def txest_entry_action(self):
+    def test_entry_action(self):
         """ Tests an action set to run on entry to a state """
         def entry_func(entered, row):
             row.field1 = 500