Skip to content

Commit f361b9c

Browse files
committed
Fix caching issue on empty dimention fields
The width and height fields may be empty, like in cases where you newly add the fields. In those cases, they width and hight properties shall not return the false null values but read the dimentions form file.
1 parent 640a4f8 commit f361b9c

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

pictures/models.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,19 @@ def update_all(self, from_aspect_ratios):
121121
@property
122122
def width(self):
123123
self._require_file()
124-
if (
125-
self._committed
126-
and self.field.width_field
127-
and hasattr(self.instance, self.field.width_field)
128-
):
129-
# get width from width field, to avoid loading image
130-
return getattr(self.instance, self.field.width_field)
124+
if self._committed and self.field.width_field:
125+
if width := getattr(self.instance, self.field.width_field, None):
126+
# get width from width field, to avoid loading image
127+
return width
131128
return self._get_image_dimensions()[0]
132129

133130
@property
134131
def height(self):
135132
self._require_file()
136-
if (
137-
self._committed
138-
and self.field.height_field
139-
and hasattr(self.instance, self.field.height_field)
140-
):
141-
# get height from height field, to avoid loading image
142-
return getattr(self.instance, self.field.height_field)
133+
if self._committed and self.field.height_field:
134+
if height := getattr(self.instance, self.field.height_field, None):
135+
# get height from height field, to avoid loading image
136+
return height
143137
return self._get_image_dimensions()[1]
144138

145139
@property

tests/test_models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ def test_update_all(self, stub_worker, image_upload_file):
186186
assert obj.picture.aspect_ratios["1/1"]["WEBP"][100].path.exists()
187187
assert not path.exists()
188188

189+
@pytest.mark.django_db
190+
def test_width(self, stub_worker, image_upload_file):
191+
obj = SimpleModel(picture=image_upload_file)
192+
obj.save()
193+
obj.picture_width = None
194+
195+
assert obj.picture.width == 800
196+
197+
@pytest.mark.django_db
198+
def test_height(self, stub_worker, image_upload_file):
199+
obj = SimpleModel(picture=image_upload_file)
200+
obj.save()
201+
obj.picture_height = None
202+
203+
assert obj.picture.height == 800
204+
189205

190206
class TestPictureField:
191207
@pytest.mark.django_db

0 commit comments

Comments
 (0)