5 |
5 |
|
6 |
6 |
import hashlib
|
7 |
7 |
|
8 |
|
import django
|
9 |
8 |
from django.conf import settings
|
10 |
9 |
from django.core.exceptions import ImproperlyConfigured
|
11 |
10 |
from django.template import engines
|
12 |
11 |
from django.template import TemplateDoesNotExist
|
|
12 |
from django.template import Origin
|
13 |
13 |
|
14 |
14 |
from django.template.loaders.cached import Loader as DjangoCachedLoader
|
15 |
15 |
from django.template.loaders.filesystem import Loader as DjangoFilesystemLoader
|
16 |
16 |
|
17 |
|
try:
|
18 |
|
from django.template import Origin
|
19 |
|
except ImportError:
|
20 |
|
Origin = None # django < 1.9
|
21 |
|
|
22 |
17 |
from django.utils.encoding import force_bytes
|
23 |
18 |
from django.utils._os import safe_join
|
24 |
19 |
from django.db import connection
|
... | ... | |
27 |
22 |
|
28 |
23 |
class CachedLoader(DjangoCachedLoader):
|
29 |
24 |
def cache_key(self, template_name, template_dirs, skip=None):
|
30 |
|
if django.VERSION <= (1, 10, 0):
|
31 |
|
key = super(CachedLoader, self).cache_key(template_name, template_dirs)
|
32 |
|
else:
|
33 |
|
key = super(CachedLoader, self).cache_key(template_name, template_dirs, skip=skip)
|
|
25 |
key = super(CachedLoader, self).cache_key(template_name, template_dirs, skip=skip)
|
34 |
26 |
if connection.tenant:
|
35 |
27 |
return connection.tenant.domain_url + '-' + key
|
36 |
28 |
return key
|
... | ... | |
51 |
43 |
except AttributeError:
|
52 |
44 |
raise ImproperlyConfigured('To use %s.%s you must define the TENANT_TEMPLATE_DIRS' % (__name__, FilesystemLoader.__name__))
|
53 |
45 |
|
54 |
|
# Django >= 1.10 will normalize template names to support relative path; this
|
55 |
|
# breaks publik-base-theme usage of "../../<orig>" in variants/ as the template
|
56 |
|
# name will then be known as "variants/<orig>" and won't be found. Cancel this
|
57 |
|
# by removing the variants/ prefix.
|
58 |
|
#
|
59 |
|
# This can be removed once django < 1.9 support is dropped and publik-base-theme
|
60 |
|
# migrated to benefit from recursive template loaders. (removing the ../../ hack
|
61 |
|
# from its extends tags).
|
62 |
|
if template_name.startswith('variants/'):
|
63 |
|
template_name = template_name[9:]
|
64 |
|
|
65 |
46 |
known_dirnames = ['templates', 'theme/templates']
|
66 |
|
known_template_names = [template_name]
|
67 |
|
if hasattr(settings, 'TEMPLATE_VARS') and settings.TEMPLATE_VARS.get('is_portal_agent'):
|
68 |
|
known_template_names = ['portal-agent/%s' % template_name, template_name]
|
69 |
|
elif hasattr(settings, 'TEMPLATE_VARS') and settings.TEMPLATE_VARS.get('theme'):
|
70 |
|
theme_value = settings.TEMPLATE_VARS['theme']
|
71 |
|
known_template_names = ['variants/%s/%s' % (theme_value, template_name), template_name]
|
|
47 |
if hasattr(settings, 'TEMPLATE_VARS'):
|
|
48 |
if settings.TEMPLATE_VARS.get('theme'):
|
|
49 |
theme_value = settings.TEMPLATE_VARS['theme']
|
|
50 |
known_dirnames = ['%s/variants/%s' % (x, theme_value) for x in known_dirnames] + known_dirnames
|
|
51 |
if settings.TEMPLATE_VARS.get('is_portal_agent'):
|
|
52 |
known_dirnames = ['%s/portal-agent' % x for x in known_dirnames] + known_dirnames
|
72 |
53 |
|
73 |
54 |
for template_dir in template_dirs:
|
74 |
55 |
for dirname in known_dirnames:
|
75 |
|
for template_name in known_template_names:
|
76 |
|
try:
|
77 |
|
template_path = safe_join(template_dir, connection.tenant.domain_url, dirname,
|
78 |
|
template_name)
|
79 |
|
except UnicodeDecodeError:
|
80 |
|
# The template dir name was a bytestring that wasn't valid UTF-8.
|
81 |
|
raise
|
82 |
|
except ValueError:
|
83 |
|
# The joined path was located outside of this particular
|
84 |
|
# template_dir (it might be inside another one, so this isn't
|
85 |
|
# fatal).
|
86 |
|
pass
|
|
56 |
try:
|
|
57 |
template_path = safe_join(template_dir, connection.tenant.domain_url, dirname,
|
|
58 |
template_name)
|
|
59 |
except UnicodeDecodeError:
|
|
60 |
# The template dir name was a bytestring that wasn't valid UTF-8.
|
|
61 |
raise
|
|
62 |
except ValueError:
|
|
63 |
# The joined path was located outside of this particular
|
|
64 |
# template_dir (it might be inside another one, so this isn't
|
|
65 |
# fatal).
|
|
66 |
pass
|
87 |
67 |
|
88 |
|
if Origin is not None:
|
89 |
|
yield Origin(template_path,
|
90 |
|
template_name=template_name,
|
91 |
|
loader=self,)
|
92 |
|
else:
|
93 |
|
yield template_path
|
|
68 |
yield Origin(template_path,
|
|
69 |
template_name=template_name,
|
|
70 |
loader=self,)
|
94 |
|
-
|