Skip to content

Commit

Permalink
Merge 49ec662 into aa7cb3b
Browse files Browse the repository at this point in the history
  • Loading branch information
matllubos committed Mar 16, 2020
2 parents aa7cb3b + 49ec662 commit 372bec1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
3 changes: 1 addition & 2 deletions chamber/importers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ def get_fields_dict(self, row):
Beware, it aligns the lists of fields and row values with Nones to allow for adding fields not found in the CSV.
Whitespace around the value of the cell is stripped.
"""
return {k: getattr(self, 'clean_{}'.format(k), lambda x: x)(v.strip() if isinstance(v, str)
else None)
return {k: getattr(self, 'clean_{}'.format(k), lambda x: x)(v.strip() if isinstance(v, str) else None)
for k, v in zip_longest(self.get_fields(), row)}

def _pre_import_rows(self, row_count):
Expand Down
49 changes: 34 additions & 15 deletions chamber/utils/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@
from django.conf import settings
from django.db import transaction, DEFAULT_DB_ALIAS
from django.db.transaction import get_connection
from django.utils.decorators import ContextDecorator

from contextlib import contextmanager, ContextDecorator


logger = logging.getLogger(__name__)


def atomic(func):
def atomic(func=None):
"""
Decorator helper that overrides django atomic decorator and automatically adds create revision.
Decorator and context manager that overrides django atomic decorator and automatically adds create revision.
The _atomic closure is required to achieve save ContextDecorator that nest more inner context decorator.
More here https://stackoverflow.com/questions/45589718/combine-two-context-managers-into-one
"""
try:
from reversion.revisions import create_revision

return transaction.atomic(create_revision()(func))
except ImportError:
return transaction.atomic(func)
@contextmanager
def _atomic():
try:
from reversion.revisions import create_revision

with transaction.atomic(), create_revision():
yield
except ImportError:
with transaction.atomic():
yield

if func:
return _atomic()(func)
else:
return _atomic()


class TransactionSignalsContext:
Expand Down Expand Up @@ -113,16 +126,22 @@ def transaction_signals(using=None):
return TransactionSignals(using)


def atomic_with_signals(func):
def atomic_with_signals(func=None):
"""
Atomic decorator with transaction signals.
Atomic decorator and context manager with transaction signals.
The _atomic_with_signals closure is required to achieve save ContextDecorator that nest more inner context
decorator. More here https://stackoverflow.com/questions/45589718/combine-two-context-managers-into-one
"""
try:
from reversion.revisions import create_revision

return transaction.atomic(create_revision()(transaction_signals(func)))
except ImportError:
return transaction.atomic(transaction_signals(func))
@contextmanager
def _atomic_with_signals():
with atomic(), transaction_signals():
yield

if func:
return _atomic_with_signals()(func)
else:
return _atomic_with_signals()


class UniqueOnSuccessCallable:
Expand Down

0 comments on commit 372bec1

Please sign in to comment.