Author: Alex
Date: 2009-06-02 22:18:48 -0500 (Tue, 02 Jun 2009)
New Revision: 10911
Modified:
django/branches/soc2009/multidb/django/contrib/comments/models.py
django/branches/soc2009/multidb/django/core/management/commands/sqlindexes.py
django/branches/soc2009/multidb/django/core/management/commands/sqlreset.py
django/branches/soc2009/multidb/django/core/management/commands/sqlsequencereset.py
django/branches/soc2009/multidb/django/db/transaction.py
Log:
[soc2009/multidb] Updated the transaction decorators for use with multiple DB.
Modified: django/branches/soc2009/multidb/django/contrib/comments/models.py
===================================================================
--- django/branches/soc2009/multidb/django/contrib/comments/models.py
2009-06-03 02:39:02 UTC (rev 10910)
+++ django/branches/soc2009/multidb/django/contrib/comments/models.py
2009-06-03 03:18:48 UTC (rev 10911)
@@ -79,10 +79,10 @@
def __unicode__(self):
return "%s: %s..." % (self.name, self.comment[:50])
- def save(self, force_insert=False, force_update=False):
+ def save(self, *args, **kwargs):
if self.submit_date is None:
self.submit_date = datetime.datetime.now()
- super(Comment, self).save(force_insert, force_update)
+ super(Comment, self).save(*args, **kwargs)
def _get_userinfo(self):
"""
Modified:
django/branches/soc2009/multidb/django/core/management/commands/sqlindexes.py
===================================================================
---
django/branches/soc2009/multidb/django/core/management/commands/sqlindexes.py
2009-06-03 02:39:02 UTC (rev 10910)
+++
django/branches/soc2009/multidb/django/core/management/commands/sqlindexes.py
2009-06-03 03:18:48 UTC (rev 10911)
@@ -11,6 +11,7 @@
make_option('--database', action='store', dest='database',
default='default', help='Nominates a database to print the SQL '
'for. Defaults to the "default" database.'),
+
)
output_transaction = True
Modified:
django/branches/soc2009/multidb/django/core/management/commands/sqlreset.py
===================================================================
--- django/branches/soc2009/multidb/django/core/management/commands/sqlreset.py
2009-06-03 02:39:02 UTC (rev 10910)
+++ django/branches/soc2009/multidb/django/core/management/commands/sqlreset.py
2009-06-03 03:18:48 UTC (rev 10911)
@@ -11,6 +11,7 @@
make_option('--database', action='store', dest='database',
default='default', help='Nominates a database to print the SQL '
'for. Defaults to the "default" database.'),
+
)
output_transaction = True
Modified:
django/branches/soc2009/multidb/django/core/management/commands/sqlsequencereset.py
===================================================================
---
django/branches/soc2009/multidb/django/core/management/commands/sqlsequencereset.py
2009-06-03 02:39:02 UTC (rev 10910)
+++
django/branches/soc2009/multidb/django/core/management/commands/sqlsequencereset.py
2009-06-03 03:18:48 UTC (rev 10911)
@@ -10,6 +10,7 @@
make_option('--database', action='store', dest='database',
default='default', help='Nominates a database to print the SQL '
'for. Defaults to the "default" database.'),
+
)
output_transaction = True
Modified: django/branches/soc2009/multidb/django/db/transaction.py
===================================================================
--- django/branches/soc2009/multidb/django/db/transaction.py 2009-06-03
02:39:02 UTC (rev 10910)
+++ django/branches/soc2009/multidb/django/db/transaction.py 2009-06-03
03:18:48 UTC (rev 10911)
@@ -20,7 +20,7 @@
from functools import wraps
except ImportError:
from django.utils.functional import wraps # Python 2.3, 2.4 fallback.
-from django.db import connections
+from django.db import connections, DEFAULT_DB_ALIAS
from django.conf import settings
class TransactionManagementError(Exception):
@@ -259,60 +259,79 @@
# TODO update all of these for multi-db
-def autocommit(func):
+def autocommit(using_or_func=None):
"""
Decorator that activates commit on save. This is Django's default behavior;
this decorator is useful if you globally activated transaction management
in
your settings file and want the default behavior in some view functions.
"""
- def _autocommit(*args, **kw):
- try:
- enter_transaction_management(managed=False)
- managed(False)
- return func(*args, **kw)
- finally:
- leave_transaction_management()
- return wraps(func)(_autocommit)
+ def inner_autocommit(func, using=None):
+ def _autocommit(*args, **kw):
+ try:
+ enter_transaction_management(managed=False, using=using)
+ managed(False, using=using)
+ return func(*args, **kw)
+ finally:
+ leave_transaction_management(using=using)
+ return wraps(func)(_autocommit)
+ if using_or_func is None:
+ using_or_func = DEFAULT_DB_ALIAS
+ if callable(using_or_func):
+ return inner_autocommit(using_or_func, DEFAULT_DB_ALIAS)
+ return lambda func: inner_autocommit(func, using_or_func)
-def commit_on_success(func):
+
+def commit_on_success(func_or_using=None):
"""
This decorator activates commit on response. This way, if the view function
runs successfully, a commit is made; if the viewfunc produces an exception,
a rollback is made. This is one of the most common ways to do transaction
control in web apps.
"""
- def _commit_on_success(*args, **kw):
- try:
- enter_transaction_management()
- managed(True)
+ def inner_commit_on_success(func, using=None):
+ def _commit_on_success(*args, **kw):
try:
- res = func(*args, **kw)
- except:
- # All exceptions must be handled here (even string ones).
- if is_dirty():
- rollback()
- raise
- else:
- if is_dirty():
- commit()
- return res
- finally:
- leave_transaction_management()
- return wraps(func)(_commit_on_success)
+ enter_transaction_management(using=using)
+ managed(True, using=using)
+ try:
+ res = func(*args, **kw)
+ except:
+ # All exceptions must be handled here (even string ones).
+ if is_dirty(using=using):
+ rollback(using=using)
+ raise
+ else:
+ if is_dirty(using=using):
+ commit(using=using)
+ return res
+ finally:
+ leave_transaction_management(using=using)
+ return wraps(func)(_commit_on_success)
+ if func_or_using is None:
+ func_or_using = DEFAULT_DB_ALIAS
+ if callable(func_or_using):
+ return inner_commit_on_success(func_or_using, DEFAULT_DB_ALIAS)
+ return lambda func: inner_commit_on_success(func, func_or_using)
-def commit_manually(func):
+def commit_manually(func_or_using=None):
"""
Decorator that activates manual transaction control. It just disables
automatic transaction control and doesn't do any commit/rollback of its
own -- it's up to the user to call the commit and rollback functions
themselves.
"""
- def _commit_manually(*args, **kw):
- try:
- enter_transaction_management()
- managed(True)
- return func(*args, **kw)
- finally:
- leave_transaction_management()
+ def inner_commit_manually(func, using=None):
+ def _commit_manually(*args, **kw):
+ try:
+ enter_transaction_management(using=using)
+ managed(True, using=using)
+ return func(*args, **kw)
+ finally:
+ leave_transaction_management(using=using)
- return wraps(func)(_commit_manually)
+ return wraps(func)(_commit_manually)
+ if func_or_using is None:
+ func_or_using = DEFALUT_DB_ALIAS
+ if callable(func_or_using):
+ return inner_commit_manually(func_or_using, DEFAULT_DB_ALIAS)
+ return lambda func: inner_commit_manually(func, func_or_using)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---