Code

Ticket #6587: new_template_lib_loader_9.diff

File new_template_lib_loader_9.diff, 4.5 KB (added by oyvind, 6 years ago)

removed os import

Line 
1diff --git a/django/template/__init__.py b/django/template/__init__.py
2index 5c4ab30..1d61387 100644
3--- a/django/template/__init__.py
4+++ b/django/template/__init__.py
5@@ -59,6 +59,7 @@ from django.utils.encoding import smart_unicode, force_unicode
6 from django.utils.translation import ugettext as _
7 from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping
8 from django.utils.html import escape
9+from django.templatetags import get_templatetags_modules
10 
11 __all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
12 
13@@ -913,22 +914,43 @@ class Library(object):
14             return func
15         return dec
16 
17-def get_library(module_name):
18-    lib = libraries.get(module_name, None)
19+def import_library(module_name):
20+    try:
21+        mod = __import__(module_name, {}, {}, [''])
22+    except ImportError:
23+        return None
24+    try:
25+        return mod.register
26+    except AttributeError:
27+        raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
28+
29+def get_library(library_name):
30+    lib = libraries.get(library_name, None)
31     if not lib:
32-        try:
33-            mod = __import__(module_name, {}, {}, [''])
34-        except ImportError, e:
35-            raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
36-        try:
37-            lib = mod.register
38-            libraries[module_name] = lib
39-        except AttributeError:
40-            raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
41+
42+        """
43+        If library is not already loaded loop over all templatetags modules to locate it.
44+
45+        {% load somelib %} and {% load someotherlib %} loops twice.
46+
47+        Subsequent loads eg. {% load somelib %} in the same thread will grab the cached
48+        module from libraries.
49+        """
50+        templatetags_modules = get_templatetags_modules()
51+        tried_modules = []
52+        for module in templatetags_modules:
53+            taglib_module = '%s.%s' % (module, library_name)
54+            tried_modules.append(taglib_module)
55+            lib = import_library(taglib_module)
56+            if lib:
57+                libraries[library_name] = lib
58+                break
59+        if not lib:
60+            raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, ','.join(tried_modules)))
61     return lib
62 
63 def add_to_builtins(module_name):
64-    builtins.append(get_library(module_name))
65+    builtins.append(import_library(module_name))
66 
67 add_to_builtins('django.template.defaulttags')
68 add_to_builtins('django.template.defaultfilters')
69diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
70index cf3b35b..665c30a 100644
71--- a/django/template/defaulttags.py
72+++ b/django/template/defaulttags.py
73@@ -850,7 +850,7 @@ def load(parser, token):
74     for taglib in bits[1:]:
75         # add the library to the parser
76         try:
77-            lib = get_library("django.templatetags.%s" % taglib)
78+            lib = get_library(taglib)
79             parser.add_library(lib)
80         except InvalidTemplateLibrary, e:
81             raise TemplateSyntaxError("'%s' is not a valid tag library: %s" %
82diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
83index 9204535..93a97b4 100644
84--- a/django/templatetags/__init__.py
85+++ b/django/templatetags/__init__.py
86@@ -1,7 +1,15 @@
87 from django.conf import settings
88 
89-for a in settings.INSTALLED_APPS:
90-    try:
91-        __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__)
92-    except ImportError:
93-        pass
94+templatetags_modules= []
95+
96+def get_templatetags_modules():
97+    if not templatetags_modules:
98+        """ Populate list once per thread. """
99+        for app_module in ['django'] + list(settings.INSTALLED_APPS):
100+            try:
101+                name = '%s.templatetags' % app_module
102+                mod = __import__(name, {}, {}, [''])
103+                templatetags_modules.append(name)
104+            except ImportError:
105+                pass
106+    return templatetags_modules
107diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
108index 4effea5..dc61a8a 100644
109--- a/tests/regressiontests/templates/tests.py
110+++ b/tests/regressiontests/templates/tests.py
111@@ -48,7 +48,7 @@ def do_echo(parser, token):
112 
113 register.tag("echo", do_echo)
114 
115-template.libraries['django.templatetags.testtags'] = register
116+template.libraries['testtags'] = register
117 
118 #####################################
119 # Helper objects for template tests #