Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StateField with abstract models #59

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions django_states/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def contribute_to_class(self, cls, name):
"""
super(StateField, self).contribute_to_class(cls, name)

if cls._meta.abstract:
# Do not contribute to abstract models
return
# Set choice options (for combo box)
self._choices = self._machine.get_state_choices()
self.default = self._machine.initial_state
Expand Down Expand Up @@ -80,6 +83,7 @@ def contribute_to_class(self, cls, name):

models.signals.class_prepared.connect(self.finalize, sender=cls)


def finalize(self, sender, **kwargs):
"""
Override ``save``, call initial state handler on save.
Expand Down
22 changes: 17 additions & 5 deletions django_states/model_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def name(si_self):
"""
return getattr(self, field)

@property
def initial(si_self):
"""
The description of the current state
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this docstring correct? Don't you mean the initial state?

"""
si = machine.get_state(getattr(self, field))
return si.initial

@property
def description(si_self):
"""
Expand Down Expand Up @@ -146,7 +154,7 @@ def test_transition(si_self, transition, user=None):
if validation_errors:
raise TransitionNotValidated(si_self, transition, validation_errors)

return True
return si_self

def make_transition(si_self, transition, user=None, **kwargs):
"""
Expand Down Expand Up @@ -194,19 +202,23 @@ def make_transition(si_self, transition, user=None, **kwargs):
try:
from_state = getattr(self, field)

before_state_execute.send(sender=self,
before_state_execute.send(sender=self.__class__,
instance=self,
from_state=from_state,
to_state=t.to_state)
to_state=t.to_state,
user=user)
# First call handler (handler should still see the original
# state.)
t.handler(self, user, **kwargs)

# Then set new state and save.
setattr(self, field, t.to_state)
self.save()
after_state_execute.send(sender=self,
after_state_execute.send(sender=self.__class__,
instance=self,
from_state=from_state,
to_state=t.to_state)
to_state=t.to_state,
user=user)
except Exception, e:
if _state_log_model:
transition_log.make_transition('fail')
Expand Down
6 changes: 4 additions & 2 deletions django_states/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

#: Signal that is sent before a state transition is executed
before_state_execute = django.dispatch.Signal(providing_args=['from_state',
'to_state'])
'to_state',
'user'])
#: Signal that s sent after a state transition is executed
after_state_execute = django.dispatch.Signal(providing_args=['from_state',
'to_state'])
'to_state',
'user'])
3 changes: 2 additions & 1 deletion test_proj/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
# Django settings for test_proj project.
import os, sys

Expand Down Expand Up @@ -157,4 +158,4 @@
'propagate': True,
},
}
}
}