#343839: update entity's modification date after changing its state
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 24 Jul 2009 17:59:49 +0200
changeset 2495 abcbadcc110a
parent 2494 ef8b86f6ca5a
child 2496 fbd1fd2ca312
#343839: update entity's modification date after changing its state
sobjects/hooks.py
--- a/sobjects/hooks.py	Fri Jul 24 17:50:41 2009 +0200
+++ b/sobjects/hooks.py	Fri Jul 24 17:59:49 2009 +0200
@@ -12,6 +12,24 @@
 from cubicweb.server.pool import PreCommitOperation
 
 
+class SetModificationDateOnStateChange(hooksmanager.Hook):
+    """update entity's modification date after changing its state"""
+    events = ('after_add_relation',)
+    accepts = ('in_state',)
+
+    def call(self, session, fromeid, rtype, toeid):
+        if fromeid in session.transaction_data.get('neweids', ()):
+            # new entity, not needed
+            return
+        entity = session.entity_from_eid(fromeid)
+        try:
+            entity.set_attributes(modification_date=datetime.now())
+        except RepositoryError, ex:
+            # usually occurs if entity is coming from a read-only source
+            # (eg ldap user)
+            self.warning('cant change modification date for %s: %s', entity, ex)
+
+
 class AddUpdateCWUserHook(Hook):
     """ensure user logins are stripped"""
     events = ('before_add_entity', 'before_update_entity',)