Removed dirs parameter in template engine methods and related funtions.

Per deprecation timeline.
This commit is contained in:
Tim Graham 2015-09-03 16:12:22 -04:00
parent 5e450c52aa
commit b3641512c8
11 changed files with 24 additions and 152 deletions

View File

@ -12,7 +12,7 @@ from django.http import (
) )
from django.template import RequestContext, loader from django.template import RequestContext, loader
from django.template.engine import ( from django.template.engine import (
_context_instance_undefined, _dictionary_undefined, _dirs_undefined, _context_instance_undefined, _dictionary_undefined
) )
from django.utils import six from django.utils import six
from django.utils.encoding import force_text from django.utils.encoding import force_text
@ -21,14 +21,13 @@ from django.utils.functional import Promise
def render_to_response(template_name, context=None, def render_to_response(template_name, context=None,
context_instance=_context_instance_undefined, context_instance=_context_instance_undefined,
content_type=None, status=None, dirs=_dirs_undefined, content_type=None, status=None,
dictionary=_dictionary_undefined, using=None): dictionary=_dictionary_undefined, using=None):
""" """
Returns a HttpResponse whose content is filled with the result of calling Returns a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments. django.template.loader.render_to_string() with the passed arguments.
""" """
if (context_instance is _context_instance_undefined if (context_instance is _context_instance_undefined
and dirs is _dirs_undefined
and dictionary is _dictionary_undefined): and dictionary is _dictionary_undefined):
# No deprecated arguments were passed - use the new code path # No deprecated arguments were passed - use the new code path
content = loader.render_to_string(template_name, context, using=using) content = loader.render_to_string(template_name, context, using=using)
@ -36,7 +35,7 @@ def render_to_response(template_name, context=None,
else: else:
# Some deprecated arguments were passed - use the legacy code path # Some deprecated arguments were passed - use the legacy code path
content = loader.render_to_string( content = loader.render_to_string(
template_name, context, context_instance, dirs, dictionary, template_name, context, context_instance, dictionary,
using=using) using=using)
return HttpResponse(content, content_type, status) return HttpResponse(content, content_type, status)
@ -45,7 +44,7 @@ def render_to_response(template_name, context=None,
def render(request, template_name, context=None, def render(request, template_name, context=None,
context_instance=_context_instance_undefined, context_instance=_context_instance_undefined,
content_type=None, status=None, content_type=None, status=None,
dirs=_dirs_undefined, dictionary=_dictionary_undefined, dictionary=_dictionary_undefined,
using=None): using=None):
""" """
Returns a HttpResponse whose content is filled with the result of calling Returns a HttpResponse whose content is filled with the result of calling
@ -53,7 +52,6 @@ def render(request, template_name, context=None,
Uses a RequestContext by default. Uses a RequestContext by default.
""" """
if (context_instance is _context_instance_undefined if (context_instance is _context_instance_undefined
and dirs is _dirs_undefined
and dictionary is _dictionary_undefined): and dictionary is _dictionary_undefined):
# No deprecated arguments were passed - use the new code path # No deprecated arguments were passed - use the new code path
# In Django 1.10, request should become a positional argument. # In Django 1.10, request should become a positional argument.
@ -66,7 +64,7 @@ def render(request, template_name, context=None,
else: else:
context_instance = RequestContext(request) context_instance = RequestContext(request)
content = loader.render_to_string( content = loader.render_to_string(
template_name, context, context_instance, dirs, dictionary, template_name, context, context_instance, dictionary,
using=using) using=using)
return HttpResponse(content, content_type, status) return HttpResponse(content, content_type, status)

View File

@ -10,7 +10,7 @@ from django.apps import apps
from django.conf import settings from django.conf import settings
from django.template import TemplateDoesNotExist from django.template import TemplateDoesNotExist
from django.template.context import Context, RequestContext, make_context from django.template.context import Context, RequestContext, make_context
from django.template.engine import Engine, _dirs_undefined from django.template.engine import Engine
from django.template.library import InvalidTemplateLibrary from django.template.library import InvalidTemplateLibrary
from django.utils import six from django.utils import six
from django.utils.deprecation import RemovedInDjango110Warning from django.utils.deprecation import RemovedInDjango110Warning
@ -35,9 +35,9 @@ class DjangoTemplates(BaseEngine):
def from_string(self, template_code): def from_string(self, template_code):
return Template(self.engine.from_string(template_code), self) return Template(self.engine.from_string(template_code), self)
def get_template(self, template_name, dirs=_dirs_undefined): def get_template(self, template_name):
try: try:
return Template(self.engine.get_template(template_name, dirs), self) return Template(self.engine.get_template(template_name), self)
except TemplateDoesNotExist as exc: except TemplateDoesNotExist as exc:
reraise(exc, self) reraise(exc, self)

View File

@ -13,7 +13,6 @@ from .library import import_library
_context_instance_undefined = object() _context_instance_undefined = object()
_dictionary_undefined = object() _dictionary_undefined = object()
_dirs_undefined = object()
class Engine(object): class Engine(object):
@ -167,19 +166,12 @@ class Engine(object):
""" """
return Template(template_code, engine=self) return Template(template_code, engine=self)
def get_template(self, template_name, dirs=_dirs_undefined): def get_template(self, template_name):
""" """
Returns a compiled Template object for the given template name, Returns a compiled Template object for the given template name,
handling template inheritance recursively. handling template inheritance recursively.
""" """
if dirs is _dirs_undefined: template, origin = self.find_template(template_name)
dirs = None
else:
warnings.warn(
"The dirs argument of get_template is deprecated.",
RemovedInDjango110Warning, stacklevel=2)
template, origin = self.find_template(template_name, dirs)
if not hasattr(template, 'render'): if not hasattr(template, 'render'):
# template needs to be compiled # template needs to be compiled
template = Template(template, origin, template_name, engine=self) template = Template(template, origin, template_name, engine=self)
@ -193,7 +185,6 @@ class Engine(object):
def render_to_string(self, template_name, context=None, def render_to_string(self, template_name, context=None,
context_instance=_context_instance_undefined, context_instance=_context_instance_undefined,
dirs=_dirs_undefined,
dictionary=_dictionary_undefined): dictionary=_dictionary_undefined):
if context_instance is _context_instance_undefined: if context_instance is _context_instance_undefined:
context_instance = None context_instance = None
@ -201,14 +192,6 @@ class Engine(object):
warnings.warn( warnings.warn(
"The context_instance argument of render_to_string is " "The context_instance argument of render_to_string is "
"deprecated.", RemovedInDjango110Warning, stacklevel=2) "deprecated.", RemovedInDjango110Warning, stacklevel=2)
if dirs is _dirs_undefined:
# Do not set dirs to None here to avoid triggering the deprecation
# warning in select_template or get_template.
pass
else:
warnings.warn(
"The dirs argument of render_to_string is deprecated.",
RemovedInDjango110Warning, stacklevel=2)
if dictionary is _dictionary_undefined: if dictionary is _dictionary_undefined:
dictionary = None dictionary = None
else: else:
@ -218,9 +201,9 @@ class Engine(object):
context = dictionary context = dictionary
if isinstance(template_name, (list, tuple)): if isinstance(template_name, (list, tuple)):
t = self.select_template(template_name, dirs) t = self.select_template(template_name)
else: else:
t = self.get_template(template_name, dirs) t = self.get_template(template_name)
if not context_instance: if not context_instance:
# Django < 1.8 accepted a Context in `context` even though that's # Django < 1.8 accepted a Context in `context` even though that's
# unintended. Preserve this ability but don't rewrap `context`. # unintended. Preserve this ability but don't rewrap `context`.
@ -235,25 +218,16 @@ class Engine(object):
with context_instance.push(context): with context_instance.push(context):
return t.render(context_instance) return t.render(context_instance)
def select_template(self, template_name_list, dirs=_dirs_undefined): def select_template(self, template_name_list):
""" """
Given a list of template names, returns the first that can be loaded. Given a list of template names, returns the first that can be loaded.
""" """
if dirs is _dirs_undefined:
# Do not set dirs to None here to avoid triggering the deprecation
# warning in get_template.
pass
else:
warnings.warn(
"The dirs argument of select_template is deprecated.",
RemovedInDjango110Warning, stacklevel=2)
if not template_name_list: if not template_name_list:
raise TemplateDoesNotExist("No template names provided") raise TemplateDoesNotExist("No template names provided")
not_found = [] not_found = []
for template_name in template_name_list: for template_name in template_name_list:
try: try:
return self.get_template(template_name, dirs) return self.get_template(template_name)
except TemplateDoesNotExist as exc: except TemplateDoesNotExist as exc:
if exc.args[0] not in not_found: if exc.args[0] not in not_found:
not_found.append(exc.args[0]) not_found.append(exc.args[0])

View File

@ -4,14 +4,12 @@ from django.utils.deprecation import RemovedInDjango110Warning
from . import engines from . import engines
from .backends.django import DjangoTemplates from .backends.django import DjangoTemplates
from .engine import ( from .engine import _context_instance_undefined, _dictionary_undefined
_context_instance_undefined, _dictionary_undefined, _dirs_undefined,
)
from .exceptions import TemplateDoesNotExist from .exceptions import TemplateDoesNotExist
from .loaders import base from .loaders import base
def get_template(template_name, dirs=_dirs_undefined, using=None): def get_template(template_name, using=None):
""" """
Loads and returns a template for the given name. Loads and returns a template for the given name.
@ -21,24 +19,14 @@ def get_template(template_name, dirs=_dirs_undefined, using=None):
engines = _engine_list(using) engines = _engine_list(using)
for engine in engines: for engine in engines:
try: try:
# This is required for deprecating the dirs argument. Simply return engine.get_template(template_name)
# return engine.get_template(template_name) in Django 1.10.
if isinstance(engine, DjangoTemplates):
return engine.get_template(template_name, dirs)
elif dirs is not _dirs_undefined:
warnings.warn(
"Skipping template backend %s because its get_template "
"method doesn't support the dirs argument." % engine.name,
stacklevel=2)
else:
return engine.get_template(template_name)
except TemplateDoesNotExist as e: except TemplateDoesNotExist as e:
chain.append(e) chain.append(e)
raise TemplateDoesNotExist(template_name, chain=chain) raise TemplateDoesNotExist(template_name, chain=chain)
def select_template(template_name_list, dirs=_dirs_undefined, using=None): def select_template(template_name_list, using=None):
""" """
Loads and returns a template for one of the given names. Loads and returns a template for one of the given names.
@ -51,17 +39,7 @@ def select_template(template_name_list, dirs=_dirs_undefined, using=None):
for template_name in template_name_list: for template_name in template_name_list:
for engine in engines: for engine in engines:
try: try:
# This is required for deprecating the dirs argument. Simply return engine.get_template(template_name)
# use engine.get_template(template_name) in Django 1.10.
if isinstance(engine, DjangoTemplates):
return engine.get_template(template_name, dirs)
elif dirs is not _dirs_undefined:
warnings.warn(
"Skipping template backend %s because its get_template "
"method doesn't support the dirs argument." % engine.name,
stacklevel=2)
else:
return engine.get_template(template_name)
except TemplateDoesNotExist as e: except TemplateDoesNotExist as e:
chain.append(e) chain.append(e)
@ -73,7 +51,6 @@ def select_template(template_name_list, dirs=_dirs_undefined, using=None):
def render_to_string(template_name, context=None, def render_to_string(template_name, context=None,
context_instance=_context_instance_undefined, context_instance=_context_instance_undefined,
dirs=_dirs_undefined,
dictionary=_dictionary_undefined, dictionary=_dictionary_undefined,
request=None, using=None): request=None, using=None):
""" """
@ -82,7 +59,6 @@ def render_to_string(template_name, context=None,
template_name may be a string or a list of strings. template_name may be a string or a list of strings.
""" """
if (context_instance is _context_instance_undefined if (context_instance is _context_instance_undefined
and dirs is _dirs_undefined
and dictionary is _dictionary_undefined): and dictionary is _dictionary_undefined):
# No deprecated arguments were passed - use the new code path # No deprecated arguments were passed - use the new code path
if isinstance(template_name, (list, tuple)): if isinstance(template_name, (list, tuple)):
@ -106,17 +82,12 @@ def render_to_string(template_name, context=None,
"when some deprecated arguments are passed.") "when some deprecated arguments are passed.")
# Hack -- use the internal Engine instance of DjangoTemplates. # Hack -- use the internal Engine instance of DjangoTemplates.
return engine.engine.render_to_string( return engine.engine.render_to_string(
template_name, context, context_instance, dirs, dictionary) template_name, context, context_instance, dictionary)
elif context_instance is not _context_instance_undefined: elif context_instance is not _context_instance_undefined:
warnings.warn( warnings.warn(
"Skipping template backend %s because its render_to_string " "Skipping template backend %s because its render_to_string "
"method doesn't support the context_instance argument." % "method doesn't support the context_instance argument." %
engine.name, stacklevel=2) engine.name, stacklevel=2)
elif dirs is not _dirs_undefined:
warnings.warn(
"Skipping template backend %s because its render_to_string "
"method doesn't support the dirs argument." % engine.name,
stacklevel=2)
elif dictionary is not _dictionary_undefined: elif dictionary is not _dictionary_undefined:
warnings.warn( warnings.warn(
"Skipping template backend %s because its render_to_string " "Skipping template backend %s because its render_to_string "

View File

@ -15,7 +15,7 @@ introduce controlled coupling for convenience's sake.
``render`` ``render``
========== ==========
.. function:: render(request, template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, dirs=_dirs_undefined, using=None) .. function:: render(request, template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, using=None)
Combines a given template with a given context dictionary and returns an Combines a given template with a given context dictionary and returns an
:class:`~django.http.HttpResponse` object with that rendered text. :class:`~django.http.HttpResponse` object with that rendered text.
@ -75,10 +75,6 @@ Optional arguments
The ``using`` parameter was added. The ``using`` parameter was added.
.. deprecated:: 1.8
The ``dirs`` parameter was deprecated.
Example Example
------- -------
@ -107,7 +103,7 @@ This example is equivalent to::
``render_to_response`` ``render_to_response``
====================== ======================
.. function:: render_to_response(template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, dirs=_dirs_undefined, using=None) .. function:: render_to_response(template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, using=None)
Renders a given template with a given context dictionary and returns an Renders a given template with a given context dictionary and returns an
:class:`~django.http.HttpResponse` object with that rendered text. :class:`~django.http.HttpResponse` object with that rendered text.
@ -165,10 +161,6 @@ Optional arguments
The ``status`` and ``using`` parameters were added. The ``status`` and ``using`` parameters were added.
.. deprecated:: 1.8
The ``dirs`` parameter was deprecated.
Example Example
------- -------

View File

@ -95,7 +95,7 @@ Usage
The ``django.template.loader`` module defines two functions to load templates. The ``django.template.loader`` module defines two functions to load templates.
.. function:: get_template(template_name, dirs=_dirs_undefined, using=None) .. function:: get_template(template_name, using=None)
This function loads the template with the given name and returns a This function loads the template with the given name and returns a
``Template`` object. ``Template`` object.
@ -115,10 +115,6 @@ The ``django.template.loader`` module defines two functions to load templates.
If you want to restrict the search to a particular template engine, pass If you want to restrict the search to a particular template engine, pass
the engine's :setting:`NAME <TEMPLATES-NAME>` in the ``using`` argument. the engine's :setting:`NAME <TEMPLATES-NAME>` in the ``using`` argument.
.. deprecated:: 1.8
The ``dirs`` parameter was deprecated.
.. versionchanged:: 1.8 .. versionchanged:: 1.8
The ``using`` parameter was added. The ``using`` parameter was added.
@ -128,16 +124,12 @@ The ``django.template.loader`` module defines two functions to load templates.
``get_template()`` returns a backend-dependent ``Template`` instead ``get_template()`` returns a backend-dependent ``Template`` instead
of a :class:`django.template.Template`. of a :class:`django.template.Template`.
.. function:: select_template(template_name_list, dirs=_dirs_undefined, using=None) .. function:: select_template(template_name_list, using=None)
``select_template()`` is just like ``get_template()``, except it takes a ``select_template()`` is just like ``get_template()``, except it takes a
list of template names. It tries each name in order and returns the first list of template names. It tries each name in order and returns the first
template that exists. template that exists.
.. deprecated:: 1.8
The ``dirs`` parameter was deprecated.
.. versionchanged:: 1.8 .. versionchanged:: 1.8
The ``using`` parameter was added. The ``using`` parameter was added.

View File

@ -1 +0,0 @@
spam eggs

View File

@ -32,13 +32,6 @@ class ShortcutTests(SimpleTestCase):
self.assertEqual(response.content, b'FOO.BAR..\n') self.assertEqual(response.content, b'FOO.BAR..\n')
self.assertEqual(response['Content-Type'], 'application/x-rendertest') self.assertEqual(response['Content-Type'], 'application/x-rendertest')
@ignore_warnings(category=RemovedInDjango110Warning)
def test_render_to_response_with_dirs(self):
response = self.client.get('/render_to_response/dirs/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'spam eggs\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_render_to_response_with_status(self): def test_render_to_response_with_status(self):
response = self.client.get('/render_to_response/status/') response = self.client.get('/render_to_response/status/')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
@ -101,10 +94,3 @@ class ShortcutTests(SimpleTestCase):
self.assertEqual(response.content, b'DTL\n') self.assertEqual(response.content, b'DTL\n')
response = self.client.get('/render/using/?using=jinja2') response = self.client.get('/render/using/?using=jinja2')
self.assertEqual(response.content, b'Jinja2\n') self.assertEqual(response.content, b'Jinja2\n')
@ignore_warnings(category=RemovedInDjango110Warning)
def test_render_with_dirs(self):
response = self.client.get('/render/dirs/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'spam eggs\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')

View File

@ -7,7 +7,6 @@ urlpatterns = [
url(r'^render_to_response/multiple_templates/$', views.render_to_response_view_with_multiple_templates), url(r'^render_to_response/multiple_templates/$', views.render_to_response_view_with_multiple_templates),
url(r'^render_to_response/request_context/$', views.render_to_response_view_with_request_context), url(r'^render_to_response/request_context/$', views.render_to_response_view_with_request_context),
url(r'^render_to_response/content_type/$', views.render_to_response_view_with_content_type), url(r'^render_to_response/content_type/$', views.render_to_response_view_with_content_type),
url(r'^render_to_response/dirs/$', views.render_to_response_view_with_dirs),
url(r'^render_to_response/status/$', views.render_to_response_view_with_status), url(r'^render_to_response/status/$', views.render_to_response_view_with_status),
url(r'^render_to_response/using/$', views.render_to_response_view_with_using), url(r'^render_to_response/using/$', views.render_to_response_view_with_using),
url(r'^render_to_response/context_instance_misuse/$', views.render_to_response_with_context_instance_misuse), url(r'^render_to_response/context_instance_misuse/$', views.render_to_response_with_context_instance_misuse),
@ -15,7 +14,6 @@ urlpatterns = [
url(r'^render/multiple_templates/$', views.render_view_with_multiple_templates), url(r'^render/multiple_templates/$', views.render_view_with_multiple_templates),
url(r'^render/base_context/$', views.render_view_with_base_context), url(r'^render/base_context/$', views.render_view_with_base_context),
url(r'^render/content_type/$', views.render_view_with_content_type), url(r'^render/content_type/$', views.render_view_with_content_type),
url(r'^render/dirs/$', views.render_with_dirs),
url(r'^render/status/$', views.render_view_with_status), url(r'^render/status/$', views.render_view_with_status),
url(r'^render/using/$', views.render_view_with_using), url(r'^render/using/$', views.render_view_with_using),
] ]

View File

@ -1,10 +1,5 @@
import os.path
from django.shortcuts import render, render_to_response from django.shortcuts import render, render_to_response
from django.template import Context, RequestContext from django.template import Context, RequestContext
from django.utils._os import upath
dirs = (os.path.join(os.path.dirname(upath(__file__)), 'other_templates'),)
def render_to_response_view(request): def render_to_response_view(request):
@ -38,10 +33,6 @@ def render_to_response_view_with_content_type(request):
}, content_type='application/x-rendertest') }, content_type='application/x-rendertest')
def render_to_response_view_with_dirs(request):
return render_to_response('render_dirs_test.html', dirs=dirs)
def render_to_response_view_with_status(request): def render_to_response_view_with_status(request):
return render_to_response('shortcuts/render_test.html', { return render_to_response('shortcuts/render_test.html', {
'foo': 'FOO', 'foo': 'FOO',
@ -95,10 +86,6 @@ def render_view_with_content_type(request):
}, content_type='application/x-rendertest') }, content_type='application/x-rendertest')
def render_with_dirs(request):
return render(request, 'render_dirs_test.html', dirs=dirs)
def render_view_with_status(request): def render_view_with_status(request):
return render(request, 'shortcuts/render_test.html', { return render(request, 'shortcuts/render_test.html', {
'foo': 'FOO', 'foo': 'FOO',

View File

@ -89,28 +89,3 @@ class LoaderTests(SimpleTestCase):
template = engine.get_template('priority/foo.html') template = engine.get_template('priority/foo.html')
self.assertEqual(template.render(Context()), 'priority\n') self.assertEqual(template.render(Context()), 'priority\n')
@ignore_warnings(category=RemovedInDjango110Warning)
class TemplateDirsOverrideTests(SimpleTestCase):
DIRS = ((OTHER_DIR, ), [OTHER_DIR])
def setUp(self):
self.engine = Engine()
def test_render_to_string(self):
for dirs in self.DIRS:
self.assertEqual(
self.engine.render_to_string('test_dirs.html', dirs=dirs),
'spam eggs\n',
)
def test_get_template(self):
for dirs in self.DIRS:
template = self.engine.get_template('test_dirs.html', dirs=dirs)
self.assertEqual(template.render(Context()), 'spam eggs\n')
def test_select_template(self):
for dirs in self.DIRS:
template = self.engine.select_template(['test_dirs.html'], dirs=dirs)
self.assertEqual(template.render(Context()), 'spam eggs\n')