hooks/workflow.py
changeset 2880 bfc8e1831290
parent 2847 c2ee28f4d4b1
child 2968 0e3460341023
equal deleted inserted replaced
2869:0cb160fd3cdf 2880:bfc8e1831290
    13 from cubicweb.interfaces import IWorkflowable
    13 from cubicweb.interfaces import IWorkflowable
    14 from cubicweb.selectors import entity_implements
    14 from cubicweb.selectors import entity_implements
    15 from cubicweb.server import hook
    15 from cubicweb.server import hook
    16 
    16 
    17 
    17 
       
    18 
    18 def previous_state(session, eid):
    19 def previous_state(session, eid):
    19     """return the state of the entity with the given eid,
    20     """return the state of the entity with the given eid,
    20     usually since it's changing in the current transaction. Due to internal
    21     usually since it's changing in the current transaction. Due to internal
    21     relation hooks, the relation may has been deleted at this point, so
    22     relation hooks, the relation may has been deleted at this point, so
    22     we have handle that
    23     we have handle that
    23     """
    24     """
    24     if session.added_in_transaction(eid):
    25     # don't check eid has been added in the current transaction, we don't want
    25         return
    26     # to miss previous state of entity whose state change in the same
       
    27     # transaction as it's being created
    26     pending = session.transaction_data.get('pendingrelations', ())
    28     pending = session.transaction_data.get('pendingrelations', ())
    27     for eidfrom, rtype, eidto in reversed(pending):
    29     for eidfrom, rtype, eidto in reversed(pending):
    28         if rtype == 'in_state' and eidfrom == eid:
    30         if rtype == 'in_state' and eidfrom == eid:
    29             rset = session.execute('Any S,N WHERE S eid %(x)s, S name N',
    31             rset = session.execute('Any S,N WHERE S eid %(x)s, S name N',
    30                                    {'x': eidto}, 'x')
    32                                    {'x': eidto}, 'x')
   137         if self._cw.added_in_transaction(self.eidfrom):
   139         if self._cw.added_in_transaction(self.eidfrom):
   138             # new entity, not needed
   140             # new entity, not needed
   139             return
   141             return
   140         entity = self._cw.entity_from_eid(self.eidfrom)
   142         entity = self._cw.entity_from_eid(self.eidfrom)
   141         try:
   143         try:
   142             entity.set_attributes(modification_date=datetime.now())
   144             entity.set_attributes(modification_date=datetime.now(),
       
   145                                   _cw_unsafe=True)
   143         except RepositoryError, ex:
   146         except RepositoryError, ex:
   144             # usually occurs if entity is coming from a read-only source
   147             # usually occurs if entity is coming from a read-only source
   145             # (eg ldap user)
   148             # (eg ldap user)
   146             self.warning('cant change modification date for %s: %s', entity, ex)
   149             self.warning('cant change modification date for %s: %s', entity, ex)