Skip to content

Commit

Permalink
Break out test for whether we are in a transaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfllaw committed Jun 4, 2010
1 parent 5c8028a commit 5badf7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 2 additions & 6 deletions django_lean/lean_retention/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core import mail
from django.db import transaction

from django_lean.lean_retention.models import (DailyActivity, LastActivity,
SignIn)
from django_lean.lean_retention import signals
from django_lean.utils import get_current_site
from django_lean.utils import get_current_site, in_transaction


class BaseTrackingMiddleware(object):
Expand All @@ -19,9 +17,7 @@ def medium(self, request, response):
return 'Default'

def process_response(self, request, response):
if transaction.is_managed() and not hasattr(mail, 'outbox'):
# We must ignore this assertion when running inside a
# Django test case, which uses transactions.
if in_transaction():
raise ImproperlyConfigured('%s cannot be inside a transaction.' %
self.__class__.__name__)
if response.status_code != 200:
Expand Down
10 changes: 10 additions & 0 deletions django_lean/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from django.contrib.sites.models import Site
from django.core import mail
from django.db import transaction


def get_current_site():
if Site._meta.installed:
return Site.objects.get_current()
return None

def in_transaction(test_ignore=True):
result = transaction.is_managed()
if test_ignore:
# Ignore when running inside a Django test case, which uses
# transactions.
result = result and not hasattr(mail, 'outbox')
return result

0 comments on commit 5badf7b

Please sign in to comment.