mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-05-24 20:23:44 +00:00
Use state--not constructor args--to recreate dynamic specs
Previously, we were relying on `__init__`'s arguments to recreate specs. Now we do it the proper way, using the dict returned by `__getstate__` (which may or may not include those arguments).
This commit is contained in:
parent
042bdcefb6
commit
12307c97aa
1 changed files with 16 additions and 5 deletions
|
|
@ -147,16 +147,27 @@ def create_spec_class(class_attrs):
|
||||||
|
|
||||||
class DynamicSpecBase(ImageSpec):
|
class DynamicSpecBase(ImageSpec):
|
||||||
def __reduce__(self):
|
def __reduce__(self):
|
||||||
kwargs = dict(self.kwargs)
|
try:
|
||||||
kwargs['source_file'] = self.source_file
|
getstate = self.__getstate__
|
||||||
return (create_spec, (class_attrs, kwargs))
|
except AttributeError:
|
||||||
|
state = self.__dict__
|
||||||
|
else:
|
||||||
|
state = getstate()
|
||||||
|
return (create_spec, (class_attrs, state))
|
||||||
|
|
||||||
return type('DynamicSpec', (DynamicSpecBase,), class_attrs)
|
return type('DynamicSpec', (DynamicSpecBase,), class_attrs)
|
||||||
|
|
||||||
|
|
||||||
def create_spec(class_attrs, kwargs):
|
def create_spec(class_attrs, state):
|
||||||
cls = create_spec_class(class_attrs)
|
cls = create_spec_class(class_attrs)
|
||||||
return cls(**kwargs)
|
instance = cls.__new__(cls) # Create an instance without calling the __init__ (which may have required args).
|
||||||
|
try:
|
||||||
|
setstate = instance.__setstate__
|
||||||
|
except AttributeError:
|
||||||
|
instance.__dict__ = state
|
||||||
|
else:
|
||||||
|
setstate(state)
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
class SpecHost(object):
|
class SpecHost(object):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue