Ticket #6587: new_template_lib_loader_5.diff

File new_template_lib_loader_5.diff, 5.1 KB (added by oyvind, 16 years ago)

More use of import, tests pass

  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index 29daa76..3141ce2 100644
    a b from django.utils.encoding import smart_unicode, force_unicode  
    5959from django.utils.translation import ugettext as _
    6060from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping
    6161from django.utils.html import escape
     62from django.templatetags import get_templatetags_modules
    6263
    6364__all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
    6465
    class Library(object):  
    910911            return func
    911912        return dec
    912913
    913 def get_library(module_name):
    914     lib = libraries.get(module_name, None)
     914def get_library(library_name, extra_modules=[]):
     915    lib = libraries.get(library_name, None)
    915916    if not lib:
    916         try:
    917             mod = __import__(module_name, {}, {}, [''])
    918         except ImportError, e:
    919             raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
    920         try:
    921             lib = mod.register
    922             libraries[module_name] = lib
    923         except AttributeError:
    924             raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
     917        templatetags_modules = get_templatetags_modules()
     918        search_modules = extra_modules + templatetags_modules
     919        for m in search_modules:
     920            app_label = m.split('.')[-1]
     921            "Allow both library_name and app_label.library_name"
     922            try:
     923                app, library = library_name.split('.')
     924            except:
     925                app, library = ('', library_name)
     926            if not app or app == app_label:
     927                try:
     928                    module_name = '%s.%s' % (m, library)
     929                    mod = __import__(module_name, {}, {}, [''])
     930                except ImportError:
     931                    continue
     932            else:
     933                continue
     934            try:
     935                lib = mod.register
     936                libraries[library_name] = lib
     937            except AttributeError:
     938                raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
     939        if not lib:
     940            raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, str(search_modules)))
    925941    return lib
    926942
    927 def add_to_builtins(module_name):
    928     builtins.append(get_library(module_name))
     943def add_to_builtins(module_name, library_name):
     944    builtins.append(get_library(library_name, extra_modules=[module_name]))
    929945
    930 add_to_builtins('django.template.defaulttags')
    931 add_to_builtins('django.template.defaultfilters')
     946add_to_builtins('django.template', 'defaulttags')
     947add_to_builtins('django.template', 'defaultfilters')
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index e5a8e66..cd671b9 100644
    a b def load(parser, token):  
    850850    for taglib in bits[1:]:
    851851        # add the library to the parser
    852852        try:
    853             lib = get_library("django.templatetags.%s" % taglib)
     853            lib = get_library(taglib)
    854854            parser.add_library(lib)
    855855        except InvalidTemplateLibrary, e:
    856856            raise TemplateSyntaxError("'%s' is not a valid tag library: %s" %
  • django/template/loader.py

    diff --git a/django/template/loader.py b/django/template/loader.py
    index 03e6f8d..9cb893c 100644
    a b def select_template(template_name_list):  
    115115    # If we get here, none of the templates could be loaded
    116116    raise TemplateDoesNotExist, ', '.join(template_name_list)
    117117
    118 add_to_builtins('django.template.loader_tags')
     118add_to_builtins('django.template', 'loader_tags')
  • django/templatetags/__init__.py

    diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
    index 9204535..6e4db0e 100644
    a b  
    11from django.conf import settings
     2import os
    23
    3 for a in settings.INSTALLED_APPS:
    4     try:
    5         __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__)
    6     except ImportError:
    7         pass
     4templatetags_modules = []
     5
     6def get_templatetags_modules():
     7    if not templatetags_modules:
     8        for a in ['django'] + list(settings.INSTALLED_APPS):
     9            try:
     10                name = a + '.templatetags'
     11                mod = __import__(name, {}, {}, [''])
     12                templatetags_modules.append(name)
     13            except ImportError:
     14                pass
     15    return templatetags_modules
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index aef6f50..0490f2a 100644
    a b def do_echo(parser, token):  
    4545
    4646register.tag("echo", do_echo)
    4747
    48 template.libraries['django.templatetags.testtags'] = register
     48template.libraries['testtags'] = register
    4949
    5050#####################################
    5151# Helper objects for template tests #
Back to Top