multitenant: drop 1.8 compatibility from template loader (#33238)

This commit is contained in:
Frédéric Péters 2019-05-19 14:15:38 +02:00
parent f0d10f92a9
commit a8670a2197
1 changed files with 22 additions and 45 deletions

View File

@ -5,20 +5,15 @@ to load templates from them in order, caching the result.
import hashlib import hashlib
import django
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.template import engines from django.template import engines
from django.template import TemplateDoesNotExist from django.template import TemplateDoesNotExist
from django.template import Origin
from django.template.loaders.cached import Loader as DjangoCachedLoader from django.template.loaders.cached import Loader as DjangoCachedLoader
from django.template.loaders.filesystem import Loader as DjangoFilesystemLoader from django.template.loaders.filesystem import Loader as DjangoFilesystemLoader
try:
from django.template import Origin
except ImportError:
Origin = None # django < 1.9
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
from django.utils._os import safe_join from django.utils._os import safe_join
from django.db import connection from django.db import connection
@ -27,10 +22,7 @@ from tenant_schemas.utils import get_public_schema_name
class CachedLoader(DjangoCachedLoader): class CachedLoader(DjangoCachedLoader):
def cache_key(self, template_name, template_dirs, skip=None): def cache_key(self, template_name, template_dirs, skip=None):
if django.VERSION <= (1, 10, 0): key = super(CachedLoader, self).cache_key(template_name, template_dirs, skip=skip)
key = super(CachedLoader, self).cache_key(template_name, template_dirs)
else:
key = super(CachedLoader, self).cache_key(template_name, template_dirs, skip=skip)
if connection.tenant: if connection.tenant:
return connection.tenant.domain_url + '-' + key return connection.tenant.domain_url + '-' + key
return key return key
@ -51,43 +43,28 @@ class FilesystemLoader(DjangoFilesystemLoader):
except AttributeError: except AttributeError:
raise ImproperlyConfigured('To use %s.%s you must define the TENANT_TEMPLATE_DIRS' % (__name__, FilesystemLoader.__name__)) raise ImproperlyConfigured('To use %s.%s you must define the TENANT_TEMPLATE_DIRS' % (__name__, FilesystemLoader.__name__))
# Django >= 1.10 will normalize template names to support relative path; this
# breaks publik-base-theme usage of "../../<orig>" in variants/ as the template
# name will then be known as "variants/<orig>" and won't be found. Cancel this
# by removing the variants/ prefix.
#
# This can be removed once django < 1.9 support is dropped and publik-base-theme
# migrated to benefit from recursive template loaders. (removing the ../../ hack
# from its extends tags).
if template_name.startswith('variants/'):
template_name = template_name[9:]
known_dirnames = ['templates', 'theme/templates'] known_dirnames = ['templates', 'theme/templates']
known_template_names = [template_name] if hasattr(settings, 'TEMPLATE_VARS'):
if hasattr(settings, 'TEMPLATE_VARS') and settings.TEMPLATE_VARS.get('is_portal_agent'): if settings.TEMPLATE_VARS.get('theme'):
known_template_names = ['portal-agent/%s' % template_name, template_name] theme_value = settings.TEMPLATE_VARS['theme']
elif hasattr(settings, 'TEMPLATE_VARS') and settings.TEMPLATE_VARS.get('theme'): known_dirnames = ['%s/variants/%s' % (x, theme_value) for x in known_dirnames] + known_dirnames
theme_value = settings.TEMPLATE_VARS['theme'] if settings.TEMPLATE_VARS.get('is_portal_agent'):
known_template_names = ['variants/%s/%s' % (theme_value, template_name), template_name] known_dirnames = ['%s/portal-agent' % x for x in known_dirnames] + known_dirnames
for template_dir in template_dirs: for template_dir in template_dirs:
for dirname in known_dirnames: for dirname in known_dirnames:
for template_name in known_template_names: try:
try: template_path = safe_join(template_dir, connection.tenant.domain_url, dirname,
template_path = safe_join(template_dir, connection.tenant.domain_url, dirname, template_name)
template_name) except UnicodeDecodeError:
except UnicodeDecodeError: # The template dir name was a bytestring that wasn't valid UTF-8.
# The template dir name was a bytestring that wasn't valid UTF-8. raise
raise except ValueError:
except ValueError: # The joined path was located outside of this particular
# The joined path was located outside of this particular # template_dir (it might be inside another one, so this isn't
# template_dir (it might be inside another one, so this isn't # fatal).
# fatal). pass
pass
if Origin is not None: yield Origin(template_path,
yield Origin(template_path, template_name=template_name,
template_name=template_name, loader=self,)
loader=self,)
else:
yield template_path