Commits

Ian George committed 241d19d

Tidying up admin areas, useless functions, and tests

Comments (0)

Files changed (3)

statemachine/fields.py

 from django.db import models
 from fsm import State, FSM
 from forms import FSM_StateFormField
+from django.forms.forms import pretty_name
 
 DEFAULT_STATE = "start"
 
 class FSM_StateField(models.Field):
     __metaclass__ = models.SubfieldBase
 
-    def __init__(self, machine, *args, **kwargs):
+    def __init__(self, machine, admin_choices_all=False, *args, **kwargs):
         self.__machine_class = machine
         defaults = {'max_length': 50, 'default' : DEFAULT_STATE}
         defaults.update(kwargs)
             name = value
         return name
 
+    def get_admin_choices(self):
+        return [(name, pretty_name(name)) for name in self.machine.states.keys()]
+
+    def get_exit_choices(self):
+        state_choices = []
+        if self.state:
+            state_choices.append((self.state,self.state))
+            state_choices.extend([(name,name) for name in self.states[self.state].exit_states])
+
+        return state_choices
+
     def formfield(self, **kwargs):
         if self.machine:
-            choices = tuple(self.machine.get_exit_choices())
+            choices = tuple(self.get_admin_choices())
         else:
-            choices = ((DEFAULT_STATE, DEFAULT_STATE),)
+            choices = ((pretty_name(DEFAULT_STATE), DEFAULT_STATE),)
         defaults = {'form_class': FSM_StateFormField, 'label':self.name, 'choices':choices}
         defaults.update(kwargs)
         return super(FSM_StateField, self).formfield(**defaults)
 from copy import deepcopy
 from django.conf import settings
+from django.forms.forms import pretty_name
 """
 Some Exceptions
 """
     def __unicode__(self):
         return self.name
 
-
     def exit(self, target_state, *args, **kwargs):
         """ 
         Checks states and exits if possible, if not possible it raises FSM_TransitionNotAllowed 
         self.verify_on_execute = verify_on_execute
 
     def __unicode__(self):
-        return self.__state
+        return pretty_name(self.__state)
 
     def getstate(self):
         return self.__state
         
     def verify(self):
         """
+        Check that all the named exit states exist, list the ones that don't
         """
         bad_states = []
         state_names = set(self.states.keys())
         if len(bad_states):
             raise FSM_VerificationError("Invalid exit state(s)", bad_states)
 
-    def get_admin_choices(self):
-        return [(name, name) for name in self.states.keys()]
-
-    def get_exit_choices(self):
-        state_choices = []
-        if self.state:
-            state_choices.append((self.state,self.state))
-            state_choices.extend([(name,name) for name in self.states[self.state].exit_states])
-
-        return state_choices
-
-    def add(self, fsm_state):
-        """
-        Adds a FSM_State to the machine
-        """
-        self.states[fsm_state.name] = fsm_state
-
     def change(self, new_state, *args, **kwargs):
         """
         Transitions the machine to its new state.

statemachine/tests.py

     def test_verify(self):
         """ Should raise a FSM_VerificationError """
         testmachine = TestStateMachine(field1=100, field2="LALALALALA")
-        testmachine.status.add(fsm.State("random", ["doesntexist", "alsodoesnexist"]))
+        testmachine.status.states["random"] = (fsm.State("random", ["doesntexist", "alsodoesnexist"]))
         with self.assertRaises(fsm.FSM_VerificationError):
             testmachine.status.verify()