Skip to content

Commit

Permalink
fix and simplify calculation of cache_key in the cached template loader
Browse files Browse the repository at this point in the history
based on bernardopires/django-tenant-schemas@5ad9342

- use calculations of parent class as base
- avoid throwing an exception if the tenant is a FakeTenant
  • Loading branch information
Bernhard Miller committed Apr 19, 2021
1 parent b0b9928 commit cfb420c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 27 deletions.
29 changes: 6 additions & 23 deletions django_tenants/template/loaders/cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,15 @@

from django.template.loaders.cached import Loader as BaseLoader

from django_tenants.postgresql_backend.base import FakeTenant


class Loader(BaseLoader):

def cache_key(self, template_name, skip=None):
"""
Generate a cache key for the template name, dirs, and skip.
If skip is provided, only origins that match template_name are included
in the cache key. This ensures each template is only parsed and cached
once if contained in different extend chains like:
x -> a -> a
y -> a -> a
z -> a -> a
"""
dirs_prefix = ''
skip_prefix = ''
tenant_prefix = ''

if skip:
matching = [origin.name for origin in skip if origin.template_name == template_name]

if matching:
skip_prefix = self.generate_hash(matching)
key = super().cache_key(template_name, skip)

if connection.tenant:
tenant_prefix = str(connection.tenant.pk)
if not connection.tenant or isinstance(connection.tenant, FakeTenant):
return key

return '-'.join(s for s in (str(template_name), tenant_prefix, skip_prefix, dirs_prefix) if s)
return "-".join([connection.tenant.schema_name, key])
4 changes: 2 additions & 2 deletions django_tenants/tests/template/loaders/test_cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUpClass(cls):
def test_cache_key(self):
loader = self.engine.template_loaders[0]
self.assertEqual(
loader.cache_key("index.html"), "index.html-{}".format(self.tenant.pk)
loader.cache_key("index.html"), "{}-index.html".format(self.tenant.schema_name)
)

def test_get_template(self):
Expand All @@ -42,7 +42,7 @@ def test_get_template(self):
)

cache = self.engine.template_loaders[0].get_template_cache
self.assertEqual(cache["index.html-{}".format(self.tenant.pk)], template)
self.assertEqual(cache["{}-index.html".format(self.tenant.schema_name)], template)

# Run a second time from cache
template = self.engine.get_template("index.html")
Expand Down
4 changes: 2 additions & 2 deletions django_tenants/tests/template/loaders/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def setUpClass(cls):
def test_cache_key(self):
loader = self.engine.template_loaders[0]
self.assertEqual(
loader.cache_key("index.html"), "index.html-{}".format(self.tenant.pk)
loader.cache_key("index.html"), "{}-index.html".format(self.tenant.schema_name)
)

def test_get_template(self):
Expand All @@ -132,7 +132,7 @@ def test_get_template(self):
)

cache = self.engine.template_loaders[0].get_template_cache
self.assertEqual(cache["index.html-{}".format(self.tenant.pk)], template)
self.assertEqual(cache["{}-index.html".format(self.tenant.schema_name)], template)

# Run a second time from cache
template = self.engine.get_template("index.html")
Expand Down

0 comments on commit cfb420c

Please sign in to comment.