Fixes #15721 -- Make {% include %} and RequestContext work together again.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16031 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Chris Beaven 2011-04-17 04:52:31 +00:00
parent 89e25403ae
commit d59baa07f0
2 changed files with 45 additions and 20 deletions

View File

@ -14,21 +14,16 @@ class ContextPopException(Exception):
"pop() has been called more times than push()" "pop() has been called more times than push()"
pass pass
class EmptyClass(object):
# No-op class which takes no args to its __init__ method, to help implement
# __copy__
pass
class BaseContext(object): class BaseContext(object):
def __init__(self, dict_=None): def __init__(self, dict_=None):
dict_ = dict_ or {} self._reset_dicts(dict_)
self.dicts = [dict_]
def _reset_dicts(self, value=None):
self.dicts = [value or {}]
def __copy__(self): def __copy__(self):
duplicate = EmptyClass() duplicate = copy(super(BaseContext, self))
duplicate.__class__ = self.__class__ duplicate.dicts = self.dicts[:]
duplicate.__dict__ = self.__dict__.copy()
duplicate.dicts = duplicate.dicts[:]
return duplicate return duplicate
def __repr__(self): def __repr__(self):
@ -78,6 +73,15 @@ class BaseContext(object):
return d[key] return d[key]
return otherwise return otherwise
def new(self, values=None):
"""
Returns a new context with the same properties, but with only the
values given in 'values' stored.
"""
new_context = copy(self)
new_context._reset_dicts(values)
return new_context
class Context(BaseContext): class Context(BaseContext):
"A stack container for variable context" "A stack container for variable context"
def __init__(self, dict_=None, autoescape=True, current_app=None, use_l10n=None): def __init__(self, dict_=None, autoescape=True, current_app=None, use_l10n=None):
@ -88,7 +92,7 @@ class Context(BaseContext):
super(Context, self).__init__(dict_) super(Context, self).__init__(dict_)
def __copy__(self): def __copy__(self):
duplicate = super(Context, self).__copy__() duplicate = copy(super(Context, self))
duplicate.render_context = copy(self.render_context) duplicate.render_context = copy(self.render_context)
return duplicate return duplicate
@ -99,14 +103,6 @@ class Context(BaseContext):
self.dicts.append(other_dict) self.dicts.append(other_dict)
return other_dict return other_dict
def new(self, values=None):
"""
Returns a new Context with the same 'autoescape' value etc, but with
only the values given in 'values' stored.
"""
return self.__class__(dict_=values, autoescape=self.autoescape,
current_app=self.current_app, use_l10n=self.use_l10n)
class RenderContext(BaseContext): class RenderContext(BaseContext):
""" """
A stack container for storing Template state. A stack container for storing Template state.

View File

@ -1639,5 +1639,34 @@ class TemplateTagLoading(unittest.TestCase):
settings.INSTALLED_APPS = ('tagsegg',) settings.INSTALLED_APPS = ('tagsegg',)
t = template.Template(ttext) t = template.Template(ttext)
class RequestContextTests(BaseTemplateResponseTest):
def setUp(self):
templates = {
'child': Template('{{ var|default:"none" }}'),
}
setup_test_template_loader(templates)
self.fake_request = RequestFactory().get('/')
def tearDown(self):
restore_template_loaders()
def test_include_only(self):
"""
Regression test for #15721, ``{% include %}`` and ``RequestContext``
not playing together nicely.
"""
ctx = RequestContext(self.fake_request, {'var': 'parent'})
self.assertEqual(
template.Template('{% include "child" %}').render(ctx),
'parent'
)
self.assertEqual(
template.Template('{% include "child" only %}').render(ctx),
'none'
)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()