use Django 1.6 atomic if available instead of our own implementation
This commit is contained in:
parent
fd5c7bae01
commit
cf1a9f460f
|
@ -6,7 +6,7 @@ from models import (Journal, Tag, Template)
|
|||
import django.db.models
|
||||
from django.conf import settings
|
||||
|
||||
from decorator import commit_on_success
|
||||
from decorator import atomic
|
||||
|
||||
__all__ = ('record', 'error_record', 'Journal')
|
||||
__version__ = '1.22.0'
|
||||
|
@ -17,7 +17,7 @@ def unicode_truncate(s, length, encoding='utf-8'):
|
|||
encoded = s.encode(encoding)[:length]
|
||||
return encoded.decode(encoding, 'ignore')
|
||||
|
||||
@commit_on_success
|
||||
@atomic
|
||||
def record(tag, template, using=None, **kwargs):
|
||||
'''Record an event in the journal. The modification is done inside the
|
||||
current transaction.
|
||||
|
|
|
@ -1,57 +1,60 @@
|
|||
from functools import wraps
|
||||
from django.db import transaction, DEFAULT_DB_ALIAS
|
||||
|
||||
class Transaction(object):
|
||||
sid = None
|
||||
if hasattr(transaction, 'atomic'):
|
||||
atomic = transaction.atomic
|
||||
else:
|
||||
class Transaction(object):
|
||||
sid = None
|
||||
|
||||
def __init__(self, using=None):
|
||||
self.using = using
|
||||
def __init__(self, using=None):
|
||||
self.using = using
|
||||
|
||||
def __enter__(self):
|
||||
if transaction.is_managed():
|
||||
self.sid = transaction.savepoint()
|
||||
pass
|
||||
else:
|
||||
transaction.enter_transaction_management(using=self.using)
|
||||
def __enter__(self):
|
||||
if transaction.is_managed():
|
||||
self.sid = transaction.savepoint()
|
||||
pass
|
||||
else:
|
||||
transaction.enter_transaction_management(using=self.using)
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
if self.sid is not None:
|
||||
if exc_value is not None:
|
||||
transaction.savepoint_rollback(self.sid, using=self.using)
|
||||
else:
|
||||
try:
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
if self.sid is not None:
|
||||
if exc_value is not None:
|
||||
if transaction.is_dirty(using=self.using):
|
||||
transaction.rollback(using=self.using)
|
||||
else:
|
||||
if transaction.is_dirty(using=self.using):
|
||||
try:
|
||||
transaction.commit(using=self.using)
|
||||
except:
|
||||
transaction.savepoint_rollback(self.sid, using=self.using)
|
||||
else:
|
||||
try:
|
||||
if exc_value is not None:
|
||||
if transaction.is_dirty(using=self.using):
|
||||
transaction.rollback(using=self.using)
|
||||
raise
|
||||
finally:
|
||||
transaction.leave_transaction_management(using=self.using)
|
||||
else:
|
||||
if transaction.is_dirty(using=self.using):
|
||||
try:
|
||||
transaction.commit(using=self.using)
|
||||
except:
|
||||
transaction.rollback(using=self.using)
|
||||
raise
|
||||
finally:
|
||||
transaction.leave_transaction_management(using=self.using)
|
||||
|
||||
def __call__(self, func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
with self.__class__(using=self.using):
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
def __call__(self, func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
with self.__class__(using=self.using):
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
|
||||
def commit_on_success(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.
|
||||
"""
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
if callable(using):
|
||||
return Transaction(DEFAULT_DB_ALIAS)(using)
|
||||
return Transaction(using)
|
||||
def atomic(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.
|
||||
"""
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
if callable(using):
|
||||
return Transaction(DEFAULT_DB_ALIAS)(using)
|
||||
return Transaction(using)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue