Ticket #14051: transaction_signals.patch
File transaction_signals.patch, 5.0 KB (added by , 14 years ago) |
---|
-
new file django/db/signals.py
diff --git a/django/db/signals.py b/django/db/signals.py new file mode 100644 index 0000000..0cf666a
- + 1 from django.dispatch import Signal 2 3 pre_commit = Signal() 4 post_commit = Signal() 5 6 pre_rollback = Signal() 7 post_rollback = Signal() -
django/db/transaction.py
diff --git a/django/db/transaction.py b/django/db/transaction.py index 3c767f1..085e54b 100644
a b try: 21 21 except ImportError: 22 22 from django.utils.functional import wraps # Python 2.4 fallback. 23 23 from django.db import connections, DEFAULT_DB_ALIAS 24 from django.db.signals import pre_commit, post_commit, pre_rollback, post_rollback 24 25 from django.conf import settings 25 26 26 27 class TransactionManagementError(Exception): … … def commit(using=None): 195 196 """ 196 197 if using is None: 197 198 using = DEFAULT_DB_ALIAS 199 200 pre_commit.send(sender=using) 198 201 connection = connections[using] 199 202 connection._commit() 200 203 set_clean(using=using) 204 post_commit.send(sender=using) 201 205 202 206 def rollback(using=None): 203 207 """ … … def rollback(using=None): 205 209 """ 206 210 if using is None: 207 211 using = DEFAULT_DB_ALIAS 212 213 pre_rollback.send(sender=using) 214 208 215 connection = connections[using] 209 216 connection._rollback() 210 217 set_clean(using=using) 218 post_rollback.send(sender=using) 219 211 220 212 221 def savepoint(using=None): 213 222 """ -
docs/ref/signals.txt
diff --git a/docs/ref/signals.txt b/docs/ref/signals.txt index 69ed48b..f48f288 100644
a b Arguments that are sent with this signal: 327 327 ``sender`` 328 328 The model class which was just prepared. 329 329 330 331 Transaction signals 332 =================== 333 334 .. module:: django.db.signals 335 :synopsis: Signals sent when database transactions are committed/rolled back. 336 337 The :mod:`django.db.signals` module defines a set of signals sent by the 338 transaction system. These signals are sent even if you are not using a transaction 339 capable database. See the :doc:`database transaction documentation </topics/db/transactions>` 340 for more information about database transactions. 341 342 343 pre_commit 344 -------- 345 346 .. attribute:: django.db.signals.pre_commit 347 :module: 348 349 .. ^^^^^^^ this :module: hack keeps Sphinx from prepending the module. 350 351 Before a transaction is committed to the database, this signal is sent. 352 353 Arguments sent with this signal: 354 355 ``sender`` 356 The database connection alias where the transaction is to be committed. 357 358 post_commit 359 --------- 360 361 .. data:: django.db.signals.post_commit 362 :module: 363 364 Like pre_commit, but this one is sent after the transaction has been committed. 365 366 Arguments sent with this signal: 367 368 ``sender`` 369 As above: The database connection alias. 370 371 372 pre_rollback 373 -------- 374 375 .. attribute:: django.db.signals.pre_rollback 376 :module: 377 378 .. ^^^^^^^ this :module: hack keeps Sphinx from prepending the module. 379 380 Before a transaction is being rollbacked the database, this signal is sent. 381 382 Arguments sent with this signal: 383 384 ``sender`` 385 The database connection alias where the transaction is to be rollbacked. 386 387 388 post_rollback 389 --------- 390 391 .. data:: django.db.signals.post_rollback 392 :module: 393 394 Like pre_rollback, but this one is sent after the transaction has been rollbacked. 395 396 Arguments sent with this signal: 397 398 ``sender`` 399 As above: The database connection alias. 400 401 330 402 Management signals 331 403 ================== 332 404 -
tests/modeltests/transactions/models.py
diff --git a/tests/modeltests/transactions/models.py b/tests/modeltests/transactions/models.py index df0dd80..5578596 100644
a b class Reporter(models.Model): 22 22 23 23 __test__ = {'API_TESTS':""" 24 24 >>> from django.db import connection, transaction 25 >>> from django.db.signals import pre_commit, post_commit, pre_rollback, post_rollback 25 26 """} 26 27 27 28 from django.conf import settings … … Traceback (most recent call last): 129 130 ... 130 131 TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK 131 132 133 # Make sure transaction signals is sent 134 135 >>> def transaction_signal_test(signal, sender, **kwargs): 136 ... signals = { 137 ... pre_commit: 'pre_commit', 138 ... post_commit: 'post_commit', 139 ... pre_rollback: 'pre_rollback', 140 ... post_rollback: 'post_rollback' 141 ... } 142 ... 143 ... print "signal: ", signals.get(signal) 144 ... print 'sender: ', sender 145 146 >>> pre_commit.connect(transaction_signal_test) 147 >>> post_commit.connect(transaction_signal_test) 148 >>> pre_rollback.connect(transaction_signal_test) 149 >>> post_rollback.connect(transaction_signal_test) 150 151 >>> transaction.commit() 152 signal: pre_commit 153 sender: default 154 signal: post_commit 155 sender: default 156 157 >>> transaction.rollback() 158 signal: pre_rollback 159 sender: default 160 signal: post_rollback 161 sender: default 162 132 163 """ 133 164 165 134 166 # Regression for #11900: If a function wrapped by commit_on_success writes a 135 167 # transaction that can't be committed, that transaction should be rolled back. 136 168 # The bug is only visible using the psycopg2 backend, though