Skip to content

Commit fac3d5a

Browse files
committed
Ensure cache keys are safe to use
Cache keys can't contain characters outside of a fairly limited ASCII range. Base 64 encoding it should be safe. Fixes #19.
1 parent 9ed041c commit fac3d5a

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

docs/changelog.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Release Notes
22
=============
33

4+
v0.6.1
5+
------
6+
7+
* Modify cache name, to prevent warnings for non-ASCII characters or
8+
whitespace (thanks @ad-m).
9+
410
v0.6.0
511
------
612

docs/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
# built documents.
5151
#
5252
# The short X.Y version.
53-
version = '0.6.0'
53+
version = '0.6.1'
5454
# The full version, including alpha/beta/rc tags.
55-
release = '0.6.0'
55+
release = '0.6.1'
5656

5757
# The language for content autogenerated by Sphinx. Refer to documentation
5858
# for a list of supported languages.

setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[wheel]
22
universal = 1
3+
4+
[flake8]
5+
ignore = F999

tests/conftest.py

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ def simple_content():
1717
return content
1818

1919

20+
@pytest.fixture()
21+
def simple_content_with_space():
22+
content, _ = TinyContent.objects.get_or_create(
23+
name='foo bar',
24+
content='This is a test with a space.'
25+
)
26+
return content
27+
28+
2029
@pytest.fixture()
2130
def split_content():
2231
content, _ = TinyContent.objects.get_or_create(

tests/test_simple.py

+7
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ def test_simple_existent(simple_content):
1313
assert "This is a test." == render_template(
1414
"{% tinycontent_simple 'foobar' %}"
1515
)
16+
17+
18+
@pytest.mark.django_db
19+
def test_simple_with_space(simple_content_with_space):
20+
assert "This is a test with a space." == render_template(
21+
"{% tinycontent_simple 'foo bar' %}"
22+
)

tinycontent/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = '0.6.0'
1+
__version__ = '0.6.1'
22
VERSION = tuple(map(int, __version__.split('.')))

tinycontent/compat.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import base64
12
import django
3+
from django.utils import six
24

35

46
if django.VERSION > (1, 8,):
@@ -43,3 +45,12 @@ def render_to_string(
4345
"Using the request keyword argument requires Django >= 1.8"
4446
)
4547
return _render_to_string(template_name, context)
48+
49+
50+
def cache_safe_key(key):
51+
if six.PY2:
52+
return base64.b64encode(
53+
key
54+
)
55+
56+
return base64.b64encode(bytes(key, 'utf-8'))

tinycontent/models.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.db import models
44
from django.utils.encoding import python_2_unicode_compatible
55
from tinycontent.conf import get_filter_list
6+
from tinycontent.compat import cache_safe_key
67

78

89
@python_2_unicode_compatible
@@ -36,7 +37,7 @@ def get_content_by_name(name):
3637

3738
@staticmethod
3839
def get_cache_key(name):
39-
return 'tinycontent_%s' % name
40+
return 'tinycontent_%s' % cache_safe_key(name)
4041

4142
def delete(self, *args, **kwargs):
4243
cache.delete(TinyContent.get_cache_key(self.name))

0 commit comments

Comments
 (0)