Created
January 31, 2018 05:55
-
-
Save dhruvbaldawa/2e525294e3ec75e8b5c3a0f8dd517b8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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