7. Events¶
Table of Contents
7.1. Introduction¶
DStore makes the use of an Event Manager in the Store and Models themselves.
These events allow you to hook custom code before or after an action takes place.
In fact, this is exactly how DStore-ACL works to provide a security layer to DStore Models.
7.1.1. Listening To Events¶
Listening to an event is as easy as adding your method to the event in question.
For example, to listen to before_init_app on the store:
from dstore import MemoryStore
def before_init_app( event, store ):
print( "Before init store %s" % store.name )
def after_init_app( event, store ):
print( "After init store %s" % store.name )
store = MemoryStore()
store.events.before_init_app += before_init_app
store.events.after_init_app += after_init_app
store.init_app()
7.2. Store Events¶
To listen to a store event, you add your method to the store’s event object.
store.events.before_init_app += method_to_call
7.2.1. init_app¶
This event is fired before and after you execute store.init_app()
7.2.2. destroy_app¶
This event is fired before and after you execute destroy_app on a store
7.2.3. register_models¶
This event is fired before and after all models have been registered.
This happens automatically when init_app is run on the store
7.2.4. register_model¶
This event is fired when a single Model is being registered on the store.
-
before_register_models
(event, store, model) -
after_register_models
(event, store, model) -
store
The Data Store that register_models is being run on
-
model
¶ The Model Class that is being registered
-
7.2.5. create_all¶
This event is fired before and after you execute create_all on a store
7.2.6. destroy_all¶
This event is fired before and after you execute destroy_all on a store
7.2.7. empty_all¶
This event is fired before and after you execute empty_all on a store
7.2.8. connect¶
This event is fired before and after you execute connect on a store
7.2.9. disconnect¶
This event is fired before and after you execute disconnect on a store
7.2.10. add_bulk¶
This event is fired before and after you execute add_bulk on a store
7.3. Model Events¶
To listen to a store event, you add your method to the store’s event object.
from dstore import MemoryStore, Model, var, mod
class Car( Model ):
_namespace = "cars.make"
_vars = [
var.RowID,
var.String( "manufacturer", 32, mods = [ mod.NotNull() ] ),
var.String( "make", 32, mods = [ mod.NotNull() ] ),
var.Number( "year", mods = [ mod.NotNull(), mod.Min( 1950 ), mod.Max( 2017 ) ] ),
]
def car_before_add( event, model, instance ):
print( "Attempting to add a new %s instance" % model._namespace )
Car.events.before_add += car_before_add
7.3.1. add¶
This event is fired before and after you attempt to add a new Model Instance
7.3.2. delete¶
This event is fired before and after you attempt to delete an existing Model Instance
7.3.3. update¶
This event is fired before and after you attempt to update an existing Model Instance
7.3.4. validate¶
This event is fired before and after you attempt to add or update a Model Instance (i.e. on validation)
7.3.5. all¶
This event is fired before and after you attempt to get all Model instances
-
before_all
(event, model)¶ -
model
The Model Class of the instance to be added or updated
-
-
after_all
(event, model, instances)¶ -
model
The Model Class of the instance to be added or updated
-
instances
The list of all instances
-
7.3.6. get¶
This event is fired before and after you attempt to get a Model instance
-
before_get
(event, model, row_id)¶ -
model
The Model Class of the instance to be added or updated
-
row_id
¶ The ID of the instance to retrieve
-
-
after_get
(event, model, instance)¶ -
model
The Model Class of the instance to be added or updated
-
instance
The Model instance retrieved
-
7.3.7. empty¶
This event is fired before and after you attempt to delete all Model instances
7.3.8. create¶
This event is fired before and after you attempt to create the storage for the Model instances
7.3.9. destroy¶
This event is fired before and after you attempt to destroy the storage for the Model instances
7.3.10. filter¶
This event is fired before and after you attempt to get a filtered list of the Model instances
-
before_filter
(event, model, params)¶ -
model
The Model Class to filter for instances
-
params
¶ A dictionary of the parameters used to filter the list
-
-
after_filter
(event, model, instances, params)¶ -
model
The Model Class to filter for instances
-
instances
The filtered list of Model instances
-
params
A dictionary of the parameters used to filter the list
-