Skip to content

Commit

Permalink
Fixes #23, don't allow transition on unsaved model
Browse files Browse the repository at this point in the history
  • Loading branch information
gvangool committed Oct 6, 2015
1 parent 2378b4a commit 410004d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions django_states/model_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ def test_transition(si_self, transition, user=None):
if not machine.has_transition(transition):
raise UnknownTransition(self, transition)

if not self.pk:
raise ValueError("Unsaved model instance %r cannot be used in make_transition." % self)

t = machine.get_transitions(transition)

if getattr(self, field) not in t.from_states:
Expand Down Expand Up @@ -162,6 +165,10 @@ def make_transition(si_self, transition, user=None, **kwargs):
# Transition name should be known
if not machine.has_transition(transition):
raise UnknownTransition(self, transition)

if not self.pk:
raise ValueError("Unsaved model instance %r cannot be used in make_transition." % self)

t = machine.get_transitions(transition)

_state_log_model = getattr(self, '_%s_log_model' % field, None)
Expand Down
6 changes: 6 additions & 0 deletions django_states/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,9 @@ def test_statelog(self):
# We should also be able to find this via
self.assertEqual(test.get_state_transitions().count(), 1)
self.assertEqual(len(test.get_public_state_transitions()), 1)

def test_statelog_unsaved(self):
test = DjangoStateLogClass(field1=42, field2="Hello world?")
# Try to make transition on unsaved model
with self.assertRaises(ValueError):
test.get_state_info().make_transition('start_step_1', user=self.superuser)

0 comments on commit 410004d

Please sign in to comment.