Code

Ticket #6587: new_template_lib_loader_5.diff

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

More use of import, tests pass

Line 
1diff --git a/django/template/__init__.py b/django/template/__init__.py
2index 29daa76..3141ce2 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@@ -910,22 +911,37 @@ 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 get_library(library_name, extra_modules=[]):
20+    lib = libraries.get(library_name, None)
21     if not lib:
22-        try:
23-            mod = __import__(module_name, {}, {}, [''])
24-        except ImportError, e:
25-            raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
26-        try:
27-            lib = mod.register
28-            libraries[module_name] = lib
29-        except AttributeError:
30-            raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
31+        templatetags_modules = get_templatetags_modules()
32+        search_modules = extra_modules + templatetags_modules
33+        for m in search_modules:
34+            app_label = m.split('.')[-1]
35+            "Allow both library_name and app_label.library_name"
36+            try:
37+                app, library = library_name.split('.')
38+            except:
39+                app, library = ('', library_name)
40+            if not app or app == app_label:
41+                try:
42+                    module_name = '%s.%s' % (m, library)
43+                    mod = __import__(module_name, {}, {}, [''])
44+                except ImportError:
45+                    continue
46+            else:
47+                continue
48+            try:
49+                lib = mod.register
50+                libraries[library_name] = lib
51+            except AttributeError:
52+                raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
53+        if not lib:
54+            raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, str(search_modules)))
55     return lib
56 
57-def add_to_builtins(module_name):
58-    builtins.append(get_library(module_name))
59+def add_to_builtins(module_name, library_name):
60+    builtins.append(get_library(library_name, extra_modules=[module_name]))
61 
62-add_to_builtins('django.template.defaulttags')
63-add_to_builtins('django.template.defaultfilters')
64+add_to_builtins('django.template', 'defaulttags')
65+add_to_builtins('django.template', 'defaultfilters')
66diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
67index e5a8e66..cd671b9 100644
68--- a/django/template/defaulttags.py
69+++ b/django/template/defaulttags.py
70@@ -850,7 +850,7 @@ def load(parser, token):
71     for taglib in bits[1:]:
72         # add the library to the parser
73         try:
74-            lib = get_library("django.templatetags.%s" % taglib)
75+            lib = get_library(taglib)
76             parser.add_library(lib)
77         except InvalidTemplateLibrary, e:
78             raise TemplateSyntaxError("'%s' is not a valid tag library: %s" %
79diff --git a/django/template/loader.py b/django/template/loader.py
80index 03e6f8d..9cb893c 100644
81--- a/django/template/loader.py
82+++ b/django/template/loader.py
83@@ -115,4 +115,4 @@ def select_template(template_name_list):
84     # If we get here, none of the templates could be loaded
85     raise TemplateDoesNotExist, ', '.join(template_name_list)
86 
87-add_to_builtins('django.template.loader_tags')
88+add_to_builtins('django.template', 'loader_tags')
89diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
90index 9204535..6e4db0e 100644
91--- a/django/templatetags/__init__.py
92+++ b/django/templatetags/__init__.py
93@@ -1,7 +1,15 @@
94 from django.conf import settings
95+import os
96 
97-for a in settings.INSTALLED_APPS:
98-    try:
99-        __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__)
100-    except ImportError:
101-        pass
102+templatetags_modules = []
103+
104+def get_templatetags_modules():
105+    if not templatetags_modules:
106+        for a in ['django'] + list(settings.INSTALLED_APPS):
107+            try:
108+                name = a + '.templatetags'
109+                mod = __import__(name, {}, {}, [''])
110+                templatetags_modules.append(name)
111+            except ImportError:
112+                pass
113+    return templatetags_modules
114diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
115index aef6f50..0490f2a 100644
116--- a/tests/regressiontests/templates/tests.py
117+++ b/tests/regressiontests/templates/tests.py
118@@ -45,7 +45,7 @@ def do_echo(parser, token):
119 
120 register.tag("echo", do_echo)
121 
122-template.libraries['django.templatetags.testtags'] = register
123+template.libraries['testtags'] = register
124 
125 #####################################
126 # Helper objects for template tests #