Python client for the G Adventures REST API
Project description
# django-dispatcher
Logic engine for determining order of things to execute
Sometimes there's a series of things that must run in sequence. This is a
configuration based approach enabling that.
Typical Usage
---
1. Create some transition objects. They are basically Interfaces that
require some methods and properties to be defined and reward you with
some context built at an appropriate time.
The required functions/params include:
- final_state (String)
- is_valid (Function returning Boolean)
- context (Function returning Dict)
```
from dispatcher import Transition
class Cart2DayReminder(Transition):
final_state = CART_2_DAY_REMINDER_SENT
@property
def onlinebooking(self):
if self._onlinebooking is None:
self._onlinebooking = OnlineBooking.objects.get(pk=self.resources['online_booking'])
return self._onlinebooking
_onlinebooking = None
@property
def profile(self):
if self._profile is None:
self._profile = self.onlinebooking.user and self.onlinebooking.user.profile
return self._profile
_profile = None
def is_valid(self):
if self.onlinebooking.date_updated < (datetime.now() - timedelta(days=2)):
self.errors.append('The booking was created less than 2 days ago')
return False
return True
def build_context(self):
return {
'booking_id': self.onlinebooking.id,
'email_to': self.profile.email,
}
class Cart1WeekReminder(Transition):
...
```
2. Create a config, listing the transitions that can happen
```
DISPATCHER_CONFIG = {
'chains': [
{
'chain_type': 'abandoned_cart',
'transitions': {
NEW: [Cart2DayReminder, Cart1WeekReminder],
Cart2DayReminder.final_state: [Cart1WeekReminder, ],
Cart1WeekReminder.final_state: [],
}
}
]
}
```
3. Initialize the dispatcher with the above settings. You'll have to
determine the `chain_type` on your own. Its name must be the same as
the above config. We include the `requests_by` to keep track of where
the request originates from.
```
from dispatcher import Dispatcher
dispatcher = Dispatcher(
chain_type='abandoned_cart',
chain_configs=DISPATCHER_CONFIG,
requests_by='cart_abandonment_scripts'
})
```
4. Provide the resources to query searching for a chain. The chain
will query using an `AND` statement, meaning all the resources must
be present when retrieving the chain.
```
dispatcher.get_or_create_chain([
('online_booking', '123456'),
('customer', '57844')
])
```
5. Provide a callback for the chain. Should a chain transition to a new
valid state, whatever callback you pass will be sent on. The callback
takes the transition passed in and any callback arguments specified.
```
def some_callback(transition, \**callback_args):
# so something magical
dispatcher.execute_chain(callback=some_callback, callback_args={})
```
Logic engine for determining order of things to execute
Sometimes there's a series of things that must run in sequence. This is a
configuration based approach enabling that.
Typical Usage
---
1. Create some transition objects. They are basically Interfaces that
require some methods and properties to be defined and reward you with
some context built at an appropriate time.
The required functions/params include:
- final_state (String)
- is_valid (Function returning Boolean)
- context (Function returning Dict)
```
from dispatcher import Transition
class Cart2DayReminder(Transition):
final_state = CART_2_DAY_REMINDER_SENT
@property
def onlinebooking(self):
if self._onlinebooking is None:
self._onlinebooking = OnlineBooking.objects.get(pk=self.resources['online_booking'])
return self._onlinebooking
_onlinebooking = None
@property
def profile(self):
if self._profile is None:
self._profile = self.onlinebooking.user and self.onlinebooking.user.profile
return self._profile
_profile = None
def is_valid(self):
if self.onlinebooking.date_updated < (datetime.now() - timedelta(days=2)):
self.errors.append('The booking was created less than 2 days ago')
return False
return True
def build_context(self):
return {
'booking_id': self.onlinebooking.id,
'email_to': self.profile.email,
}
class Cart1WeekReminder(Transition):
...
```
2. Create a config, listing the transitions that can happen
```
DISPATCHER_CONFIG = {
'chains': [
{
'chain_type': 'abandoned_cart',
'transitions': {
NEW: [Cart2DayReminder, Cart1WeekReminder],
Cart2DayReminder.final_state: [Cart1WeekReminder, ],
Cart1WeekReminder.final_state: [],
}
}
]
}
```
3. Initialize the dispatcher with the above settings. You'll have to
determine the `chain_type` on your own. Its name must be the same as
the above config. We include the `requests_by` to keep track of where
the request originates from.
```
from dispatcher import Dispatcher
dispatcher = Dispatcher(
chain_type='abandoned_cart',
chain_configs=DISPATCHER_CONFIG,
requests_by='cart_abandonment_scripts'
})
```
4. Provide the resources to query searching for a chain. The chain
will query using an `AND` statement, meaning all the resources must
be present when retrieving the chain.
```
dispatcher.get_or_create_chain([
('online_booking', '123456'),
('customer', '57844')
])
```
5. Provide a callback for the chain. Should a chain transition to a new
valid state, whatever callback you pass will be sent on. The callback
takes the transition passed in and any callback arguments specified.
```
def some_callback(transition, \**callback_args):
# so something magical
dispatcher.execute_chain(callback=some_callback, callback_args={})
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-dispatcher-0.1.1.tar.gz
.
File metadata
- Download URL: django-dispatcher-0.1.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e68fa53e7d40ff5b6192f6d9561e8386720fee05f13bc3de6f1a8894edfb64c |
|
MD5 | aa6c052e2bea0e7ba2997526057f7752 |
|
BLAKE2b-256 | 4a93070a89f705ff5738d94cb56f86aa01998e5de8eaf56959d5307f55c5e6db |