From 6e6722bbd2182ba28645644e858f67f80215f01f Mon Sep 17 00:00:00 2001
From: Benjamin Dauvergne <bdauvergne@entrouvert.com>
Date: Tue, 5 Aug 2014 18:56:07 +0200
Subject: [PATCH 4/6] [django-1.6] use ATOMIC_REQUESTS setting instead of
 TransactionMiddleware

Also remove all use of commit_on_success decorator which is useless
since all requests are atomic.

refs #5244
---
 authentic2/auth2_auth/auth2_ssl/backend.py  |    2 --
 authentic2/backends/ldap_backend.py         |    3 +--
 authentic2/management/commands/load-ldif.py |    4 ++--
 authentic2/saml/common.py                   |    1 -
 authentic2/settings.py                      |   12 +++++++++++-
 5 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/authentic2/auth2_auth/auth2_ssl/backend.py b/authentic2/auth2_auth/auth2_ssl/backend.py
index 434ec48..b5badf4 100644
--- a/authentic2/auth2_auth/auth2_ssl/backend.py
+++ b/authentic2/auth2_auth/auth2_ssl/backend.py
@@ -69,7 +69,6 @@ settings')
             return None
 
 
-    @transaction.commit_on_success
     def create_user(self, ssl_info):
         """
         This method creates a new django User and ClientCertificate record
@@ -100,7 +99,6 @@ settings')
         self.link_user(ssl_info, user)
         return user
 
-    @transaction.commit_on_success
     def link_user(self, ssl_info, user):
         """
         This method creates a new django User and ClientCertificate record
diff --git a/authentic2/backends/ldap_backend.py b/authentic2/backends/ldap_backend.py
index b894b3e..a490350 100644
--- a/authentic2/backends/ldap_backend.py
+++ b/authentic2/backends/ldap_backend.py
@@ -31,7 +31,7 @@ except ImportError:
 
 from authentic2.cache import get_shared_cache
 from authentic2.decorators import to_list
-from authentic2.compat import get_user_model, commit_on_success
+from authentic2.compat import get_user_model
 from authentic2.models import UserExternalId
 
 
@@ -701,7 +701,6 @@ class LDAPBackend(object):
                         .filter(user=user, source=block['realm']) \
                         .delete()
 
-    @commit_on_success
     def _return_user(self, uri, dn, username, password, conn, block):
         attributes = self.get_ldap_attributes(block, conn, dn)
         if attributes is None:
diff --git a/authentic2/management/commands/load-ldif.py b/authentic2/management/commands/load-ldif.py
index 94e7bb5..f9b828a 100644
--- a/authentic2/management/commands/load-ldif.py
+++ b/authentic2/management/commands/load-ldif.py
@@ -4,10 +4,10 @@ import json
 
 
 from django.core.management.base import BaseCommand, CommandError
-from django.db import transaction
 from django.contrib.auth import get_user_model
 
 
+from authentic2.compat import atomic
 from authentic2.hashers import olap_password_to_dj
 from authentic2.models import Attribute
 
@@ -136,7 +136,7 @@ class Command(BaseCommand):
     args = '<ldif_file...>'
     help = 'Load/update LDIF files as users'
 
-    @transaction.commit_on_success
+    @atomic
     def handle(self, *args, **options):
         options['verbosity'] = int(options['verbosity'])
         for arg in args:
diff --git a/authentic2/saml/common.py b/authentic2/saml/common.py
index 5b68d28..954766a 100644
--- a/authentic2/saml/common.py
+++ b/authentic2/saml/common.py
@@ -311,7 +311,6 @@ def get_manage_dump(request):
     d = LibertyManageDump.objects.filter(django_session_key = request.session.session_key)
     return d
 
-@transaction.commit_on_success
 def retrieve_metadata_and_create(request, provider_id, sp_or_idp):
     logger.debug('trying to load %s from wkl' % provider_id)
     if not provider_id.startswith('http'):
diff --git a/authentic2/settings.py b/authentic2/settings.py
index 6097fd2..31ea2fe 100644
--- a/authentic2/settings.py
+++ b/authentic2/settings.py
@@ -4,6 +4,7 @@ import json
 import glob
 import re
 
+import django
 from django.core.exceptions import ImproperlyConfigured
 from django.conf import global_settings
 
@@ -118,7 +119,16 @@ MIDDLEWARE_CLASSES = (
     'django.middleware.locale.LocaleMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
-    'django.middleware.transaction.TransactionMiddleware',
+)
+
+if django.VERSION < (1, 6, 0):
+    MIDDLEWARE_CLASSES += (
+        'django.middleware.transaction.TransactionMiddleware',
+    )
+else:
+    ATOMIC_REQUESTS = True
+
+MIDDLEWARE_CLASSES += (
     'authentic2.idp.middleware.DebugMiddleware',
     'authentic2.middleware.CollectIPMiddleware',
 )
-- 
1.7.10.4

