Add __getstate__ method to ImageCacheFile

This commit is contained in:
nex2hex 2013-09-24 15:18:59 +04:00
parent 857b1e160e
commit 1c26a2ea5c
2 changed files with 22 additions and 1 deletions

View file

@ -1,3 +1,4 @@
from copy import copy
from django.conf import settings
from django.core.files import File
from django.core.files.images import ImageFile
@ -129,6 +130,14 @@ class ImageCacheFile(BaseIKFile, ImageFile):
existence_required.send(sender=self, file=self)
return self.cachefile_backend.exists(self)
def __getstate__(self):
state = copy(self.__dict__)
# file is hidden link to "file" attribute
state.pop('_file', None)
return state
class LazyImageCacheFile(SimpleLazyObject):
def __init__(self, generator_id, *args, **kwargs):

View file

@ -4,7 +4,9 @@ deserialized. This is important when using IK with Celery.
"""
from .utils import create_photo, pickleback
from imagekit.cachefiles import ImageCacheFile
from .imagegenerators import TestSpec
from .utils import create_photo, pickleback, get_unique_image_file
def test_imagespecfield():
@ -23,3 +25,13 @@ def test_circular_ref():
instance = create_photo('pickletest3.jpg')
instance.thumbnail # Cause thumbnail to be added to instance's __dict__
pickleback(instance)
def test_cachefiles():
spec = TestSpec(source=get_unique_image_file())
file = ImageCacheFile(spec)
file.url
# remove link to file from spec source generator
# test __getstate__ of ImageCacheFile
file.generator.source = None
pickleback(file)