Fix sanitizing cache key under Python 3

This commit is contained in:
Venelin Stoykov 2013-12-14 18:56:47 +02:00
parent bf1b45c943
commit fb947b1937
2 changed files with 12 additions and 4 deletions

View file

@ -1,14 +1,18 @@
from __future__ import unicode_literals
import logging
import re
from tempfile import NamedTemporaryFile
from hashlib import md5
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.files import File
from django.utils.importlib import import_module
from hashlib import md5
try:
from django.utils.encoding import force_bytes
except ImportError:
from django.utils.encoding import smart_str as force_bytes
from pilkit.utils import *
import re
from .lib import NullHandler
@ -148,7 +152,7 @@ def sanitize_cache_key(key):
# The also can't be > 250 chars long. Since we don't know what the
# user's cache ``KEY_FUNCTION`` setting is like, we'll limit it to 200.
if len(new_key) >= 200:
new_key = '%s:%s' % (new_key[:200-33], md5(key).hexdigest())
new_key = '%s:%s' % (new_key[:200-33], md5(force_bytes(key)).hexdigest())
key = new_key
return key

View file

@ -1,4 +1,8 @@
from django.conf import settings
try:
from django.utils.encoding import force_bytes
except ImportError:
from django.utils.encoding import smart_str as force_bytes
from hashlib import md5
from imagekit.cachefiles import ImageCacheFile, LazyImageCacheFile
from imagekit.cachefiles.backends import Simple
@ -73,7 +77,7 @@ def test_memcached_cache_key():
eq_(backend.get_key(file), '%s%s:%s' % (
settings.IMAGEKIT_CACHE_PREFIX,
'1' * (200 - len(':') - 32 - len(settings.IMAGEKIT_CACHE_PREFIX)),
md5('%s%s-state' % (settings.IMAGEKIT_CACHE_PREFIX, filename)).hexdigest()))
md5(force_bytes('%s%s-state' % (settings.IMAGEKIT_CACHE_PREFIX, filename))).hexdigest()))
def test_lazyfile_stringification():