summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBojan Mihelac <bmihelac@mihelac.org>2015-04-08 07:33:31 (GMT)
committerBojan Mihelac <bmihelac@mihelac.org>2015-04-08 07:33:31 (GMT)
commite9869f05702cc1d2eb328615ca20de036c9abcca (patch)
tree6bf4a4ec8e7673c4b70d0fd75aec8fc1956bf5e5
parent7b867686f928071618ac3ec14306a34948f3dcbf (diff)
downloaddjango-import-export-e9869f05702cc1d2eb328615ca20de036c9abcca.zip
django-import-export-e9869f05702cc1d2eb328615ca20de036c9abcca.tar.gz
django-import-export-e9869f05702cc1d2eb328615ca20de036c9abcca.tar.bz2
Do not use private transaction api for Django > 1.6
-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 ca542d7..7c9ce39 100644
--- a/import_export/resources.py
+++ b/import_export/resources.py
@@ -10,7 +10,6 @@ from diff_match_patch import diff_match_patch
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.db.models.related import RelatedObject
@@ -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
+
try:
from django.utils.encoding import force_text
@@ -279,6 +283,7 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
"""
pass
+ @atomic()
def import_data(self, dataset, dry_run=False, raise_errors=False,
use_transactions=None):
"""
@@ -299,8 +304,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
@@ -311,8 +315,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)
@@ -354,8 +357,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):
@@ -363,10 +365,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