[1.9.x] Fixed #25466 -- Added backwards compatibility aliases for LoaderOrigin and StringOrigin.

Thanks Simon Charette for the DeprecationInstanceCheck class.

Backport of 8d1a001ef6 from master
This commit is contained in:
Tim Graham 2015-09-26 13:38:04 -04:00
parent 60fe6efe50
commit 59027a4cae
7 changed files with 58 additions and 5 deletions

View File

@ -58,7 +58,8 @@ from .exceptions import TemplateDoesNotExist, TemplateSyntaxError # NOQA i
# Template parts
from .base import ( # NOQA isort:skip
Context, Node, NodeList, Origin, RequestContext, Template, Variable,
Context, Node, NodeList, Origin, RequestContext, StringOrigin, Template,
Variable,
)
# Deprecated in Django 1.8, will be removed in Django 1.10.

View File

@ -60,7 +60,10 @@ from django.template.context import ( # NOQA: imported for backwards compatibil
BaseContext, Context, ContextPopException, RequestContext,
)
from django.utils import six
from django.utils.deprecation import RemovedInDjango110Warning
from django.utils.deprecation import (
DeprecationInstanceCheck, RemovedInDjango20Warning,
RemovedInDjango110Warning,
)
from django.utils.encoding import (
force_str, force_text, python_2_unicode_compatible,
)
@ -158,6 +161,11 @@ class Origin(object):
)
class StringOrigin(six.with_metaclass(DeprecationInstanceCheck, Origin)):
alternative = 'django.template.Origin'
deprecation_warning = RemovedInDjango20Warning
class Template(object):
def __init__(self, template_string, origin=None, name=None, engine=None):
try:

View File

@ -1,9 +1,14 @@
import warnings
from django.utils.deprecation import RemovedInDjango110Warning
from django.utils import six
from django.utils.deprecation import (
DeprecationInstanceCheck, RemovedInDjango20Warning,
RemovedInDjango110Warning,
)
from . import engines
from .backends.django import DjangoTemplates
from .base import Origin
from .engine import (
_context_instance_undefined, _dictionary_undefined, _dirs_undefined,
)
@ -147,3 +152,8 @@ class BaseLoader(base.Loader):
"django.template.loaders.base.Loader.",
RemovedInDjango110Warning, stacklevel=2)
super(BaseLoader, self).__init__(*args, **kwargs)
class LoaderOrigin(six.with_metaclass(DeprecationInstanceCheck, Origin)):
alternative = 'django.template.Origin'
deprecation_warning = RemovedInDjango20Warning

View File

@ -73,3 +73,12 @@ class RenameMethodsBase(type):
setattr(base, old_method_name, wrapper(new_method))
return new_class
class DeprecationInstanceCheck(type):
def __instancecheck__(self, instance):
warnings.warn(
"`%s` is deprecated, use `%s` instead." % (self.__name__, self.alternative),
self.deprecation_warning, 2
)
return super(DeprecationInstanceCheck, self).__instancecheck__(instance)

View File

@ -100,6 +100,10 @@ details on these changes.
* The ``enclosure`` keyword argument to ``SyndicationFeed.add_item()`` will be
removed.
* The ``django.template.loader.LoaderOrigin`` and
``django.template.base.StringOrigin`` aliases for
``django.template.base.Origin`` will be removed.
.. _deprecation-removed-in-1.10:
1.10

View File

@ -818,7 +818,9 @@ debug as ``True``, an instance of ``django.template.loader.LoaderOrigin`` or
``django.template.base.StringOrigin`` was set as the origin attribute on the
template object. These classes have been combined into
:class:`~django.template.base.Origin` and is now always set regardless of the
engine debug setting.
engine debug setting. For a minimal level of backwards compatibility, the old
class names will be kept as aliases to the new ``Origin`` class until
Django 2.0.
.. _default-logging-changes-19:
@ -1335,6 +1337,10 @@ Miscellaneous
deprecated. Use the new ``enclosures`` argument which accepts a list of
``Enclosure`` objects instead of a single one.
* The ``django.template.loader.LoaderOrigin`` and
``django.template.base.StringOrigin`` aliases for
``django.template.base.Origin`` are deprecated.
.. _removed-features-1.9:
Features removed in 1.9

View File

@ -7,7 +7,9 @@ import warnings
from django.test import SimpleTestCase
from django.test.utils import reset_warning_registry
from django.utils import six
from django.utils.deprecation import RenameMethodsBase
from django.utils.deprecation import (
DeprecationInstanceCheck, RemovedInNextVersionWarning, RenameMethodsBase,
)
from django.utils.encoding import force_text
@ -197,3 +199,16 @@ class DeprecatingSimpleTestCaseUrls(unittest.TestCase):
"SimpleTestCase.urls is deprecated and will be removed in "
"Django 1.10. Use @override_settings(ROOT_URLCONF=...) "
"in TempTestCase instead.")
class DeprecationInstanceCheckTest(SimpleTestCase):
def test_warning(self):
class Manager(six.with_metaclass(DeprecationInstanceCheck)):
alternative = 'fake.path.Foo'
deprecation_warning = RemovedInNextVersionWarning
msg = '`Manager` is deprecated, use `fake.path.Foo` instead.'
with warnings.catch_warnings():
warnings.simplefilter('error', category=RemovedInNextVersionWarning)
with self.assertRaisesMessage(RemovedInNextVersionWarning, msg):
isinstance(object, Manager)