Support new MIDDLEWARE Django 1.10 Setting (#623)

This commit is contained in:
Bill Prin
2016-08-11 12:11:33 -07:00
committed by Jon Wayne Parrott
parent f439dcbef4
commit 619dff806e
2 changed files with 33 additions and 6 deletions

View File

@@ -52,6 +52,9 @@ Add the helper to your INSTALLED_APPS:
This helper also requires the Django Session Middleware, so This helper also requires the Django Session Middleware, so
``django.contrib.sessions.middleware`` should be in INSTALLED_APPS as well. ``django.contrib.sessions.middleware`` should be in INSTALLED_APPS as well.
MIDDLEWARE or MIDDLEWARE_CLASSES (in Django versions <1.10) should also
contain the string 'django.contrib.sessions.middleware.SessionMiddleware'.
Add the client secrets created earlier to the settings. You can either Add the client secrets created earlier to the settings. You can either
specify the path to the credentials file in JSON format specify the path to the credentials file in JSON format
@@ -338,13 +341,23 @@ class OAuth2Settings(object):
info = _get_oauth2_client_id_and_secret(settings_instance) info = _get_oauth2_client_id_and_secret(settings_instance)
self.client_id, self.client_secret = info self.client_id, self.client_secret = info
if ('django.contrib.sessions.middleware.SessionMiddleware' # Django 1.10 deprecated MIDDLEWARE_CLASSES in favor of MIDDLEWARE
not in settings_instance.MIDDLEWARE_CLASSES): middleware_settings = getattr(settings_instance, 'MIDDLEWARE', None)
if middleware_settings is None:
middleware_settings = getattr(
settings_instance, 'MIDDLEWARE_CLASSES', None)
if middleware_settings is None:
raise exceptions.ImproperlyConfigured( raise exceptions.ImproperlyConfigured(
'The Google OAuth2 Helper requires session middleware to ' 'Django settings has neither MIDDLEWARE nor MIDDLEWARE_CLASSES'
'be installed. Edit your MIDDLEWARE_CLASSES setting' 'configured')
' to include \'django.contrib.sessions.middleware.'
'SessionMiddleware\'.') if ('django.contrib.sessions.middleware.SessionMiddleware' not in
middleware_settings):
raise exceptions.ImproperlyConfigured(
'The Google OAuth2 Helper requires session middleware to '
'be installed. Edit your MIDDLEWARE_CLASSES or MIDDLEWARE '
'setting to include \'django.contrib.sessions.middleware.'
'SessionMiddleware\'.')
(self.storage_model, self.storage_model_user_property, (self.storage_model, self.storage_model_user_property,
self.storage_model_credentials_property) = _get_storage_model() self.storage_model_credentials_property) = _get_storage_model()

View File

@@ -101,6 +101,20 @@ class OAuth2SetupTest(unittest.TestCase):
object.__new__(django_util.OAuth2Settings), object.__new__(django_util.OAuth2Settings),
django.conf.settings) django.conf.settings)
def test_no_middleware(self):
django.conf.settings.MIDDLEWARE_CLASSES = None
with self.assertRaises(exceptions.ImproperlyConfigured):
django_util.OAuth2Settings.__init__(
object.__new__(django_util.OAuth2Settings),
django.conf.settings)
def test_middleware_no_classes(self):
django.conf.settings.MIDDLEWARE = (
django.conf.settings.MIDDLEWARE_CLASSES)
django.conf.settings.MIDDLEWARE_CLASSES = None
# primarily testing this doesn't raise an exception
django_util.OAuth2Settings(django.conf.settings)
def test_storage_model(self): def test_storage_model(self):
STORAGE_MODEL = { STORAGE_MODEL = {
'model': 'tests.contrib.django_util.models.CredentialsModel', 'model': 'tests.contrib.django_util.models.CredentialsModel',