From 410004d945c28a094a99199100439d5647418c9e Mon Sep 17 00:00:00 2001 From: Gert Van Gool Date: Tue, 6 Oct 2015 10:33:22 +0200 Subject: [PATCH] Fixes #23, don't allow transition on unsaved model --- django_states/model_methods.py | 7 +++++++ django_states/tests.py | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/django_states/model_methods.py b/django_states/model_methods.py index 8d09eef..30fc4f9 100644 --- a/django_states/model_methods.py +++ b/django_states/model_methods.py @@ -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: @@ -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) diff --git a/django_states/tests.py b/django_states/tests.py index fed97e9..f7722fe 100644 --- a/django_states/tests.py +++ b/django_states/tests.py @@ -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)