summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBojan Mihelac <bmihelac@mihelac.org>2015-04-08 07:33:42 (GMT)
committerBojan Mihelac <bmihelac@mihelac.org>2015-04-08 07:33:42 (GMT)
commit4c3bb4098affd3e952dcadca4a945336272d5fd5 (patch)
treeb7a46fa9b85ad3fd5c91ccecffac8a2cef40878e
parent7247e07484efb69dcc6d3e7cd74ef220f8cb84a4 (diff)
parente9869f05702cc1d2eb328615ca20de036c9abcca (diff)
downloaddjango-import-export-4c3bb4098affd3e952dcadca4a945336272d5fd5.zip
django-import-export-4c3bb4098affd3e952dcadca4a945336272d5fd5.tar.gz
django-import-export-4c3bb4098affd3e952dcadca4a945336272d5fd5.tar.bz2
Merge branch 'django-1.8-fixes-enter_transaction_management' into django-1.8-fixes
-rw-r--r--import_export/django_compat.py23
-rw-r--r--import_export/resources.py21
2 files changed, 34 insertions, 10 deletions
diff --git a/import_export/django_compat.py b/import_export/django_compat.py
new file mode 100644
index 0000000..c5d1ca4
--- /dev/null
+++ b/import_export/django_compat.py
@@ -0,0 +1,23 @@
+from __future__ import unicode_literals
+
+from django.db import transaction
+
+# transaction management for Django < 1.6
+
+def atomic(*args, **kw):
+ def noop_decorator(func):
+ return func # pass through
+
+ return noop_decorator
+
+def savepoint(*args, **kwargs):
+ transaction.enter_transaction_management()
+ transaction.managed(True)
+
+def savepoint_rollback(*args, **kwargs):
+ transaction.rollback()
+ transaction.leave_transaction_management()
+
+def savepoint_commit(*args, **kwargs):
+ transaction.commit()
+ transaction.leave_transaction_management()
diff --git a/import_export/resources.py b/import_export/resources.py
index 798a953..1de8dc5 100644
--- a/import_export/resources.py
+++ b/import_export/resources.py
@@ -11,7 +11,6 @@ from diff_match_patch import diff_match_patch
from django import VERSION
from django.utils.safestring import mark_safe
from django.utils import six
-from django.db import transaction
from django.db.models.fields import FieldDoesNotExist
from django.db.models.query import QuerySet
from django.conf import settings
@@ -23,6 +22,11 @@ from .instance_loaders import (
ModelInstanceLoader,
)
+try:
+ from django.db.transaction import atomic, savepoint, savepoint_rollback, savepoint_commit # noqa
+except ImportError:
+ from .django_compat import atomic, savepoint, savepoint_rollback, savepoint_commit # noqa
+
if VERSION < (1, 8):
from django.db.models.related import RelatedObject
@@ -286,6 +290,7 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
"""
pass
+ @atomic()
def import_data(self, dataset, dry_run=False, raise_errors=False,
use_transactions=None):
"""
@@ -306,8 +311,7 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
# when transactions are used we want to create/update/delete object
# as transaction will be rolled back if dry_run is set
real_dry_run = False
- transaction.enter_transaction_management()
- transaction.managed(True)
+ sp1 = savepoint()
else:
real_dry_run = dry_run
@@ -318,8 +322,7 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
result.base_errors.append(Error(repr(e), tb_info))
if raise_errors:
if use_transactions:
- transaction.rollback()
- transaction.leave_transaction_management()
+ savepoint_rollback(sp1)
raise
instance_loader = self._meta.instance_loader_class(self, dataset)
@@ -361,8 +364,7 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
row_result.errors.append(Error(e, tb_info))
if raise_errors:
if use_transactions:
- transaction.rollback()
- transaction.leave_transaction_management()
+ savepoint_rollback(sp1)
six.reraise(*sys.exc_info())
if (row_result.import_type != RowResult.IMPORT_TYPE_SKIP or
self._meta.report_skipped):
@@ -370,10 +372,9 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
if use_transactions:
if dry_run or result.has_errors():
- transaction.rollback()
+ savepoint_rollback(sp1)
else:
- transaction.commit()
- transaction.leave_transaction_management()
+ savepoint_commit(sp1)
return result