Skip to content

Commit ae4f605

Browse files
jmsmkncodingjoe
andauthored
Fix #227 -- Always warn about missing dimension fields (#229)
At least the width dimension is always needed to generate the full source set, regardless of the requested aspect ratio. Since only smaller versions of the original file are created, the original size is the upper bound for the source set. Close #227 Co-authored-by: Johannes Maron <[email protected]>
1 parent 0ec2e4d commit ae4f605

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ from pictures.models import PictureField
3333

3434
class Profile(models.Model):
3535
title = models.CharField(max_length=255)
36-
picture = PictureField(upload_to="avatars")
36+
picture = PictureField(
37+
upload_to="avatars", width_field="picture_width", height_field="picture_height"
38+
)
39+
picture_width = models.PositiveIntegerField(editable=False)
40+
picture_height = models.PositiveIntegerField(editable=False)
3741
```
3842

3943
```html
@@ -157,7 +161,11 @@ class Profile(models.Model):
157161
picture = PictureField(
158162
upload_to="avatars",
159163
aspect_ratios=[None, "1/1", "3/2", "16/9"],
164+
width_field="picture_width",
165+
height_field="picture_height",
160166
)
167+
picture_width = models.PositiveIntegerField(editable=False)
168+
picture_height = models.PositiveIntegerField(editable=False)
161169
```
162170

163171
```html
@@ -235,10 +243,15 @@ class Profile(models.Model):
235243
validators=[
236244
MinSizeValidator(400, 300), # At least 400x300 pixels
237245
MaxSizeValidator(4096, 4096), # At most 4096x4096 pixels
238-
]
246+
],
247+
width_field="picture_width",
248+
height_field="picture_height",
239249
)
250+
picture_width = models.PositiveIntegerField(editable=False)
251+
picture_height = models.PositiveIntegerField(editable=False)
252+
240253

241-
Use `None` to limit only one dimension: `MaxSizeValidator(2048, None)` limits only width.
254+
# Use `None` to limit only one dimension: `MaxSizeValidator(2048, None)` limits only width.
242255
```
243256

244257
> [!IMPORTANT]

pictures/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def _check_aspect_ratios(self):
291291
return errors
292292

293293
def _check_width_height_field(self):
294-
if None in self.aspect_ratios and not (self.width_field and self.height_field):
294+
if not (self.width_field and self.height_field):
295295
return [
296296
checks.Warning(
297297
"width_field and height_field attributes are missing",

tests/test_models.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,19 @@ def test_check_aspect_ratios(self):
825825
assert errors
826826
assert errors[0].id == "fields.E100"
827827

828-
def test_check_width_height_field(self):
829-
assert not PictureField(aspect_ratios=["3/2"])._check_width_height_field()
830-
with override_field_aspect_ratios(Profile.picture.field, [None]):
828+
@pytest.mark.parametrize(
829+
"aspect_ratios",
830+
[
831+
("3/2",),
832+
(None,),
833+
(
834+
"3/2",
835+
None,
836+
),
837+
],
838+
)
839+
def test_check_width_height_field(self, aspect_ratios):
840+
with override_field_aspect_ratios(Profile.picture.field, aspect_ratios):
831841
errors = Profile.picture.field._check_width_height_field()
832842
assert errors
833843
assert errors[0].id == "fields.E101"

0 commit comments

Comments
 (0)