Ticket #4952: 4952.3.diff

File 4952.3.diff, 4.1 KB (added by Gary Wilson, 17 years ago)

another try

  • django/template/loaders/util.py

    === added file 'django/template/loaders/util.py'
     
     1"""
     2Utility functions for template loaders.
     3"""
     4
     5from os.path import abspath, join
     6
     7def get_template_sources(template_name, template_dirs=None):
     8    """
     9    Return a generater that will return the full paths of possible template
     10    locations given the template_name and a list of template_dirs.
     11    """
     12    if not template_dirs:
     13        raise StopIteration
     14    for template_dir in template_dirs:
     15        full_template_dir = abspath(join(template_dir, template_name))
     16        # Security check to ensure that we are still in the template directory
     17        # after the os.path.join (see ticket #4952).
     18        if full_template_dir.startswith(template_dir):
     19            yield full_template_dir
  • tests/regressiontests/template_loaders/tests.py

    === added directory 'tests/regressiontests/template_loaders'
    === added file 'tests/regressiontests/template_loaders/__init__.py'
    === added file 'tests/regressiontests/template_loaders/models.py'
    === added file 'tests/regressiontests/template_loaders/tests.py'
     
     1"""
     2>>> template_dirs = ['/dir1', '/dir2']
     3
     4>>> list(get_template_sources('index.html', template_dirs))
     5['/dir1/index.html', '/dir2/index.html']
     6
     7>>> list(get_template_sources('/etc/passwd', template_dirs))
     8[]
     9
     10>>> list(get_template_sources('etc/passwd', template_dirs))
     11['/dir1/etc/passwd', '/dir2/etc/passwd']
     12
     13>>> list(get_template_sources('../etc/passwd', template_dirs))
     14[]
     15
     16>>> list(get_template_sources('../../../etc/passwd', template_dirs))
     17[]
     18"""
     19
     20from django.template.loaders.util import get_template_sources
     21
     22if __name__ == "__main__":
     23    import doctest
     24    doctest.testmod()
  • django/template/loaders/app_directories.py

    === modified file 'django/template/loaders/app_directories.py'
     
    1 # Wrapper for loading templates from "template" directories in installed app packages.
     1"""
     2Wrapper for loading templates from "template" directories in INSTALLED_APPS
     3packages.
     4"""
     5
     6import os
    27
    38from django.conf import settings
    49from django.core.exceptions import ImproperlyConfigured
    510from django.template import TemplateDoesNotExist
    6 import os
     11from django.template.loaders.util import get_template_sources
    712
    813# At compile time, cache the directories to search.
    914app_template_dirs = []
     
    2732# It won't change, so convert it to a tuple to save memory.
    2833app_template_dirs = tuple(app_template_dirs)
    2934
    30 def get_template_sources(template_name, template_dirs=None):
    31     for template_dir in app_template_dirs:
    32         yield os.path.join(template_dir, template_name)
    33 
    3435def load_template_source(template_name, template_dirs=None):
     36    if not template_dirs:
     37        template_dirs = app_template_dirs
    3538    for filepath in get_template_sources(template_name, template_dirs):
    3639        try:
    3740            return (open(filepath).read().decode(settings.FILE_CHARSET), filepath)
  • django/template/loaders/filesystem.py

    === modified file 'django/template/loaders/filesystem.py'
     
    1 # Wrapper for loading templates from the filesystem.
     1"""
     2Wrapper for loading templates from the filesystem.
     3"""
    24
    35from django.conf import settings
    46from django.template import TemplateDoesNotExist
    5 import os
     7from django.template.loaders.util import get_template_sources
    68
    7 def get_template_sources(template_name, template_dirs=None):
     9def load_template_source(template_name, template_dirs=None):
    810    if not template_dirs:
    911        template_dirs = settings.TEMPLATE_DIRS
    10     for template_dir in template_dirs:
    11         yield os.path.join(template_dir, template_name)
    12 
    13 def load_template_source(template_name, template_dirs=None):
    1412    tried = []
    1513    for filepath in get_template_sources(template_name, template_dirs):
    1614        try:
Back to Top