From be98e7bb66c08dff3c82bd2169f20309768791ba Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Mon, 14 Nov 2016 10:43:14 +0000 Subject: [PATCH 1/6] In Django 1.9, an Origin instance is always returned, regardless of DEBUG setting, so we need to extract the Origin's directory name. --- templatefinder/utils.py | 10 +++++++--- tox.ini | 26 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/templatefinder/utils.py b/templatefinder/utils.py index b956135..2cf43b8 100644 --- a/templatefinder/utils.py +++ b/templatefinder/utils.py @@ -54,11 +54,15 @@ def find_all_templates(pattern='*.html'): loader = loader_class(Engine.get_default()) else: loader = loader_class() - for dir in loader.get_template_sources(''): - for root, dirnames, filenames in os.walk(dir): + for directory in loader.get_template_sources(''): + # In 1.9, Origin is always set. + # https://docs.djangoproject.com/en/1.9/releases/1.9/#template-loaderorigin-and-stringorigin-are-removed + if hasattr(directory, 'loader_name'): + directory = directory.name + for root, dirnames, filenames in os.walk(directory): for basename in filenames: filename = os.path.join(root, basename) - rel_filename = filename[len(dir)+1:] + rel_filename = filename[len(directory)+1:] if fnmatch.fnmatch(filename, pattern) or \ fnmatch.fnmatch(basename, pattern) or \ fnmatch.fnmatch(rel_filename, pattern): diff --git a/tox.ini b/tox.ini index c176ce9..176efe6 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py3.4-django1.7, py3.3-django1.7, py2.7-django1.7, py3.4-django1.6, py3.3-django1.6, py2.7-django1.6, py2.6-django1.6, py3.3-django1.5, py2.7-django1.5, py2.6-django1.5, py2.7-django1.4, py2.6-django1.4, py2.7-django1.3, py2.6-django1.3 +envlist = py3.4-django1.9, py2.7-django1.9, py3.4-django1.8, py3.3-django1.8, py2.7-django1.8, py3.4-django1.7, py3.3-django1.7, py2.7-django1.7, py3.4-django1.6, py3.3-django1.6, py2.7-django1.6, py2.6-django1.6, py3.3-django1.5, py2.7-django1.5, py2.6-django1.5, py2.7-django1.4, py2.6-django1.4, py2.7-django1.3, py2.6-django1.3 [testenv] commands = {envpython} setup.py test @@ -8,6 +8,30 @@ commands = {envpython} setup.py test ;basepython = pypy ;deps = Django>=1.5,<1.6 +; Django 1.9.x + +[testenv:py3.4-django1.9] +basepython = python3.4 +deps = Django>=1.9,<1.10 + +[testenv:py2.7-django1.9] +basepython = python2.7 +deps = Django>=1.9,<1.10 + +; Django 1.8.x + +[testenv:py3.4-django1.8] +basepython = python3.4 +deps = Django>=1.8,<1.9 + +[testenv:py3.3-django1.8] +basepython = python3.3 +deps = Django>=1.8,<1.9 + +[testenv:py2.7-django1.8] +basepython = python2.7 +deps = Django>=1.8,<1.9 + ; Django 1.7.x [testenv:py3.4-django1.7] From 2d6cb1e362e4d4826972f4442d002eb6d0bee062 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Mon, 14 Nov 2016 11:06:40 +0000 Subject: [PATCH 2/6] In Django 1.8+, TEMPLATE_LOADERS is deprecated and the loaders are attached to the engine, as such, we must extract them from the appropriate place, falling back to the old setting as necessary. In 1.10, it looks like _accepts_engine_in_init is gone, and all loaders require the engine be passed down to their constructor. --- .travis.yml | 7 +++++++ templatefinder/tests.py | 5 ++++- templatefinder/utils.py | 18 ++++++++++++++---- tox.ini | 12 +++++++++++- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 630e266..35c3c81 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,9 @@ python: - "3.4" env: + - DJANGO="Django>=1.10,<1.11" + - DJANGO="Django>=1.9,<1.10" + - DJANGO="Django>=1.8,<1.9" - DJANGO="Django>=1.7,<1.8" - DJANGO="Django>=1.6,<1.7" - DJANGO="Django>=1.5,<1.6" @@ -20,6 +23,10 @@ script: 'python setup.py test' matrix: exclude: + - python: "2.6" + env: DJANGO="Django>=1.9,<1.10" + - python: "2.6" + env: DJANGO="Django>=1.8,<1.9" - python: "2.6" env: DJANGO="Django>=1.7,<1.8" - python: "3.3" diff --git a/templatefinder/tests.py b/templatefinder/tests.py index de01d84..5ca5345 100644 --- a/templatefinder/tests.py +++ b/templatefinder/tests.py @@ -1,5 +1,8 @@ from django.conf import settings -from django.utils import unittest +try: + from django.utils import unittest +except ImportError: + import unittest from . import find_all_templates, flatten_template_loaders, template_choices diff --git a/templatefinder/utils.py b/templatefinder/utils.py index 2cf43b8..a42ccfd 100644 --- a/templatefinder/utils.py +++ b/templatefinder/utils.py @@ -23,7 +23,10 @@ class Engine(object): @staticmethod def get_default(): - return None + return Engine() + @property + def loaders(self): + return settings.TEMPLATE_LOADERS __all__ = ('find_all_templates', 'flatten_template_loaders', 'template_choices') @@ -42,7 +45,8 @@ def find_all_templates(pattern='*.html'): .. important:: At the moment egg loader is not supported. """ templates = [] - template_loaders = flatten_template_loaders(settings.TEMPLATE_LOADERS) + engine = Engine.get_default() + template_loaders = flatten_template_loaders(engine.loaders) for loader_name in template_loaders: module, klass = loader_name.rsplit('.', 1) if loader_name in ( @@ -51,9 +55,15 @@ def find_all_templates(pattern='*.html'): ): loader_class = getattr(import_module(module), klass) if getattr(loader_class, '_accepts_engine_in_init', False): - loader = loader_class(Engine.get_default()) + loader = loader_class(engine) else: - loader = loader_class() + try: + loader = loader_class() + except TypeError: + # probably Django which is modern enough to require + # engine being passed to the init, but has since removed + # _accepts_engine_in_init + loader = loader_class(engine) for directory in loader.get_template_sources(''): # In 1.9, Origin is always set. # https://docs.djangoproject.com/en/1.9/releases/1.9/#template-loaderorigin-and-stringorigin-are-removed diff --git a/tox.ini b/tox.ini index 176efe6..819d63f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py3.4-django1.9, py2.7-django1.9, py3.4-django1.8, py3.3-django1.8, py2.7-django1.8, py3.4-django1.7, py3.3-django1.7, py2.7-django1.7, py3.4-django1.6, py3.3-django1.6, py2.7-django1.6, py2.6-django1.6, py3.3-django1.5, py2.7-django1.5, py2.6-django1.5, py2.7-django1.4, py2.6-django1.4, py2.7-django1.3, py2.6-django1.3 +envlist = py3.4-django1.10, py2.7-django1.10, py3.4-django1.9, py2.7-django1.9, py3.4-django1.8, py3.3-django1.8, py2.7-django1.8, py3.4-django1.7, py3.3-django1.7, py2.7-django1.7, py3.4-django1.6, py3.3-django1.6, py2.7-django1.6, py2.6-django1.6, py3.3-django1.5, py2.7-django1.5, py2.6-django1.5, py2.7-django1.4, py2.6-django1.4, py2.7-django1.3, py2.6-django1.3 [testenv] commands = {envpython} setup.py test @@ -8,6 +8,16 @@ commands = {envpython} setup.py test ;basepython = pypy ;deps = Django>=1.5,<1.6 +; Django 1.10.x + +[testenv:py3.4-django1.10] +basepython = python3.4 +deps = Django>=1.10,<1.11 + +[testenv:py2.7-django1.10] +basepython = python2.7 +deps = Django>=1.10,<1.11 + ; Django 1.9.x [testenv:py3.4-django1.9] From dcc392cd2574743b6b8be47df2393df23a1808a2 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Mon, 14 Nov 2016 12:40:26 +0000 Subject: [PATCH 3/6] Need to provide the TEMPLATES setting on 1.10, and it'll be preferred on 1.8+ --- runtests.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/runtests.py b/runtests.py index bfefe48..a06b076 100644 --- a/runtests.py +++ b/runtests.py @@ -8,6 +8,9 @@ def main(): + template_dirs = ( + os.path.join(os.path.dirname(__file__), 'templatefinder', 'test_project', 'templates'), + ) settings.configure( DATABASES={ 'default': { @@ -17,9 +20,16 @@ def main(): INSTALLED_APPS=( 'templatefinder.test_project.testapp', ), - TEMPLATE_DIRS=( - os.path.join(os.path.dirname(__file__), 'templatefinder', 'test_project', 'templates'), - ) + TEMPLATE_DIRS=template_dirs, + TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': template_dirs, + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + ], + }, + }] ) if hasattr(django, 'setup'): django.setup() From 8e77afc3747ee18582da21b31ea80651778ce3bf Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Mon, 14 Nov 2016 12:44:24 +0000 Subject: [PATCH 4/6] Django 1.9+ does not support Python 3.3 - this is an import error "from importlib.util import find_spec as importlib_find" For build failure, see https://travis-ci.org/kezabelle/django-template-finder/jobs/175687136 --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.travis.yml b/.travis.yml index 35c3c81..f168205 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,12 +23,18 @@ script: 'python setup.py test' matrix: exclude: + - python: "2.6" + env: DJANGO="Django>=1.10,<1.11" - python: "2.6" env: DJANGO="Django>=1.9,<1.10" - python: "2.6" env: DJANGO="Django>=1.8,<1.9" - python: "2.6" env: DJANGO="Django>=1.7,<1.8" + - python: "3.3" + env: DJANGO="Django>=1.10,<1.11" + - python: "3.3" + env: DJANGO="Django>=1.9,<1.10" - python: "3.3" env: DJANGO="Django>=1.4,<1.5" - python: "3.3" From b3132a155bf1b23ce8fb313c0037590b40fc79f8 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Sat, 4 Mar 2017 10:11:21 +0000 Subject: [PATCH 5/6] Refs #12 - Document the version for which django.utils.unittest stops being available. --- templatefinder/tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/templatefinder/tests.py b/templatefinder/tests.py index 5ca5345..e9e7e7f 100644 --- a/templatefinder/tests.py +++ b/templatefinder/tests.py @@ -2,6 +2,7 @@ try: from django.utils import unittest except ImportError: + # Django removed its unittest vendoring in 1.9 import unittest from . import find_all_templates, flatten_template_loaders, template_choices From ff92788f276823404be8ac9f4990de92117e0cb7 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Sat, 4 Mar 2017 10:19:17 +0000 Subject: [PATCH 6/6] Refs #12 - Since we're already handling TypeError for passing an engine to the loader, we can avoid checking the transitory variable _accepts_engine_in_init --- templatefinder/utils.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/templatefinder/utils.py b/templatefinder/utils.py index a42ccfd..c3e04ba 100644 --- a/templatefinder/utils.py +++ b/templatefinder/utils.py @@ -54,16 +54,15 @@ def find_all_templates(pattern='*.html'): 'django.template.loaders.filesystem.Loader', ): loader_class = getattr(import_module(module), klass) - if getattr(loader_class, '_accepts_engine_in_init', False): + try: + # Between 1.8 and 1.10, its possible to check + # _accepts_engine_in_init to see if it accepts one, but the + # normal suite of loaders we likely care about all want one. loader = loader_class(engine) - else: - try: - loader = loader_class() - except TypeError: - # probably Django which is modern enough to require - # engine being passed to the init, but has since removed - # _accepts_engine_in_init - loader = loader_class(engine) + except TypeError: + # Prior to the template refactor in 1.8, loaders never + # accepted an engine. + loader = loader_class() for directory in loader.get_template_sources(''): # In 1.9, Origin is always set. # https://docs.djangoproject.com/en/1.9/releases/1.9/#template-loaderorigin-and-stringorigin-are-removed