~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/template/loaders/filesystem.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
from django.conf import settings
6
6
from django.template import TemplateDoesNotExist
 
7
from django.template.loader import BaseLoader
7
8
from django.utils._os import safe_join
8
9
 
9
 
def get_template_sources(template_name, template_dirs=None):
10
 
    """
11
 
    Returns the absolute paths to "template_name", when appended to each
12
 
    directory in "template_dirs". Any paths that don't lie inside one of the
13
 
    template dirs are excluded from the result set, for security reasons.
14
 
    """
15
 
    if not template_dirs:
16
 
        template_dirs = settings.TEMPLATE_DIRS
17
 
    for template_dir in template_dirs:
18
 
        try:
19
 
            yield safe_join(template_dir, template_name)
20
 
        except UnicodeDecodeError:
21
 
            # The template dir name was a bytestring that wasn't valid UTF-8.
22
 
            raise
23
 
        except ValueError:
24
 
            # The joined path was located outside of this particular
25
 
            # template_dir (it might be inside another one, so this isn't
26
 
            # fatal).
27
 
            pass
 
10
class Loader(BaseLoader):
 
11
    is_usable = True
 
12
 
 
13
    def get_template_sources(self, template_name, template_dirs=None):
 
14
        """
 
15
        Returns the absolute paths to "template_name", when appended to each
 
16
        directory in "template_dirs". Any paths that don't lie inside one of the
 
17
        template dirs are excluded from the result set, for security reasons.
 
18
        """
 
19
        if not template_dirs:
 
20
            template_dirs = settings.TEMPLATE_DIRS
 
21
        for template_dir in template_dirs:
 
22
            try:
 
23
                yield safe_join(template_dir, template_name)
 
24
            except UnicodeDecodeError:
 
25
                # The template dir name was a bytestring that wasn't valid UTF-8.
 
26
                raise
 
27
            except ValueError:
 
28
                # The joined path was located outside of this particular
 
29
                # template_dir (it might be inside another one, so this isn't
 
30
                # fatal).
 
31
                pass
 
32
 
 
33
    def load_template_source(self, template_name, template_dirs=None):
 
34
        tried = []
 
35
        for filepath in self.get_template_sources(template_name, template_dirs):
 
36
            try:
 
37
                file = open(filepath)
 
38
                try:
 
39
                    return (file.read().decode(settings.FILE_CHARSET), filepath)
 
40
                finally:
 
41
                    file.close()
 
42
            except IOError:
 
43
                tried.append(filepath)
 
44
        if tried:
 
45
            error_msg = "Tried %s" % tried
 
46
        else:
 
47
            error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
 
48
        raise TemplateDoesNotExist(error_msg)
 
49
    load_template_source.is_usable = True
 
50
 
 
51
_loader = Loader()
28
52
 
29
53
def load_template_source(template_name, template_dirs=None):
30
 
    tried = []
31
 
    for filepath in get_template_sources(template_name, template_dirs):
32
 
        try:
33
 
            return (open(filepath).read().decode(settings.FILE_CHARSET), filepath)
34
 
        except IOError:
35
 
            tried.append(filepath)
36
 
    if tried:
37
 
        error_msg = "Tried %s" % tried
38
 
    else:
39
 
        error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
40
 
    raise TemplateDoesNotExist, error_msg
 
54
    # For backwards compatibility
 
55
    import warnings
 
56
    warnings.warn(
 
57
        "'django.template.loaders.filesystem.load_template_source' is deprecated; use 'django.template.loaders.filesystem.Loader' instead.",
 
58
        PendingDeprecationWarning
 
59
    )
 
60
    return _loader.load_template_source(template_name, template_dirs)
41
61
load_template_source.is_usable = True