Ticket #15721: 15721.2.diff

File 15721.2.diff, 4.0 KB (added by Chris Beaven, 14 years ago)
  • django/template/context.py

    diff --git a/django/template/context.py b/django/template/context.py
    index bcfaa3b..acc5758 100644
    a b class ContextPopException(Exception):  
    1414    "pop() has been called more times than push()"
    1515    pass
    1616
    17 class EmptyClass(object):
    18     # No-op class which takes no args to its __init__ method, to help implement
    19     # __copy__
    20     pass
    21 
    2217class BaseContext(object):
    2318    def __init__(self, dict_=None):
    24         dict_ = dict_ or {}
    25         self.dicts = [dict_]
     19        self._reset_dicts(dict_)
     20
     21    def _reset_dicts(self, value=None):
     22        self.dicts = [value or {}]
    2623
    2724    def __copy__(self):
    28         duplicate = EmptyClass()
    29         duplicate.__class__ = self.__class__
    30         duplicate.__dict__ = self.__dict__.copy()
    31         duplicate.dicts = duplicate.dicts[:]
     25        duplicate = copy(super(BaseContext, self))
     26        duplicate.dicts = self.dicts[:]
    3227        return duplicate
    3328
    3429    def __repr__(self):
    class BaseContext(object):  
    7873                return d[key]
    7974        return otherwise
    8075
     76    def new(self, values=None):
     77        """
     78        Returns a new context with the same properties, but with only the
     79        values given in 'values' stored.
     80        """
     81        new_context = copy(self)
     82        new_context._reset_dicts(values)
     83        return new_context
     84
    8185class Context(BaseContext):
    8286    "A stack container for variable context"
    8387    def __init__(self, dict_=None, autoescape=True, current_app=None, use_l10n=None):
    class Context(BaseContext):  
    8892        super(Context, self).__init__(dict_)
    8993
    9094    def __copy__(self):
    91         duplicate = super(Context, self).__copy__()
     95        duplicate = copy(super(Context, self))
    9296        duplicate.render_context = copy(self.render_context)
    9397        return duplicate
    9498
    class Context(BaseContext):  
    99103        self.dicts.append(other_dict)
    100104        return other_dict
    101105
    102     def new(self, values=None):
    103         """
    104         Returns a new Context with the same 'autoescape' value etc, but with
    105         only the values given in 'values' stored.
    106         """
    107         return self.__class__(dict_=values, autoescape=self.autoescape,
    108                               current_app=self.current_app, use_l10n=self.use_l10n)
    109 
    110106class RenderContext(BaseContext):
    111107    """
    112108    A stack container for storing Template state.
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 10c7a37..c326604 100644
    a b class TemplateTagLoading(unittest.TestCase):  
    16401640        settings.INSTALLED_APPS = ('tagsegg',)
    16411641        t = template.Template(ttext)
    16421642
     1643class RequestContextTests(BaseTemplateResponseTest):
     1644
     1645    def test_include_only(self):
     1646        # Regression test for #15721
     1647        templates = {
     1648            'child': Template('{{ var|default:"none" }}'),
     1649        }
     1650
     1651        # Register a custom template loader.
     1652        def test_template_loader(template_name, template_dirs=None):
     1653            "A custom template loader that loads the unit-test templates."
     1654            try:
     1655                return (templates[template_name] , "test:%s" % template_name)
     1656            except KeyError:
     1657                raise template.TemplateDoesNotExist(template_name)
     1658
     1659        old_template_loaders = loader.template_source_loaders
     1660        loader.template_source_loaders = [test_template_loader]
     1661        try:
     1662            ctx = RequestContext(self.factory.get('/'), {'var': 'parent'})
     1663            self.assertEqual(
     1664                template.Template('{% include "child" %}').render(ctx),
     1665                'parent'
     1666            )
     1667            self.assertEqual(
     1668                template.Template('{% include "child" only %}').render(ctx),
     1669                'none'
     1670            )
     1671        finally:
     1672            loader.template_source_loaders = old_template_loaders
     1673
    16431674if __name__ == "__main__":
    16441675    unittest.main()
Back to Top