From 83e46c072362f52b81c130a671c49460c5ade8ad Mon Sep 17 00:00:00 2001 From: Johannes Hoppe Date: Thu, 2 Apr 2020 11:01:15 +0200 Subject: [PATCH] Add support for Pillow 7.1.0 --- README.md | 4 +-- stdimage/models.py | 14 +++++++--- stdimage/validators.py | 6 ++-- tests/models.py | 2 +- tests/test_validators.py | 60 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 tests/test_validators.py diff --git a/README.md b/README.md index 7ee097f..709b586 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,11 @@ class MyModel(models.Model): # is the same as dictionary-style call image = StdImageField(upload_to='path/to/img', variations={'thumbnail': (100, 75)}) - + # variations are converted to JPEGs jpeg = JPEGField( upload_to='path/to/img', - variations={'full': (float('inf'), float('inf')), 'thumbnail': (100, 75)}, + variations={'full': (None, None), 'thumbnail': (100, 75)}, ) # creates a thumbnail resized to 100x100 croping if necessary diff --git a/stdimage/models.py b/stdimage/models.py index 354dc09..e1528b6 100644 --- a/stdimage/models.py +++ b/stdimage/models.py @@ -102,7 +102,7 @@ def process_variation(cls, variation, image): ) size = variation['width'], variation['height'] - size = tuple(int(i) if i != float('inf') else i + size = tuple(int(i) if i is not None else i for i in size) if file_format == 'JPEG': @@ -168,8 +168,8 @@ class StdImageField(ImageField): descriptor_class = StdImageFileDescriptor attr_class = StdImageFieldFile def_variation = { - 'width': float('inf'), - 'height': float('inf'), + 'width': None, + 'height': None, 'crop': False, 'resample': Image.ANTIALIAS, } @@ -308,6 +308,12 @@ def process_variation(cls, variation, image): resample = variation['resample'] + if variation['width'] is None: + variation['width'] = image.size[0] + + if variation['height'] is None: + variation['height'] = image.size[1] + factor = 1 while image.size[0] / factor \ > 2 * variation['width'] \ @@ -322,7 +328,7 @@ def process_variation(cls, variation, image): ) size = variation['width'], variation['height'] - size = tuple(int(i) if i != float('inf') else i + size = tuple(int(i) if i is not None else i for i in size) # http://stackoverflow.com/a/21669827 diff --git a/stdimage/validators.py b/stdimage/validators.py index b273f0b..f0abd7d 100644 --- a/stdimage/validators.py +++ b/stdimage/validators.py @@ -13,7 +13,7 @@ def compare(self, x): return True def __init__(self, width, height): - self.limit_value = width, height + self.limit_value = width or float('inf'), height or float('inf') def __call__(self, value): cleaned = self.clean(value) @@ -36,7 +36,7 @@ class MaxSizeValidator(BaseSizeValidator): """ ImageField validator to validate the max width and height of an image. - You may use float("inf") as an infinite boundary. + You may use None as an infinite boundary. """ def compare(self, img_size, max_size): @@ -51,7 +51,7 @@ class MinSizeValidator(BaseSizeValidator): """ ImageField validator to validate the min width and height of an image. - You may use float("inf") as an infinite boundary. + You may use None as an infinite boundary. """ def compare(self, img_size, min_size): diff --git a/tests/models.py b/tests/models.py index 8f861e0..34a4255 100644 --- a/tests/models.py +++ b/tests/models.py @@ -65,7 +65,7 @@ class JPEGModel(models.Model): upload_to=upload_to, blank=True, variations={ - 'full': (float('inf'), float('inf')), + 'full': (None, None), 'thumbnail': (100, 75, True), }, delete_orphans=True, diff --git a/tests/test_validators.py b/tests/test_validators.py new file mode 100644 index 0000000..4a3b4ac --- /dev/null +++ b/tests/test_validators.py @@ -0,0 +1,60 @@ +from stdimage import validators + + +class TestBaseSizeValidator: + def test_init__none(self): + assert validators.MinSizeValidator(None, None).limit_value == ( + float('inf'), float('inf') + ) + + +class TestMaxSizeValidator: + def test_compare__inf(self): + limit_value = float("inf"), float("inf") + instance = validators.MaxSizeValidator(*limit_value) + assert not instance.compare((300, 200), limit_value) + + def test_compare__eq(self): + assert not validators.MaxSizeValidator(300, 200).compare((300, 200), (300, 200)) + + def test_compare__gt(self): + limit_value = 300, 200 + instance = validators.MaxSizeValidator(*limit_value) + assert instance.compare((600, 400), limit_value) + assert instance.compare((600, 200), limit_value) + assert instance.compare((300, 400), limit_value) + assert instance.compare((600, 100), limit_value) + assert instance.compare((150, 400), limit_value) + + def test_compare__lt(self): + limit_value = 300, 200 + instance = validators.MaxSizeValidator(*limit_value) + assert not instance.compare((150, 100), (300, 200)) + assert not instance.compare((300, 100), (300, 200)) + assert not instance.compare((150, 200), (300, 200)) + + +class TestMinSizeValidator: + def test_compare__inf(self): + limit_value = float("inf"), float("inf") + instance = validators.MinSizeValidator(*limit_value) + assert instance.compare((300, 200), limit_value) + + def test_compare__eq(self): + assert not validators.MinSizeValidator(300, 200).compare((300, 200), (300, 200)) + + def test_compare__gt(self): + limit_value = 300, 200 + instance = validators.MinSizeValidator(*limit_value) + assert not instance.compare((600, 400), limit_value) + assert not instance.compare((600, 200), limit_value) + assert not instance.compare((300, 400), limit_value) + assert instance.compare((600, 100), limit_value) + assert instance.compare((150, 400), limit_value) + + def test_compare__lt(self): + limit_value = 300, 200 + instance = validators.MinSizeValidator(*limit_value) + assert instance.compare((150, 100), (300, 200)) + assert instance.compare((300, 100), (300, 200)) + assert instance.compare((150, 200), (300, 200))