Commits

Ian George committed c98534a

Bug with saving, and one with the form state choices

  • Participants
  • Parent commits 81f10b4

Comments (0)

Files changed (4)

File statemachine/fields.py

 from fsm import FSM_State, FSM
 from forms import FSM_StateFormField
 
+DEFAULT_STATE = "start"
+
 class FSM_StateField(models.Field):
     __metaclass__ = models.SubfieldBase
     _machine = None
         machine must be a class, not an instance
         """
         self.__base_machine = machine
-        defaults = {'max_length': 50, 'default' : "start"}
+        defaults = {'max_length': 50, 'default' : DEFAULT_STATE}
         defaults.update(kwargs)
         super(FSM_StateField, self).__init__(self, **defaults)
 
         return self._machine.state
 
     def formfield(self, **kwargs):
-        choices = tuple(self._machine.get_django_choices())
+        if self._machine:
+            choices = tuple(self._machine.get_django_choices())
+        else:
+            choices = ((DEFAULT_STATE, DEFAULT_STATE),)
         defaults = {'form_class': FSM_StateFormField, 'label':self.name, 'choices':choices}
         defaults.update(kwargs)
-
         return super(FSM_StateField, self).formfield(**defaults)

File statemachine/forms.py

+from django import forms
+
+class FSM_StateFormField(forms.ChoiceField):
+    pass

File statemachine/models.py

 
     def save(self, user, notes='', *args, **kwargs):
         super(StateMachineWithHistory, self).save(*args, **kwargs)
-        #log transition - not sure if we should do this here if we're not saving
-        s = StateMachineHistory()
-        s.machine = self
-        s.from_state = self._opening_state
-        s.to_state = self.status.state
-        s.user = user
-        s.notes = notes
-        s.save()
+
+        if self.status.state != self._opening_state:
+            s = StateMachineHistory()
+            s.machine = self
+            s.from_state = self._opening_state
+            s.to_state = self.status.state
+            s.user = user
+            s.notes = notes
+            s.save() 
 
 
     class Meta:

File statemachine/widgets.py

-from django import forms
-
-class FSM_StateChoice(forms.Select):
-    pass
-        
-