Ticket #582: template_eggs-4.patch

File template_eggs-4.patch, 3.1 KB (added by sune.kirkeby@…, 9 years ago)

Python 2.3 safe patch

  • django/core/template_eggs.py

     
     1"""Wrapper for loading templates from eggs via pkg_resources.resource_string."""
     2
     3try:
     4    from pkg_resources import resource_string
     5except ImportError:
     6    # python2.3 compatibility hack
     7    def resource_string(*args):
     8        raise AssertionError, 'pkg_resource required for template_eggs'
     9
     10from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION
     11from django.core.template import TemplateDoesNotExist
     12
     13def load_template_source(name, dirs=None):
     14    """I load templates from Python eggs via pkg_resource.resource_string.
     15
     16    For every install app I try to get the resource (app, name)."""
     17
     18    pkg_name = 'templates/' + name + TEMPLATE_FILE_EXTENSION
     19
     20    for app in INSTALLED_APPS:
     21        try:
     22            return resource_string(app, pkg_name)
     23        except:
     24            pass
     25
     26    raise TemplateDoesNotExist, name
  • django/core/template_loader.py

     
    11"Wrapper for loading templates from storage of some sort (e.g. files or db)"
    22import template
    3 from template_file import load_template_source
    43
     4from django.conf.settings import TEMPLATE_SOURCE_LOADERS
     5from django.core import exceptions
     6
     7template_source_loaders = []
     8for path in TEMPLATE_SOURCE_LOADERS:
     9    i = path.rfind('.')
     10    module, attr = path[:i], path[i+1:]
     11    try:
     12        mod = __import__(module, globals(), locals(), [attr])
     13    except ImportError, e:
     14        raise exceptions.ImproperlyConfigured, 'Error importing template_source_loader %s: "%s"' % (module, e)
     15    try:
     16        template_source_loaders.append(getattr(mod, attr))
     17    except AttributeError:
     18        raise exceptions.ImproperlyConfigured, 'Module "%s" does not define a "%s" callable template_source_loader' % (module, attr)
     19
     20def load_template_source(name, dirs=None):
     21    for loader in template_source_loaders:
     22        try:
     23            return loader(name, dirs)
     24        except template.TemplateDoesNotExist:
     25            pass
     26    raise template.TemplateDoesNotExist, name
     27
    528class ExtendsError(Exception):
    629    pass
    730
  • django/conf/global_settings.py

     
    6060# Extension on all templates.
    6161TEMPLATE_FILE_EXTENSION = '.html'
    6262
     63# Callables which know how to import template sources from various
     64# sources; the expected interface is callable(name, [dirs]), where dirs
     65# is a list of directories to search instead of TEMPLATE_DIRS.
     66TEMPLATE_SOURCE_LOADERS = (
     67    'django.core.template_file.load_template_source',
     68    'django.core.template_eggs.load_template_source',
     69)
     70
    6371# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
    6472# trailing slash.
    6573# Examples: "http://foo.com/media/", "/media/".
Back to Top