Skip to content

Instantly share code, notes, and snippets.

@dhruvbaldawa
Created January 31, 2018 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhruvbaldawa/2e525294e3ec75e8b5c3a0f8dd517b8d to your computer and use it in GitHub Desktop.
Save dhruvbaldawa/2e525294e3ec75e8b5c3a0f8dd517b8d to your computer and use it in GitHub Desktop.
class Payments(models.Model):
STATES = {
'started': 'Started',
'captured': 'Captured',
'completed': 'Completed',
}
state = models.CharField(default='started', choices=STATES.items(), max_length=16)
def __init__(self, *args, **kwargs):
super(Payments, self).__init__(*args, **kwargs)
# Initialize the state machine
self.machine = Machine(
model=self, # the object on which the library attaches trigger functions
states=self.STATES.keys(), # states in the state machine
initial='started', # initial state of the machine
after_state_change='save', # method to call after state change, this will call self.save()
)
# Lets add transitions to our state machine, we can now call self.capture() to perform
# the transition and capture a payment
self.machine.add_transition(
trigger='capture',
source='started',
destination='captured',
before='contact_payment_gateway',
)
# We can now call self.complete() to perform the transition and complete a payment
self.machine.add_transition(
trigger='complete',
source='captured',
destination='completed',
after='notify_user',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment