Ticket #9666: 9666-with-tests.diff

File 9666-with-tests.diff, 5.6 KB (added by Eric Holscher, 16 years ago)

Actually attach template this time.

  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index 835b1a9..91bc169 100644
    a b  
    22
    33import sys
    44import re
     5import os
    56from itertools import cycle as itertools_cycle
    67try:
    78    reversed
    class SsiNode(Node):  
    287288        self.filepath, self.parsed = filepath, parsed
    288289
    289290    def render(self, context):
    290         if not include_is_allowed(self.filepath):
     291        filepath = self.filepath
     292        try:
     293            filepath = filepath.resolve(context)
     294        except (AttributeError, VariableDoesNotExist):
     295            pass
     296        if not include_is_allowed(filepath):
    291297            if settings.DEBUG:
    292298                return "[Didn't have permission to include file]"
    293299            else:
    294300                return '' # Fail silently for invalid includes.
    295301        try:
    296             fp = open(self.filepath, 'r')
     302            fp = open(filepath, 'r')
    297303            output = fp.read()
    298304            fp.close()
    299305        except IOError:
    300306            output = ''
    301307        if self.parsed:
    302308            try:
    303                 t = Template(output, name=self.filepath)
     309                t = Template(output, name=filepath)
    304310                return t.render(context)
    305311            except TemplateSyntaxError, e:
    306312                if settings.DEBUG:
    class URLNode(Node):  
    377383                except NoReverseMatch:
    378384                    if self.asvar is None:
    379385                        # Re-raise the original exception, not the one with
    380                         # the path relative to the project. This makes a 
     386                        # the path relative to the project. This makes a
    381387                        # better error message.
    382388                        raise e
    383389            else:
    def ssi(parser, token):  
    897903        else:
    898904            raise TemplateSyntaxError("Second (optional) argument to %s tag"
    899905                                      " must be 'parsed'" % bits[0])
    900     return SsiNode(bits[1], parsed)
     906    var = bits[1]
     907    #For backwards compatibility, let things that look like a path be quoted.
     908    if os.path.sep in var and var[0] != "'" and var[0] != '"':
     909        var = '"' + var + '"'
     910    var = parser.compile_filter(var)
     911    return SsiNode(var, parsed)
    901912ssi = register.tag(ssi)
    902913
    903914#@register.tag
  • new file tests/regressiontests/templates/ssi_include.html

    diff --git a/tests/regressiontests/templates/ssi_include.html b/tests/regressiontests/templates/ssi_include.html
    new file mode 100755
    index 0000000..58d5926
    - +  
     1This is for testing an ssi include. {{ test }}
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 953e8fa..6767778 100644
    a b class SomeException(Exception):  
    6969
    7070class SomeOtherException(Exception):
    7171    pass
    72    
     72
    7373class ContextStackException(Exception):
    7474    pass
    7575
    class Templates(unittest.TestCase):  
    211211        old_invalid = settings.TEMPLATE_STRING_IF_INVALID
    212212        expected_invalid_str = 'INVALID'
    213213
     214        #Set ALLOWED_INCLUDE_ROOTS so that ssi works.
     215        old_allowed, settings.ALLOWED_INCLUDE_ROOTS = settings.ALLOWED_INCLUDE_ROOTS, os.path.dirname(os.path.abspath(__file__))
     216
    214217        for name, vals in tests:
    215218            if isinstance(vals[2], tuple):
    216219                normal_string_result = vals[2][0]
    class Templates(unittest.TestCase):  
    257260        deactivate()
    258261        settings.TEMPLATE_DEBUG = old_td
    259262        settings.TEMPLATE_STRING_IF_INVALID = old_invalid
     263        settings.ALLOWED_INCLUDE_ROOTS = old_allowed
    260264
    261265        self.assertEqual(failures, [], "Tests failed:\n%s\n%s" %
    262266            ('-'*70, ("\n%s\n" % ('-'*70)).join(failures)))
    class Templates(unittest.TestCase):  
    915919                          '{% endfor %}',
    916920                          {}, ''),
    917921
     922            ### SSI TAG ########################################################
     923            #Test normal behavior
     924            'ssi01': ('{%% ssi %s %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ssi_include.html'), {}, 'This is for testing an ssi include. {{ test }}\n'),
     925            'ssi02': ('{%% ssi %s %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'not_here'), {}, ''),
     926            #Test passing as a variable
     927            'ssi03': ('{% ssi ssi_file %}', {'ssi_file': os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ssi_include.html')}, 'This is for testing an ssi include. {{ test }}\n'),
     928            'ssi04': ('{% ssi ssi_file %}', {'ssi_file': 'no_file'}, ''),
     929            #Test parsed output
     930            'ssi05': ('{%% ssi %s parsed %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ssi_include.html'), {'test': 'Look ma! It parsed!'}, 'This is for testing an ssi include. Look ma! It parsed!\n'),
     931            'ssi06': ('{%% ssi %s parsed %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'not_here'), {'test': 'Look ma! It parsed!'}, ''),
     932            #Test quoting
     933            'ssi07': ('{%% ssi "%s" %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ssi_include.html'), {}, 'This is for testing an ssi include. {{ test }}\n'),
     934            'ssi08': ('{%% ssi "%s" %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'not_here'), {}, ''),
     935
    918936            ### TEMPLATETAG TAG #######################################################
    919937            'templatetag01': ('{% templatetag openblock %}', {}, '{%'),
    920938            'templatetag02': ('{% templatetag closeblock %}', {}, '%}'),
Back to Top