diff --git a/imagekit/utils.py b/imagekit/utils.py index 598be43..baaf234 100644 --- a/imagekit/utils.py +++ b/imagekit/utils.py @@ -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 diff --git a/tests/test_cachefiles.py b/tests/test_cachefiles.py index 4338a82..ba250fb 100644 --- a/tests/test_cachefiles.py +++ b/tests/test_cachefiles.py @@ -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():