Event

class connector.event.Event[source]

Bases: object

An event contains consumers called when the event is fired.

The events are able to filter the consumers to execute by model name.

The usage of an event is to instantiate an Event object:

on_my_event = Event()

An event always have at least the 2 following arguments:

  • session
  • model_name

Then to subscribe one or more consumers, an event has a function:

def do_something(session, model_name, a, b):
    print "Event was fired with arguments: %s, %s" % (a, b)

# active on all models
on_my_event.subscribe(do_something)

def do_something_product(session, model_name, a, b):
    print ("Event was fired on a product "
           "with arguments: %s, %s" % (a, b))

# active only on product.product
on_my_event.subscribe(do_something_product,
                      model_names='product.product')

We can also replace a consumer:

def do_something_product2(session, model_name, a, b):
    print "Consumer 2"
    print ("Event was fired on a product "
          "with arguments: %s, %s" % (a, b))

on_my_event.subscribe(do_something_product2,
                      replacing=do_something_product)

Finally, we fire the event:

on_my_event.fire(session, 'res.users', 'value_a', 'value_b')

A consumer can be subscribed using a decorator:

@on_my_event
def do_other_thing(session, model_name, a, b):
    print 'foo'

@on_my_event(replacing=do_other_thing)
def print_bar(session, model_name, a, b):
    print 'bar'
fire(session, model_name, *args, **kwargs)[source]

Call each consumer subscribed on the event with the given arguments and keyword arguments.

All the consumers which were subscribed globally (no model name) or which are subscribed on the same model

Parameters:
  • session (connector.session.Session) – current session
  • model_name (str) – name of the model
  • args – arguments propagated to the consumer The second argument of args is the model name. The first argument is the session.
  • kwargs – keyword arguments propagated to the consumer
has_consumer_for(session, model_name)[source]

Return True if at least one consumer is registered for the model.

subscribe(consumer, model_names=None, replacing=None)[source]

Subscribe a consumer on the event

Parameters:
  • consumer – the function to register on the event
  • model_names – the consumer will be active only on these models, active on all models if None
  • replacing – the function beeing replaced by this new one.
unsubscribe(consumer, model_names=None)[source]

Remove a consumer from the event

Parameters:
  • consumer – the function to unsubscribe
  • model_names – remove only for these models or remove a consumer which is active on all models if None.
connector.event.on_record_create = <connector.event.Event object>

on_record_create is fired when one record has been created.

Listeners should take the following arguments:

  • session: ConnectorSession object
  • model_name: name of the model
  • record_id: id of the created record
  • vals: field values updated, e.g {‘field_name’: field_value, …}

on_record_unlink is fired when one record has been deleted.

Listeners should take the following arguments:

  • session: ConnectorSession object
  • model_name: name of the model
  • record_id: id of the record
connector.event.on_record_write = <connector.event.Event object>

on_record_write is fired when one record has been updated.

Listeners should take the following arguments:

  • session: ConnectorSession object
  • model_name: name of the model
  • record_id: id of the record
  • vals: field values of the new record, e.g {‘field_name’: field_value, …}