equal
deleted
inserted
replaced
56 |
56 |
57 Operations are subclasses of the Operation class in `server/hook.py`, |
57 Operations are subclasses of the Operation class in `server/hook.py`, |
58 implementing `precommit_event` and other standard methods (wholly |
58 implementing `precommit_event` and other standard methods (wholly |
59 described in :ref:`operations_api`). |
59 described in :ref:`operations_api`). |
60 |
60 |
|
61 .. hint:: |
|
62 |
|
63 It is a good practice, to write unit tests for each hook. See an example in :ref:`hook_test` |
|
64 |
61 Events |
65 Events |
62 ------ |
66 ------ |
63 |
67 |
64 Hooks are mostly defined and used to handle `dataflow`_ operations. It |
68 Hooks are mostly defined and used to handle `dataflow`_ operations. It |
65 means as data gets in (entities added, updated, relations set or |
69 means as data gets in (entities added, updated, relations set or |
238 We would like to check that there is no cycle by the `subsidiary_of` |
242 We would like to check that there is no cycle by the `subsidiary_of` |
239 relation. This is best achieved in an Operation since all relations |
243 relation. This is best achieved in an Operation since all relations |
240 are likely to be set at commit time. |
244 are likely to be set at commit time. |
241 |
245 |
242 .. sourcecode:: python |
246 .. sourcecode:: python |
|
247 |
|
248 from cubicweb.server.hook import Hook, Operation, match_rtype |
243 |
249 |
244 def check_cycle(self, session, eid, rtype, role='subject'): |
250 def check_cycle(self, session, eid, rtype, role='subject'): |
245 parents = set([eid]) |
251 parents = set([eid]) |
246 parent = session.entity_from_eid(eid) |
252 parent = session.entity_from_eid(eid) |
247 while parent.related(rtype, role): |
253 while parent.related(rtype, role): |
298 CheckSubsidiaryCycleOp, rtype=self.rtype) |
304 CheckSubsidiaryCycleOp, rtype=self.rtype) |
299 |
305 |
300 class CheckSubsidiaryCycleOp(Operation): |
306 class CheckSubsidiaryCycleOp(Operation): |
301 |
307 |
302 def precommit_event(self): |
308 def precommit_event(self): |
303 for eid in self._cw.transaction_data['subsidiary_cycle_detection']: |
309 for eid in self.session.transaction_data['subsidiary_cycle_detection']: |
304 check_cycle(self.session, eid, self.rtype) |
310 check_cycle(self.session, eid, self.rtype) |
305 |
311 |
306 Here, we call set_operation with a session object, a specially forged |
312 Here, we call set_operation with a session object, a specially forged |
307 key, a value that is the actual payload of an individual operation (in |
313 key, a value that is the actual payload of an individual operation (in |
308 our case, the object of the subsidiary_of relation) , the class of the |
314 our case, the object of the subsidiary_of relation) , the class of the |