server/hook.py
changeset 10006 8391bf718485
parent 9608 e4d9a489ec3f
child 10351 91e63306e277
equal deleted inserted replaced
10005:7769d0f61810 10006:8391bf718485
     1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
   413 
   413 
   414 
   414 
   415 for event in ALL_HOOKS:
   415 for event in ALL_HOOKS:
   416     CWRegistryStore.REGISTRY_FACTORY['%s_hooks' % event] = HooksRegistry
   416     CWRegistryStore.REGISTRY_FACTORY['%s_hooks' % event] = HooksRegistry
   417 
   417 
   418 @deprecated('[3.10] use entity.cw_edited.oldnewvalue(attr)')
       
   419 def entity_oldnewvalue(entity, attr):
       
   420     return entity.cw_edited.oldnewvalue(attr)
       
   421 
       
   422 
   418 
   423 # some hook specific predicates #################################################
   419 # some hook specific predicates #################################################
   424 
   420 
   425 @objectify_predicate
   421 @objectify_predicate
   426 def enabled_category(cls, req, **kwargs):
   422 def enabled_category(cls, req, **kwargs):
   761             return None
   757             return None
   762         return -(i + 1)
   758         return -(i + 1)
   763 
   759 
   764     def handle_event(self, event):
   760     def handle_event(self, event):
   765         """delegate event handling to the opertaion"""
   761         """delegate event handling to the opertaion"""
   766         if event == 'postcommit_event' and hasattr(self, 'commit_event'):
       
   767             warn('[3.10] %s: commit_event method has been replaced by postcommit_event'
       
   768                  % self.__class__, DeprecationWarning)
       
   769             self.commit_event() # pylint: disable=E1101
       
   770         getattr(self, event)()
   762         getattr(self, event)()
   771 
   763 
   772     def precommit_event(self):
   764     def precommit_event(self):
   773         """the observed connections set is preparing a commit"""
   765         """the observed connections set is preparing a commit"""
   774 
   766 
   900         op = self.cnx.transaction_data.pop(self.data_key)
   892         op = self.cnx.transaction_data.pop(self.data_key)
   901         assert op is self, "Bad handling of operation data, found %s instead of %s for key %s" % (
   893         assert op is self, "Bad handling of operation data, found %s instead of %s for key %s" % (
   902             op, self, self.data_key)
   894             op, self, self.data_key)
   903         return self._container
   895         return self._container
   904 
   896 
   905 
       
   906 @deprecated('[3.10] use opcls.get_instance(cnx, **opkwargs).add_data(value)')
       
   907 def set_operation(cnx, datakey, value, opcls, containercls=set, **opkwargs):
       
   908     """Function to ease applying a single operation on a set of data, avoiding
       
   909     to create as many as operation as they are individual modification. You
       
   910     should try to use this instead of creating on operation for each `value`,
       
   911     since handling operations becomes coslty on massive data import.
       
   912 
       
   913     Arguments are:
       
   914 
       
   915     * `cnx`, the current connection
       
   916 
       
   917     * `datakey`, a specially forged key that will be used as key in
       
   918       cnx.transaction_data
       
   919 
       
   920     * `value` that is the actual payload of an individual operation
       
   921 
       
   922     * `opcls`, the class of the operation. An instance is created on the first
       
   923       call for the given key, and then subsequent calls will simply add the
       
   924       payload to the container (hence `opkwargs` is only used on that first
       
   925       call)
       
   926 
       
   927     * `containercls`, the container class that should be instantiated to hold
       
   928       payloads.  An instance is created on the first call for the given key, and
       
   929       then subsequent calls will add the data to the existing container. Default
       
   930       to a set. Give `list` if you want to keep arrival ordering.
       
   931 
       
   932     * more optional parameters to give to the operation (here the rtype which do not
       
   933       vary accross operations).
       
   934 
       
   935     The body of the operation must then iterate over the values that have been mapped
       
   936     in the transaction_data dictionary to the forged key, e.g.:
       
   937 
       
   938     .. sourcecode:: python
       
   939 
       
   940            for value in self._cw.transaction_data.pop(datakey):
       
   941                ...
       
   942 
       
   943     .. Note::
       
   944        **poping** the key from `transaction_data` is not an option, else you may
       
   945        get unexpected data loss in some case of nested hooks.
       
   946     """
       
   947     try:
       
   948         # Search for cnx.transaction_data[`datakey`] (expected to be a set):
       
   949         # if found, simply append `value`
       
   950         _container_add(cnx.transaction_data[datakey], value)
       
   951     except KeyError:
       
   952         # else, initialize it to containercls([`value`]) and instantiate the given
       
   953         # `opcls` operation class with additional keyword arguments
       
   954         opcls(cnx, **opkwargs)
       
   955         cnx.transaction_data[datakey] = containercls()
       
   956         _container_add(cnx.transaction_data[datakey], value)
       
   957 
   897 
   958 
   898 
   959 class LateOperation(Operation):
   899 class LateOperation(Operation):
   960     """special operation which should be called after all possible (ie non late)
   900     """special operation which should be called after all possible (ie non late)
   961     operations
   901     operations