Merge pull request #469 from matthewwithanm/fix-image-cachefile-serializtion

Fix pickle serialization for ImageCacheFile
This commit is contained in:
Venelin Stoykov 2018-06-03 18:21:48 +03:00 committed by GitHub
commit 6f7de35f79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

View file

@ -185,6 +185,11 @@ Or, in Python:
def on_source_saved(self, file):
file.generate()
.. note::
If you use custom storage backend for some specs,
(storage passed to the field different than configured one)
it's required the storage to be pickleable
__ https://pypi.python.org/pypi/django-celery

View file

@ -144,8 +144,24 @@ class ImageCacheFile(BaseIKFile, ImageFile):
# file is hidden link to "file" attribute
state.pop('_file', None)
# remove storage from state as some non-FileSystemStorage can't be
# pickled
settings_storage = get_singleton(
settings.IMAGEKIT_DEFAULT_FILE_STORAGE,
'file storage backend'
)
if state['storage'] == settings_storage:
state.pop('storage')
return state
def __setstate__(self, state):
if 'storage' not in state:
state['storage'] = get_singleton(
settings.IMAGEKIT_DEFAULT_FILE_STORAGE,
'file storage backend'
)
self.__dict__.update(state)
def __nonzero__(self):
# Python 2 compatibility
return self.__bool__()

View file

@ -37,4 +37,7 @@ def test_cachefiles():
# remove link to file from spec source generator
# test __getstate__ of ImageCacheFile
file.generator.source = None
pickleback(file)
restored_file = pickleback(file)
assert file is not restored_file
# Assertion for #437 and #451
assert file.storage is restored_file.storage