Code

Ticket #500: optional_template_file_extensions.2.diff

File optional_template_file_extensions.2.diff, 5.3 KB (added by SmileyChris, 8 years ago)

Updated patch, now it won't break admin if TEMPLATE_FILE_EXTENSION is changed

Line 
1Index: django/conf/global_settings.py
2===================================================================
3--- django/conf/global_settings.py      (revision 2524)
4+++ django/conf/global_settings.py      (working copy)
5@@ -91,9 +91,12 @@
6 # List of locations of the template source files, in search order.
7 TEMPLATE_DIRS = ()
8 
9-# Extension on all templates.
10+# Default extension for templates if none is given.
11 TEMPLATE_FILE_EXTENSION = '.html'
12 
13+# Default extension for admin templates.
14+ADMIN_TEMPLATE_FILE_EXTENSION = '.html'
15+
16 # List of callables that know how to import templates from various sources.
17 # See the comments in django/core/template/loader.py for interface
18 # documentation.
19Index: django/core/template/loaders/app_directories.py
20===================================================================
21--- django/core/template/loaders/app_directories.py     (revision 2524)
22+++ django/core/template/loaders/app_directories.py     (working copy)
23@@ -1,6 +1,6 @@
24 # Wrapper for loading templates from "template" directories in installed app packages.
25 
26-from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION
27+from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION, ADMIN_TEMPLATE_FILE_EXTENSION
28 from django.core.exceptions import ImproperlyConfigured
29 from django.core.template import TemplateDoesNotExist
30 import os
31@@ -28,8 +28,16 @@
32 app_template_dirs = tuple(app_template_dirs)
33 
34 def get_template_sources(template_name, template_dirs=None):
35+    admin_template_name = None
36+    if not os.path.splitext(template_name):
37+        # Only append file extension if there isn't an extension already.
38+        if TEMPLATE_FILE_EXTENSION != ADMIN_TEMPLATE_FILE_EXTENSION:
39+            admin_template_name = template_name + TEMPLATE_FILE_EXTENSION
40+        template_name = template_name + TEMPLATE_FILE_EXTENSION
41     for template_dir in app_template_dirs:
42-        yield os.path.join(template_dir, template_name) + TEMPLATE_FILE_EXTENSION
43+        yield os.path.join(template_dir, template_name)
44+        if admin_template_name:
45+            yield os.path.join(template_dir, admin_template_name)
46 
47 def load_template_source(template_name, template_dirs=None):
48     for filepath in get_template_sources(template_name, template_dirs):
49Index: django/core/template/loaders/eggs.py
50===================================================================
51--- django/core/template/loaders/eggs.py        (revision 2524)
52+++ django/core/template/loaders/eggs.py        (working copy)
53@@ -6,7 +6,8 @@
54     resource_string = None
55 
56 from django.core.template import TemplateDoesNotExist
57-from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION
58+from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION, ADMIN_TEMPLATE_FILE_EXTENSION
59+import os
60 
61 def load_template_source(template_name, template_dirs=None):
62     """
63@@ -15,11 +16,20 @@
64     For every installed app, it tries to get the resource (app, template_name).
65     """
66     if resource_string is not None:
67-        pkg_name = 'templates/' + template_name + TEMPLATE_FILE_EXTENSION
68+        admin_pkg_name = None
69+        if not os.path.splitext(template_name):
70+            # Only append TEMPLATE_FILE_EXTENSION if there isn't an extension already.
71+            if TEMPLATE_FILE_EXTENSION != ADMIN_TEMPLATE_FILE_EXTENSION:
72+                admin_pkg_name = 'templates/' + template_name + TEMPLATE_FILE_EXTENSION
73+            template_name = template_name + TEMPLATE_FILE_EXTENSION
74+        pkg_name = 'templates/' + template_name
75         for app in INSTALLED_APPS:
76             try:
77                 return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name))
78             except:
79                 pass
80+            if admin_pkg_name:
81+                try:
82+                    return (resource_string(app, admin_pkg_name), 'egg:%s:%s ' % (app, admin_pkg_name))
83     raise TemplateDoesNotExist, template_name
84 load_template_source.is_usable = resource_string is not None
85Index: django/core/template/loaders/filesystem.py
86===================================================================
87--- django/core/template/loaders/filesystem.py  (revision 2524)
88+++ django/core/template/loaders/filesystem.py  (working copy)
89@@ -1,14 +1,21 @@
90 # Wrapper for loading templates from the filesystem.
91 
92-from django.conf.settings import TEMPLATE_DIRS, TEMPLATE_FILE_EXTENSION
93+from django.conf.settings import TEMPLATE_DIRS, TEMPLATE_FILE_EXTENSION, ADMIN_TEMPLATE_FILE_EXTENSION
94 from django.core.template import TemplateDoesNotExist
95 import os
96 
97 def get_template_sources(template_name, template_dirs=None):
98     if not template_dirs:
99         template_dirs = TEMPLATE_DIRS
100+    if not os.path.splitext(template_name):
101+        # Only append TEMPLATE_FILE_EXTENSION if there isn't an extension already.
102+        if TEMPLATE_FILE_EXTENSION != ADMIN_TEMPLATE_FILE_EXTENSION:
103+            admin_template_name = template_name + TEMPLATE_FILE_EXTENSION
104+        template_name = template_name + TEMPLATE_FILE_EXTENSION
105     for template_dir in template_dirs:
106-        yield os.path.join(template_dir, template_name) + TEMPLATE_FILE_EXTENSION
107+        yield os.path.join(template_dir, template_name)
108+        if admin_template_name:
109+            yield os.path.join(template_dir, admin_template_name)
110 
111 def load_template_source(template_name, template_dirs=None):
112     tried = []