-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.py
80 lines (66 loc) · 2.69 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding:utf-8 -*-
"""
@author: wTayyeb https://github.com/wtayyeb
@license: MIT
"""
import io
from django.conf import settings
from django.template import TemplateDoesNotExist
from django.templatetags.static import static
from django.utils._os import safe_join
from .models import thememanager
from .threadlocals import get_thread_variable
try:
from django.core.exceptions import SuspiciousFileOperation
except ImportError:
from django.core.exceptions import SuspiciousOperation as SuspiciousFileOperation
try:
from django.template.loaders.base import Loader as BaseLoader
except ImportError:
from django.template.loader import BaseLoader
class Loader(BaseLoader):
is_usable = True
def get_template_sources(self, template_name, template_dirs=None):
"""
Returns the absolute paths to "template_name", when appended to each
directory in "template_dirs". Any paths that don't lie inside one of the
template dirs are excluded from the result set, for security reasons.
"""
theme = thememanager.get_current_theme()
if not template_dirs:
template_dirs = [safe_join(settings.THEMING_ROOT, theme.slug), ]
for template_dir in template_dirs:
try:
yield safe_join(template_dir, template_name)
except SuspiciousFileOperation:
# The joined path was located outside of this template_dir
# (it might be inside another one, so this isn't fatal).
pass
def load_template_source(self, template_name, template_dirs=None):
tried = []
for filepath in self.get_template_sources(template_name, template_dirs):
try:
with io.open(filepath, encoding=self.engine.file_charset) as fp:
return fp.read(), filepath
except IOError:
tried.append(filepath)
if tried:
error_msg = "Tried %s" % tried
else:
error_msg = ("Your template directories configuration is empty. "
"Change it to point to at least one template directory.")
raise TemplateDoesNotExist(error_msg)
load_template_source.is_usable = True
def context_processor(request):
""" theming template context processor """
theme = thememanager.get_current_theme()
theme_url = settings.THEMING_URL
theme_url += '/' if theme_url[-1] != '/' else ''
theme_url = static(theme_url + theme.slug).replace('\\', '/')
sitetheme = get_thread_variable('sitetheme')
return {
'theme_url': theme_url,
'sitetheme': theme,
'site_title': sitetheme.site_title,
'site_description': sitetheme.site_description,
}