Commits

Anonymous committed 0fd1118

Break out test for whether we are in a transaction.

Comments (0)

Files changed (2)

django_lean/lean_retention/middleware.py

 
 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):
         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:

django_lean/utils.py

 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