From bb4e7ce36fd2f32105cc957c884447a67ddfc73a Mon Sep 17 00:00:00 2001 From: Selwin Ong Date: Sun, 6 May 2012 20:52:17 +0700 Subject: [PATCH] Fixed an issue where ``get_cache_key`` may produce invalid memcached keys. --- dbtemplates/tests.py | 6 +++++- dbtemplates/utils/cache.py | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/dbtemplates/tests.py b/dbtemplates/tests.py index f5741d7..fc44a84 100644 --- a/dbtemplates/tests.py +++ b/dbtemplates/tests.py @@ -14,7 +14,7 @@ from django.contrib.sites.models import Site from dbtemplates.conf import settings from dbtemplates.models import Template -from dbtemplates.utils.cache import get_cache_backend +from dbtemplates.utils.cache import get_cache_backend, get_cache_key from dbtemplates.utils.template import (get_template_source, check_template_syntax) from dbtemplates.management.commands.sync_templates import (FILES_TO_DATABASE, @@ -142,3 +142,7 @@ class DbTemplatesTestCase(TestCase): name='good.html', content='{% if foo %}Bar{% endif %}') self.assertFalse(check_template_syntax(bad_template)[0]) self.assertTrue(check_template_syntax(good_template)[0]) + + def test_get_cache_name(self): + name = 'name with spaces' + self.assertEqual(get_cache_key(name), 'dbtemplates::name-with-spaces::1') \ No newline at end of file diff --git a/dbtemplates/utils/cache.py b/dbtemplates/utils/cache.py index 299647c..13a48d7 100644 --- a/dbtemplates/utils/cache.py +++ b/dbtemplates/utils/cache.py @@ -1,6 +1,7 @@ from django.core.cache import get_cache from django.contrib.sites.models import Site +from django.template.defaultfilters import slugify from dbtemplates.conf import settings @@ -13,7 +14,7 @@ cache = get_cache_backend() def get_cache_key(name): current_site = Site.objects.get_current() - return 'dbtemplates::%s::%s' % (name, current_site.pk) + return 'dbtemplates::%s::%s' % (slugify(name), current_site.pk) def get_cache_notfound_key(name):