From 885bb0f4bf31df0d4f30926826a7766dfdf9c759 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 19 May 2019 14:15:38 +0200
Subject: [PATCH 2/2] multitenant: drop 1.8 compatibility from template loader
 (#33238)

---
 hobo/multitenant/template_loader.py | 67 ++++++++++-------------------
 1 file changed, 22 insertions(+), 45 deletions(-)

diff --git a/hobo/multitenant/template_loader.py b/hobo/multitenant/template_loader.py
index e3c6eb4..f81e533 100644
--- a/hobo/multitenant/template_loader.py
+++ b/hobo/multitenant/template_loader.py
@@ -5,20 +5,15 @@ to load templates from them in order, caching the result.
 
 import hashlib
 
-import django
 from django.conf import settings
 from django.core.exceptions import ImproperlyConfigured
 from django.template import engines
 from django.template import TemplateDoesNotExist
+from django.template import Origin
 
 from django.template.loaders.cached import Loader as DjangoCachedLoader
 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._os import safe_join
 from django.db import connection
@@ -27,10 +22,7 @@ from tenant_schemas.utils import get_public_schema_name
 
 class CachedLoader(DjangoCachedLoader):
     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)
-        else:
-            key = super(CachedLoader, self).cache_key(template_name, template_dirs, skip=skip)
+        key = super(CachedLoader, self).cache_key(template_name, template_dirs, skip=skip)
         if connection.tenant:
             return connection.tenant.domain_url + '-' + key
         return key
@@ -51,43 +43,28 @@ class FilesystemLoader(DjangoFilesystemLoader):
             except AttributeError:
                 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_template_names = [template_name]
-        if hasattr(settings, 'TEMPLATE_VARS') and settings.TEMPLATE_VARS.get('is_portal_agent'):
-            known_template_names = ['portal-agent/%s' % template_name, template_name]
-        elif hasattr(settings, 'TEMPLATE_VARS') and settings.TEMPLATE_VARS.get('theme'):
-            theme_value = settings.TEMPLATE_VARS['theme']
-            known_template_names = ['variants/%s/%s' % (theme_value, template_name), template_name]
+        if hasattr(settings, 'TEMPLATE_VARS'):
+            if settings.TEMPLATE_VARS.get('theme'):
+                theme_value = settings.TEMPLATE_VARS['theme']
+                known_dirnames = ['%s/variants/%s' % (x, theme_value) for x in known_dirnames] + known_dirnames
+            if settings.TEMPLATE_VARS.get('is_portal_agent'):
+                known_dirnames = ['%s/portal-agent' % x for x in known_dirnames] + known_dirnames
 
         for template_dir in template_dirs:
             for dirname in known_dirnames:
-                for template_name in known_template_names:
-                    try:
-                        template_path = safe_join(template_dir, connection.tenant.domain_url, dirname,
-                                template_name)
-                    except UnicodeDecodeError:
-                        # The template dir name was a bytestring that wasn't valid UTF-8.
-                        raise
-                    except ValueError:
-                        # The joined path was located outside of this particular
-                        # template_dir (it might be inside another one, so this isn't
-                        # fatal).
-                        pass
+                try:
+                    template_path = safe_join(template_dir, connection.tenant.domain_url, dirname,
+                            template_name)
+                except UnicodeDecodeError:
+                    # The template dir name was a bytestring that wasn't valid UTF-8.
+                    raise
+                except ValueError:
+                    # The joined path was located outside of this particular
+                    # template_dir (it might be inside another one, so this isn't
+                    # fatal).
+                    pass
 
-                    if Origin is not None:
-                        yield Origin(template_path,
-                                template_name=template_name,
-                                loader=self,)
-                    else:
-                        yield template_path
+                yield Origin(template_path,
+                        template_name=template_name,
+                        loader=self,)
-- 
2.20.1

