Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CachedLoader when template_dirs is None #498

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions tenant_schemas/template_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ def __init__(self, engine, loaders):

@staticmethod
def cache_key(template_name, template_dirs):
if connection.tenant and template_dirs:
if connection.tenant and not isinstance(connection.tenant, FakeTenant):
if not template_dirs:
try:
template_dirs = settings.MULTITENANT_TEMPLATE_DIRS
except AttributeError:
raise ImproperlyConfigured('To use %s.%s you must define the MULTITENANT_TEMPLATE_DIRS' %
(__name__, CachedLoader.__name__))
return '-'.join([str(connection.tenant.pk), template_name,
hashlib.sha1(force_bytes('|'.join(template_dirs))).hexdigest()])
if template_dirs:
Expand Down Expand Up @@ -77,7 +83,7 @@ def load_template(self, template_name, template_dirs=None):
template_tuple = self.template_cache.get(key)
# A cached previous failure:
if template_tuple is TemplateDoesNotExist:
raise TemplateDoesNotExist
raise TemplateDoesNotExist(template_name)
elif template_tuple is None:
template, origin = self.find_template(template_name, template_dirs)
if not hasattr(template, 'render'):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Tenant! (Django templates)
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os

from django.template.loader import get_template
from django.test import SimpleTestCase, override_settings
from django.test import TransactionTestCase, override_settings
from django.db import connection

from tenant_schemas.tests.models import Tenant


@override_settings(
Expand All @@ -23,9 +26,19 @@
]
},
}
],
MULTITENANT_TEMPLATE_DIRS = [
os.path.join(os.path.dirname(__file__), "templates")
]
)
class CachedLoaderTests(SimpleTestCase):
class CachedLoaderTests(TransactionTestCase):
def test_get_template(self):
template = get_template("hello.html")
self.assertEqual(template.render(), "Hello! (Django templates)\n")

def test_get_tenant_template(self):
tenant = Tenant(domain_url="tenant", schema_name="tenant")
tenant.save()
connection.set_tenant(tenant)
template = get_template("hello.html")
self.assertEqual(template.render(), "Hello Tenant! (Django templates)\n")