Skip to content

Commit

Permalink
Added support for new-style middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
charettes committed Nov 11, 2017
1 parent 38c58c0 commit c9320b7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
42 changes: 27 additions & 15 deletions tenancy/middleware.py
Expand Up @@ -8,26 +8,37 @@
from . import get_tenant_model
from .settings import HOST_NAME

try:
from django.utils.deprecation import MiddlewareMixin
except ImportError:
MiddlewareMixin = object

class TenantHostMiddleware(object):
def __init__(self):

class TenantHostMiddleware(MiddlewareMixin):
def __init__(self, get_response=None):
super(TenantHostMiddleware, self).__init__(get_response)
try:
import django_hosts # NOQA
except ImportError:
raise ImproperlyConfigured(
'You must install django-hosts in order to '
'use `TenantHostMiddleware`.'
'You must install django-hosts in order to use `TenantHostMiddleware`.'
)
django_hosts_middleware = 'django_hosts.middleware.HostsRequestMiddleware'
path = "%s.%s" % (self.__module__, self.__class__.__name__)
for middleware in settings.MIDDLEWARE_CLASSES:
if middleware == 'django_hosts.middleware.HostsRequestMiddleware':
break
elif middleware == path:
raise ImproperlyConfigured(
"Make sure that 'django_hosts.middleware.HostsRequestMiddleware' "
"is placed before '%s' in your `MIDDLEWARE_CLASSES` "
"setting." % path
)
for setting in ('MIDDLEWARE', 'MIDDLEWARE_CLASSES'):
middlewares = getattr(settings, setting, None)
if middlewares is None:
continue
for middleware in middlewares:
if middleware == django_hosts_middleware:
break
elif middleware == path:
raise ImproperlyConfigured(
"Make sure '%s' appears before '%s' in your `%s` setting." % (
django_hosts_middleware, path, setting,
)
)
break
self.tenant_model = get_tenant_model()
self.attr_name = self.tenant_model.ATTR_NAME

Expand All @@ -45,14 +56,15 @@ def process_request(self, request):
setattr(request, self.attr_name, tenant)


class GlobalTenantMiddleware(object):
class GlobalTenantMiddleware(MiddlewareMixin):
"""
Middleware that assigns the request's tenant attribute to the default
connection object. This unfortunate global state is required in order to
allow things such as a tenant custom user with the required auth backend.
"""

def __init__(self):
def __init__(self, get_response=None):
super(GlobalTenantMiddleware, self).__init__(get_response)
self.attr_name = get_tenant_model().ATTR_NAME

def get_global_state(self):
Expand Down
33 changes: 24 additions & 9 deletions tests/test_hosts.py
Expand Up @@ -2,6 +2,7 @@

from unittest import skipIf, skipUnless

import django
from django.core.exceptions import ImproperlyConfigured
from django.test.utils import override_settings
from django.utils.encoding import force_bytes
Expand All @@ -21,7 +22,7 @@ def django_hosts_installed_setup(func):
func = override_settings(
DEFAULT_HOST='default',
ROOT_HOSTCONF='tests.hosts',
MIDDLEWARE_CLASSES=[
MIDDLEWARE=[
'django_hosts.middleware.HostsRequestMiddleware',
'tenancy.middleware.TenantHostMiddleware',
'django_hosts.middleware.HostsResponseMiddleware',
Expand Down Expand Up @@ -49,21 +50,35 @@ def test_not_installed(self):
TenantHostMiddleware
)

@skipUnless(django.VERSION >= (1, 10), 'New-style middleware was introduced in Django 1.10.')
@skipUnless(django_hosts, 'django-hosts is not installed.')
@override_settings(
MIDDLEWARE_CLASSES=(
MIDDLEWARE=(
'tenancy.middleware.TenantHostMiddleware',
'django_hosts.middleware.HostsRequestMiddleware',
),
)
def test_wrong_middleware_order(self):
message = (
"Make sure 'django_hosts.middleware.HostsRequestMiddleware' appears before "
"'tenancy.middleware.TenantHostMiddleware' in your `MIDDLEWARE` setting."
)
self.assertRaisesMessage(ImproperlyConfigured, message, TenantHostMiddleware)

@skipUnless(django.VERSION < (2, 0), 'Old-style middleware was support was removed in Django 2.0.')
@skipUnless(django_hosts, 'django-hosts is not installed.')
@override_settings(
MIDDLEWARE_CLASSES=(
'tenancy.middleware.TenantHostMiddleware',
'django_hosts.middleware.HostsRequestMiddleware',
)l
)
def test_wrong_order(self):
self.assertRaisesMessage(
ImproperlyConfigured,
"Make sure that 'django_hosts.middleware.HostsRequestMiddleware' is "
"placed before 'tenancy.middleware.TenantHostMiddleware' in your "
"`MIDDLEWARE_CLASSES` setting.",
TenantHostMiddleware
def test_wrong_middleware_classes_order(self):
message = (
"Make sure 'django_hosts.middleware.HostsRequestMiddleware' appears before "
"'tenancy.middleware.TenantHostMiddleware' in your `MIDDLEWARE_CLASSES` setting."
)
self.assertRaisesMessage(ImproperlyConfigured, message, TenantHostMiddleware)

@django_hosts_installed_setup
def test_tenant_not_found(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_middleware.py
Expand Up @@ -11,7 +11,7 @@

@override_settings(
ROOT_URLCONF='tests.urls',
MIDDLEWARE_CLASSES=(
MIDDLEWARE=(
'tenancy.middleware.GlobalTenantMiddleware',
),
)
Expand Down

0 comments on commit c9320b7

Please sign in to comment.