[Cubicweb] Workflows and Hooks

Aurélien Campéas aurelien.campeas at logilab.fr
Wed Aug 1 11:37:53 CEST 2012

Le 01/08/2012 11:17, Stéphane Bugat a écrit :
> Dear all

Hi Stéphane,

> I'm trying to capture the moment when a workflow changes its status to perform a relation creation.

A quite common need indeed.

> To be more specific, I have created a workflowable entity, 'ContactNotification',
 > and I want that when the corresponding status of this entity becomes 
 > a link is created between the 2 persons (the one that created the 
contact notification,
 > and the targeted person by this notification).
> For that, I've tried to create a hook selecting instances of 'State' on events
 >'after_update_entity', get the workflowable entity with a 
``related('in_state', 'object')``,
 > then get both persons involved, and finally set a symmetric relation 
 >both of them (with something like 

sometimes I write such hooks like:

class FooHappens(Hook):
     __regid__ = 'mycube.foo_happens'
     events = ('after_add_relation',)
     __select__ = (Hook.__select__ &
                   match_rtype('in_state', frometypes=('Notif',)))

     def __call__(self):
         state = self._cw.entity_from_eid(self.eidto)
  	if state.name = 'foo':

This may not be the only/prettiest/... way but certainly useful.

> Tests performed shown that once a ContactNotification is accepted, the hook
 >is not called at all (I've traced it using ugly *print* statements in 
the __call__ of the hook)...

I personnally don't think print statements are an ugly debugging tool ....

> My 2 questions are:
>    1. Why this kind of hook is not called on such entities?

State entities dont get updated on workflowable transitions (see 
cw/schemas/workflow.py for its structure & relations)... (They are 
mostly immutable entities, which used in conjunction with Transitions, 
describe a workflow.)

What gets updated is the workflowable entity state & history, through 
the (X in_state State) (current) and (TrInfo wf_info_for X) (all) relations.

>    2. Is there another way to do it?

That leaves at least another way using wf_info_for (as an exercice ;-)

> Thanks
> _______________________________________________
> Cubicweb mailing list
> Cubicweb at lists.cubicweb.org
> http://lists.cubicweb.org/mailman/listinfo/cubicweb

More information about the Cubicweb mailing list