Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix deepcopy & pickle for unsaved model instances & empty image fields #324

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions stdimage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ def __getstate__(self):
state = super().__getstate__()
state["variations"] = {}
for variation_name in self.field.variations:
variation = getattr(self, variation_name)
variation_state = variation.__getstate__()
state["variations"][variation_name] = variation_state
if variation := getattr(self, variation_name, None):
variation_state = variation.__getstate__()
state["variations"][variation_name] = variation_state
return state

def __setstate__(self, state):
Expand Down Expand Up @@ -207,7 +207,7 @@ def __init__(
render_variations=True,
force_min_size=False,
delete_orphans=False,
**kwargs
**kwargs,
):
"""
Standardized ImageField for Django.
Expand Down
14 changes: 14 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ def test_defer(self, db, django_assert_num_queries):
deferred.image
assert instance.image.thumbnail == deferred.image.thumbnail

@pytest.mark.django_db
def test_variations_deepcopy_unsaved(self):
instance_original = ResizeModel(image=self.fixtures["600x400.jpg"])
instance = deepcopy(instance_original)
assert isinstance(instance.image, StdImageFieldFile)
assert instance.image == instance_original.image

@pytest.mark.django_db
def test_variations_deepcopy_without_image(self):
instance_original = ThumbnailModel.objects.create(image=None)
instance = deepcopy(instance_original)
assert isinstance(instance.image, StdImageFieldFile)
assert instance.image == instance_original.image

@pytest.mark.django_db
def test_variations_deepcopy(self):
"""Tests test_variations() with a deep copied object"""
Expand Down
Loading