Author: phd
Date: 2009-02-21 11:38:17 -0700 (Sat, 21 Feb 2009)
New Revision: 3792
Modified:
SQLObject/trunk/docs/News.txt
SQLObject/trunk/sqlobject/events.py
SQLObject/trunk/sqlobject/main.py
SQLObject/trunk/sqlobject/tests/test_events.py
Log:
A patch by Gabriel <ga...@op...>: added RowDestroyedSignal and RowUpdatedSignal .
Modified: SQLObject/trunk/docs/News.txt
===================================================================
--- SQLObject/trunk/docs/News.txt 2009-02-21 18:14:44 UTC (rev 3791)
+++ SQLObject/trunk/docs/News.txt 2009-02-21 18:38:17 UTC (rev 3792)
@@ -27,6 +27,8 @@
* Added some support for schemas in PostgreSQL.
+* RowDestroyedSignal and RowUpdatedSignal were added.
+
Minor features
--------------
Modified: SQLObject/trunk/sqlobject/events.py
===================================================================
--- SQLObject/trunk/sqlobject/events.py 2009-02-21 18:14:44 UTC (rev 3791)
+++ SQLObject/trunk/sqlobject/events.py 2009-02-21 18:38:17 UTC (rev 3792)
@@ -115,6 +115,17 @@
row can be deleted without first fetching it?
"""
+class RowDestroyedSignal(Signal):
+ """
+ Called after an instance is deleted. Sender is the instance's
+ class. Arguments are ``(instance)``.
+
+ This is called before the post_funcs of RowDestroySignal
+
+ Note: this is not called when an instance is destroyed through
+ garbage collection.
+ """
+
class RowUpdateSignal(Signal):
"""
Called when an instance is updated through a call to ``.set()``
@@ -124,6 +135,14 @@
current values, simply look at ``instance``.
"""
+class RowUpdatedSignal(Signal):
+ """
+ Called when an instance is updated through a call to ``.set()``
+ (or a column attribute assignment). The arguments are
+ ``(instance)``. This is run *after* the instance is updated;
+ Works better with lazyUpdate = True
+ """
+
class AddColumnSignal(Signal):
"""
Called when a column is added to a class, with arguments ``(cls,
Modified: SQLObject/trunk/sqlobject/main.py
===================================================================
--- SQLObject/trunk/sqlobject/main.py 2009-02-21 18:14:44 UTC (rev 3791)
+++ SQLObject/trunk/sqlobject/main.py 2009-02-21 18:38:17 UTC (rev 3792)
@@ -991,6 +991,11 @@
finally:
self._SO_writeLock.release()
+ post_funcs = []
+ self.sqlmeta.send(events.RowUpdatedSignal, self, post_funcs)
+ for func in post_funcs:
+ func()
+
def expire(self):
if self.sqlmeta.expired:
return
@@ -1043,6 +1048,11 @@
if self.sqlmeta.cacheValues:
setattr(self, instanceName(name), value)
+ post_funcs = []
+ self.sqlmeta.send(events.RowUpdatedSignal, self, post_funcs)
+ for func in post_funcs:
+ func()
+
def set(self, _suppress_set_sig=False, **kw):
if not self.sqlmeta._creating and not getattr(self.sqlmeta, "row_update_sig_suppress", False) and not _suppress_set_sig:
self.sqlmeta.send(events.RowUpdateSignal, self, kw)
@@ -1130,6 +1140,11 @@
finally:
self._SO_writeLock.release()
+ post_funcs = []
+ self.sqlmeta.send(events.RowUpdatedSignal, self, post_funcs)
+ for func in post_funcs:
+ func()
+
def _SO_selectInit(self, row):
for col, colValue in zip(self.sqlmeta.columnList, row):
if col.to_python:
@@ -1566,6 +1581,11 @@
for func in post_funcs:
func()
+ post_funcs = []
+ self.sqlmeta.send(events.RowDestroyedSignal, self, post_funcs)
+ for func in post_funcs:
+ func()
+
def delete(cls, id, connection=None):
obj = cls.get(id, connection=connection)
obj.destroySelf()
Modified: SQLObject/trunk/sqlobject/tests/test_events.py
===================================================================
--- SQLObject/trunk/sqlobject/tests/test_events.py 2009-02-21 18:14:44 UTC (rev 3791)
+++ SQLObject/trunk/sqlobject/tests/test_events.py 2009-02-21 18:38:17 UTC (rev 3792)
@@ -50,6 +50,14 @@
f.destroySelf()
assert watcher.log == [(f, [])]
+def test_row_destroyed():
+ setupClass(EventTester)
+ watcher = make_listen(events.RowDestroyedSignal)
+ f = EventTester(name='foo')
+ assert not watcher.log
+ f.destroySelf()
+ assert watcher.log == [(f, [])]
+
def test_row_update():
setupClass(EventTester)
watcher = make_listen(events.RowUpdateSignal)
@@ -61,6 +69,15 @@
(f, {'name': 'bar2'}),
(f, {'name': 'bar3'})]
+def test_row_updated():
+ setupClass(EventTester)
+ watcher = make_listen(events.RowUpdatedSignal)
+ f = EventTester(name='bar')
+ assert not watcher.log
+ f.name = 'bar2'
+ f.set(name='bar3')
+ assert watcher.log == [(f, []), (f, [])]
+
def test_add_column():
setupClass(EventTester)
watcher = make_listen(events.AddColumnSignal)
|