From f258521ebe55f61a97bfb69e97788b0f451618c1 Mon Sep 17 00:00:00 2001 From: Adnan Umer Date: Thu, 16 Mar 2017 00:47:17 +0500 Subject: [PATCH 1/7] Django 1.10 Support --- tenant_schemas/template_loaders.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tenant_schemas/template_loaders.py b/tenant_schemas/template_loaders.py index c8c98ee..7cf1342 100644 --- a/tenant_schemas/template_loaders.py +++ b/tenant_schemas/template_loaders.py @@ -4,16 +4,31 @@ multi-tenant setting """ import hashlib +from django import VERSION as DJANGO_VERSION 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.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 +DJANGO_1_10 = DJANGO_VERSION[1] >= 10 + +if DJANGO_1_10: + from django.template import Origin, TemplateDoesNotExist + + + def make_origin(engine, name, loader, template_name, dirs): + return Origin(name=name, template_name=template_name, loader=loader) +else: + from django.template.base import TemplateDoesNotExist + + + 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 +65,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 +148,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 From 1fcb4917336bb284035107edd31d3d39d49c411e Mon Sep 17 00:00:00 2001 From: Adnan Umer Date: Thu, 16 Mar 2017 00:52:07 +0500 Subject: [PATCH 2/7] Updated Django 1.10 Version Check --- tenant_schemas/template_loaders.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tenant_schemas/template_loaders.py b/tenant_schemas/template_loaders.py index 7cf1342..24c36da 100644 --- a/tenant_schemas/template_loaders.py +++ b/tenant_schemas/template_loaders.py @@ -14,7 +14,7 @@ from django.utils._os import safe_join from django.utils.encoding import force_bytes from tenant_schemas.postgresql_backend.base import FakeTenant -DJANGO_1_10 = DJANGO_VERSION[1] >= 10 +DJANGO_1_10 = DJANGO_VERSION[0] == 1 and DJANGO_VERSION[1] >= 10 if DJANGO_1_10: from django.template import Origin, TemplateDoesNotExist From 472456d4d227dc052e81197714ebbe854a9e6c9b Mon Sep 17 00:00:00 2001 From: Adnan Umer Date: Mon, 17 Apr 2017 15:52:37 +0500 Subject: [PATCH 3/7] Django 1.9 Support --- tenant_schemas/template_loaders.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tenant_schemas/template_loaders.py b/tenant_schemas/template_loaders.py index 24c36da..4889edf 100644 --- a/tenant_schemas/template_loaders.py +++ b/tenant_schemas/template_loaders.py @@ -4,28 +4,28 @@ multi-tenant setting """ import hashlib + from django import VERSION as DJANGO_VERSION from django.conf import settings from django.core.exceptions import ImproperlyConfigured 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 -DJANGO_1_10 = DJANGO_VERSION[0] == 1 and DJANGO_VERSION[1] >= 10 +DJANGO_1_9 = DJANGO_VERSION[0] == 1 and DJANGO_VERSION[1] >= 9 -if DJANGO_1_10: - from django.template import Origin, TemplateDoesNotExist +if DJANGO_1_9: + from django.template import Origin def make_origin(engine, name, loader, template_name, dirs): return Origin(name=name, template_name=template_name, loader=loader) else: - from django.template.base import TemplateDoesNotExist - - def make_origin(engine, name, loader, template_name, dirs): return engine.make_origin(name, loader, template_name, dirs) From c1842304c62fd5806c009cfe703acc35c5289444 Mon Sep 17 00:00:00 2001 From: Adnan Umer Date: Mon, 17 Apr 2017 15:53:56 +0500 Subject: [PATCH 4/7] Unit Tests for CachedLoader --- tenant_schemas/tests/__init__.py | 5 +-- .../tests/template_loader/__init__.py | 1 + .../template_loader/templates/hello.html | 1 + .../test_cached_template_loader.py | 31 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tenant_schemas/tests/template_loader/__init__.py create mode 100755 tenant_schemas/tests/template_loader/templates/hello.html create mode 100755 tenant_schemas/tests/template_loader/test_cached_template_loader.py 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") From c2aa3a1662848b631272eb5328b67a2676dee881 Mon Sep 17 00:00:00 2001 From: Adnan Umer Date: Mon, 17 Apr 2017 15:54:16 +0500 Subject: [PATCH 5/7] Migrated to TEMPLATES settings --- dts_test_project/dts_test_project/settings.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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/ From 2abbbdab433bcbb74f2d19ebc47f78122fb07ecb Mon Sep 17 00:00:00 2001 From: Gary Reynolds Date: Fri, 2 Jun 2017 06:56:57 +1000 Subject: [PATCH 6/7] Remove Django 1.9 from testing matrix, unsupported version. --- .travis.yml | 1 - tox.ini | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) 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/tox.ini b/tox.ini index 839fe27..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,9 +15,8 @@ deps = mock tblib dj18: Django~=1.8.0 - dj19: Django~=1.9.0 dj110: Django~=1.10.0 - dj111: Django~=1.11rc1 + dj111: Django~=1.11.0 changedir = dts_test_project From df359d2bfcf890751ec9f363a26da91360dca5b3 Mon Sep 17 00:00:00 2001 From: Gary Reynolds Date: Fri, 2 Jun 2017 17:56:37 +1000 Subject: [PATCH 7/7] Deal with backwards compatibility by exception. --- tenant_schemas/template_loaders.py | 9 +++------ tenant_schemas/test/cases.py | 1 - 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/tenant_schemas/template_loaders.py b/tenant_schemas/template_loaders.py index 4889edf..6224a26 100644 --- a/tenant_schemas/template_loaders.py +++ b/tenant_schemas/template_loaders.py @@ -5,7 +5,6 @@ multi-tenant setting import hashlib -from django import VERSION as DJANGO_VERSION from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.db import connection @@ -17,15 +16,13 @@ from django.utils.encoding import force_bytes from tenant_schemas.postgresql_backend.base import FakeTenant -DJANGO_1_9 = DJANGO_VERSION[0] == 1 and DJANGO_VERSION[1] >= 9 - -if DJANGO_1_9: +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) -else: + +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) 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'