From 619dff806e12a616683842447fbac90a76d663a0 Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Thu, 11 Aug 2016 12:11:33 -0700 Subject: [PATCH] Support new MIDDLEWARE Django 1.10 Setting (#623) --- oauth2client/contrib/django_util/__init__.py | 25 ++++++++++++++----- tests/contrib/django_util/test_django_util.py | 14 +++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/oauth2client/contrib/django_util/__init__.py b/oauth2client/contrib/django_util/__init__.py index c27ded2..644a8f9 100644 --- a/oauth2client/contrib/django_util/__init__.py +++ b/oauth2client/contrib/django_util/__init__.py @@ -52,6 +52,9 @@ Add the helper to your INSTALLED_APPS: This helper also requires the Django Session Middleware, so ``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 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) self.client_id, self.client_secret = info - if ('django.contrib.sessions.middleware.SessionMiddleware' - not in settings_instance.MIDDLEWARE_CLASSES): + # Django 1.10 deprecated MIDDLEWARE_CLASSES in favor of MIDDLEWARE + 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( - 'The Google OAuth2 Helper requires session middleware to ' - 'be installed. Edit your MIDDLEWARE_CLASSES setting' - ' to include \'django.contrib.sessions.middleware.' - 'SessionMiddleware\'.') + 'Django settings has neither MIDDLEWARE nor MIDDLEWARE_CLASSES' + 'configured') + + 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_credentials_property) = _get_storage_model() diff --git a/tests/contrib/django_util/test_django_util.py b/tests/contrib/django_util/test_django_util.py index d708f62..15ac9cb 100644 --- a/tests/contrib/django_util/test_django_util.py +++ b/tests/contrib/django_util/test_django_util.py @@ -101,6 +101,20 @@ class OAuth2SetupTest(unittest.TestCase): object.__new__(django_util.OAuth2Settings), 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): STORAGE_MODEL = { 'model': 'tests.contrib.django_util.models.CredentialsModel',