Skip to content

Commit

Permalink
Merge pull request #433 from AlexvEck/features/#430-better-check-for-…
Browse files Browse the repository at this point in the history
…django-tenant_schemas-placement

#430 - Check wether tenant_schemas comes before any django apps instead of before all apps.
  • Loading branch information
goodtune committed Jan 25, 2017
2 parents 1a83c52 + 50a9c69 commit 6c96dee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ To make use of shared and tenant-specific applications, there are two settings c
.. code-block:: python
SHARED_APPS = (
'tenant_schemas', # mandatory
'tenant_schemas', # mandatory, should always be before any django app
'customers', # you must list the app where your tenant model resides in
'django.contrib.contenttypes',
Expand Down
6 changes: 4 additions & 2 deletions tenant_schemas/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ def best_practice(app_configs, **kwargs):

errors = []

if INSTALLED_APPS[0] != 'tenant_schemas':
django_index = next(i for i, s in enumerate(INSTALLED_APPS) if s.startswith('django.'))
if INSTALLED_APPS.index('tenant_schemas') > django_index:
errors.append(
Warning("You should put 'tenant_schemas' first in INSTALLED_APPS.",
Warning("You should put 'tenant_schemas' before any django "
"core applications in INSTALLED_APPS.",
obj="django.conf.settings",
hint="This is necessary to overwrite built-in django "
"management commands with their schema-aware "
Expand Down
18 changes: 16 additions & 2 deletions tenant_schemas/tests/test_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,29 @@ def test_database_routers(self):
'django.contrib.messages',
'django.contrib.staticfiles',
])
def test_tenant_schemas_last_installed_apps(self):
def test_tenant_schemas_before_django_installed_apps(self):
self.assertBestPractice([
Warning("You should put 'tenant_schemas' first in INSTALLED_APPS.",
Warning("You should put 'tenant_schemas' before any django "
"core applications in INSTALLED_APPS.",
obj="django.conf.settings",
hint="This is necessary to overwrite built-in django "
"management commands with their schema-aware "
"implementations."),
])

@override_settings(INSTALLED_APPS=[
'dts_test_app',
'customers',
'tenant_schemas',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
])
def test_tenant_schemas_after_custom_apps_in_installed_apps(self):
self.assertBestPractice([])

@override_settings(TENANT_APPS=())
def test_tenant_apps_empty(self):
self.assertBestPractice([
Expand Down

0 comments on commit 6c96dee

Please sign in to comment.