server/hookhelper.py
changeset 3890 d7a270f50f54
parent 2968 0e3460341023
parent 3877 7ca53fc72a0a
child 4011 394f853bb653
--- a/server/hookhelper.py	Sun Nov 08 21:53:18 2009 +0100
+++ b/server/hookhelper.py	Fri Nov 20 19:35:54 2009 +0100
@@ -11,6 +11,17 @@
 
 from cubicweb import RepositoryError
 
+def entity_oldnewvalue(entity, attr):
+    """returns the couple (old attr value, new attr value)
+    NOTE: will only work in a before_update_entity hook
+    """
+    # get new value and remove from local dict to force a db query to
+    # fetch old value
+    newvalue = entity.pop(attr, None)
+    oldvalue = getattr(entity, attr)
+    if newvalue is not None:
+        entity[attr] = newvalue
+    return oldvalue, newvalue
 
 @deprecated('[3.6] entity_name is deprecated, use entity.name')
 def entity_name(session, eid):