diff --git a/.travis.yml b/.travis.yml index 845cc71..d83cf90 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,6 @@ addons: install: pip install -q tox-travis env: - DJANGO=1.8 - - DJANGO=1.9 - DJANGO=1.10 - DJANGO=1.11 matrix: diff --git a/dts_test_project/dts_test_project/settings.py b/dts_test_project/dts_test_project/settings.py index 245b017..9378c67 100644 --- a/dts_test_project/dts_test_project/settings.py +++ b/dts_test_project/dts_test_project/settings.py @@ -10,8 +10,8 @@ https://docs.djangoproject.com/en/1.8/ref/settings/ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os -BASE_DIR = os.path.dirname(os.path.dirname(__file__)) +BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ @@ -28,7 +28,6 @@ ALLOWED_HOSTS = [] DEFAULT_FILE_STORAGE = 'tenant_schemas.storage.TenantFileSystemStorage' - # Application definition SHARED_APPS = ( @@ -74,7 +73,6 @@ ROOT_URLCONF = 'dts_test_project.urls' WSGI_APPLICATION = 'dts_test_project.wsgi.application' - # Database # https://docs.djangoproject.com/en/1.8/ref/settings/#databases @@ -111,6 +109,16 @@ TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.messages.context_processors.messages', ) +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'OPTIONS': { + 'context_processors': TEMPLATE_CONTEXT_PROCESSORS + }, + } +] + # Internationalization # https://docs.djangoproject.com/en/1.8/topics/i18n/ @@ -124,7 +132,6 @@ USE_L10N = True USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.8/howto/static-files/ diff --git a/tenant_schemas/postgresql_backend/base.py b/tenant_schemas/postgresql_backend/base.py index 8929d2e..6e940fa 100644 --- a/tenant_schemas/postgresql_backend/base.py +++ b/tenant_schemas/postgresql_backend/base.py @@ -1,13 +1,14 @@ import re import warnings - -import django.db.utils import psycopg2 + from django.conf import settings from django.core.exceptions import ImproperlyConfigured, ValidationError +import django.db.utils +from tenant_schemas.utils import get_public_schema_name, get_limit_set_calls from tenant_schemas.postgresql_backend.introspection import DatabaseSchemaIntrospection -from tenant_schemas.utils import get_limit_set_calls, get_public_schema_name + ORIGINAL_BACKEND = getattr(settings, 'ORIGINAL_BACKEND', 'django.db.backends.postgresql_psycopg2') # Django 1.9+ takes care to rename the default backend to 'django.db.backends.postgresql' diff --git a/tenant_schemas/template_loaders.py b/tenant_schemas/template_loaders.py index c8c98ee..6224a26 100644 --- a/tenant_schemas/template_loaders.py +++ b/tenant_schemas/template_loaders.py @@ -4,16 +4,28 @@ multi-tenant setting """ import hashlib + from django.conf import settings from django.core.exceptions import ImproperlyConfigured -from django.template.base import TemplateDoesNotExist, Template -from django.utils.encoding import force_bytes -from django.utils._os import safe_join from django.db import connection +from django.template import TemplateDoesNotExist +from django.template.base import Template from django.template.loaders.base import Loader as BaseLoader +from django.utils._os import safe_join +from django.utils.encoding import force_bytes from tenant_schemas.postgresql_backend.base import FakeTenant +try: + from django.template import Origin + + def make_origin(engine, name, loader, template_name, dirs): + return Origin(name=name, template_name=template_name, loader=loader) + +except ImportError: # Django 1.8 backwards compatibility + def make_origin(engine, name, loader, template_name, dirs): + return engine.make_origin(name, loader, template_name, dirs) + class CachedLoader(BaseLoader): is_usable = True @@ -50,7 +62,7 @@ class CachedLoader(BaseLoader): except TemplateDoesNotExist: pass else: - origin = self.engine.make_origin(display_name, loader, name, dirs) + origin = make_origin(self.engine, display_name, loader, name, dirs) result = template, origin break self.find_template_cache[key] = result @@ -133,4 +145,5 @@ class FilesystemLoader(BaseLoader): else: error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory." raise TemplateDoesNotExist(error_msg) + load_template_source.is_usable = True diff --git a/tenant_schemas/test/cases.py b/tenant_schemas/test/cases.py index d7a92f8..beef8dd 100644 --- a/tenant_schemas/test/cases.py +++ b/tenant_schemas/test/cases.py @@ -2,7 +2,6 @@ from django.conf import settings from django.core.management import call_command from django.db import connection from django.test import TestCase - from tenant_schemas.utils import get_public_schema_name, get_tenant_model ALLOWED_TEST_DOMAIN = '.test.com' diff --git a/tenant_schemas/tests/__init__.py b/tenant_schemas/tests/__init__.py index 09a3f81..2b6c0f2 100644 --- a/tenant_schemas/tests/__init__.py +++ b/tenant_schemas/tests/__init__.py @@ -1,5 +1,6 @@ -from .test_routes import * -from .test_tenants import * +from .template_loader import * from .test_cache import * from .test_log import * +from .test_routes import * +from .test_tenants import * from .test_utils import * diff --git a/tenant_schemas/tests/template_loader/__init__.py b/tenant_schemas/tests/template_loader/__init__.py new file mode 100644 index 0000000..ac17d9d --- /dev/null +++ b/tenant_schemas/tests/template_loader/__init__.py @@ -0,0 +1 @@ +from .test_cached_template_loader import CachedLoaderTests diff --git a/tenant_schemas/tests/template_loader/templates/hello.html b/tenant_schemas/tests/template_loader/templates/hello.html new file mode 100755 index 0000000..7f54a62 --- /dev/null +++ b/tenant_schemas/tests/template_loader/templates/hello.html @@ -0,0 +1 @@ +Hello! (Django templates) diff --git a/tenant_schemas/tests/template_loader/test_cached_template_loader.py b/tenant_schemas/tests/template_loader/test_cached_template_loader.py new file mode 100755 index 0000000..39facfe --- /dev/null +++ b/tenant_schemas/tests/template_loader/test_cached_template_loader.py @@ -0,0 +1,31 @@ +import os + +from django.template.loader import get_template +from django.test import SimpleTestCase, override_settings + + +@override_settings( + TEMPLATES=[ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [ + os.path.join(os.path.dirname(__file__), "templates") + ], + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.request', + ], + 'loaders': [ + ('tenant_schemas.template_loaders.CachedLoader', ( + 'tenant_schemas.template_loaders.FilesystemLoader', + 'django.template.loaders.filesystem.Loader' + )) + ] + }, + } + ] +) +class CachedLoaderTests(SimpleTestCase): + def test_get_template(self): + template = get_template("hello.html") + self.assertEqual(template.render(), "Hello! (Django templates)\n") diff --git a/tox.ini b/tox.ini index 05d4d6a..29ee264 100644 --- a/tox.ini +++ b/tox.ini @@ -1,10 +1,9 @@ [tox] -envlist = py{27,35}-dj{18,19,110,111}-{standard,parallel} +envlist = py{27,35}-dj{18,110,111}-{standard,parallel} [travis:env] DJANGO = 1.8: dj18-{standard,parallel} - 1.9: dj19-{standard,parallel} 1.10: dj110-{standard,parallel} 1.11: dj111-{standard,parallel} @@ -16,7 +15,6 @@ deps = mock tblib dj18: Django~=1.8.0 - dj19: Django~=1.9.0 dj110: Django~=1.10.0 dj111: Django~=1.11.0