7. Events

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()

before_init_app(event, store)
after_init_app(event, store)
store

The Data Store that init_app is being run on

7.2.2. destroy_app

This event is fired before and after you execute destroy_app on a store

before_destroy_app(event, store)
after_destroy_app(event, store)
store

The Data Store that destroy_app is being run on

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

before_register_models(event, store)
after_register_models(event, store)
store

The Data Store that register_models is being run on

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

before_create_all(event, store)
after_create_all(event, store)
store

The Data Store that create_all is being run on

7.2.6. destroy_all

This event is fired before and after you execute destroy_all on a store

before_destroy_all(event, store)
after_destroy_all(event, store)
store

The Data Store that destroy_all is being run on

7.2.7. empty_all

This event is fired before and after you execute empty_all on a store

before_empty_all(event, store)
after_empty_all(event, store)
store

The Data Store that empty_all is being run on

7.2.8. connect

This event is fired before and after you execute connect on a store

before_connect(event, store)
after_connect(event, store)
store

The Data Store that connect is being run on

7.2.9. disconnect

This event is fired before and after you execute disconnect on a store

before_disconnect(event, store)
after_disconnect(event, store)
store

The Data Store that disconnect is being run on

7.2.10. add_bulk

This event is fired before and after you execute add_bulk on a store

before_add_bulk(event, store, data)
after_add_bulk(event, store, data, instances)
store

The Data Store that disconnect is being run on

data

The dictionary provided that is used to add Model instances to the Store

instances

The Model instances that were added to the 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

before_add(event, model, instance)
after_add(event, model, instance)
model

The Model Class that a new instance is being added to

instance

The instance that is attempting to be added to the Model Class storage

7.3.2. delete

This event is fired before and after you attempt to delete an existing Model Instance

before_delete(event, model, instance)
after_delete(event, model, instance)
model

The Model Class of the instance to be deleted

instance

The instance that is attempting to be deleted from the Model Class storage

7.3.3. update

This event is fired before and after you attempt to update an existing Model Instance

before_update(event, model, instance)
after_update(event, model, instance)
model

The Model Class of the instance to be updated

instance

The instance that is attempting to be updated

7.3.4. validate

This event is fired before and after you attempt to add or update a Model Instance (i.e. on validation)

before_validate(event, model, instance)
after_validate(event, model, instance)
model

The Model Class of the instance to be added or updated

instance

The instance that is attempting to be added or updated

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

before_empty(event, model)
after_empty(event, model)
model

The Model Class that is to be emptied

7.3.8. create

This event is fired before and after you attempt to create the storage for the Model instances

before_create(event, model)
after_create(event, model)
model

The Model Class that storage is to be created for

7.3.9. destroy

This event is fired before and after you attempt to destroy the storage for the Model instances

before_destroy(event, model)
after_destroy(event, model)
model

The Model Class that storage is to be destroyed for

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