Skip to content

Commit

Permalink
Set file stream curstor to zero position after running validators (#218)
Browse files Browse the repository at this point in the history
The `BaseSizeValidator` read the file stream to create an in memory Pillow Image object. In reading the file, the file streams cursor was set to the end. Therefore, the validator left the file in a different state than receiving it. This patch sets the cursor back to zero after reading the file.
  • Loading branch information
jeromelebleu authored Apr 14, 2020
1 parent 7eeb13a commit 4c7194a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions stdimage/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def __call__(self, value):
def clean(value):
value.seek(0)
stream = BytesIO(value.read())
img = Image.open(stream)
return img.size
size = Image.open(stream).size
value.seek(0)
return size


class MaxSizeValidator(BaseSizeValidator):
Expand Down
6 changes: 4 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,17 @@ def test_render_variations_overwrite(self, db, image_upload_file):

class TestValidators(TestStdImage):
def test_max_size_validator(self, admin_client):
admin_client.post('/admin/tests/maxsizemodel/add/', {
response = admin_client.post('/admin/tests/maxsizemodel/add/', {
'image': self.fixtures['600x400.jpg'],
})
assert 'too large' in response.context['adminform'].form.errors['image'][0]
assert not os.path.exists(os.path.join(IMG_DIR, '800x600.jpg'))

def test_min_size_validator(self, admin_client):
admin_client.post('/admin/tests/minsizemodel/add/', {
response = admin_client.post('/admin/tests/minsizemodel/add/', {
'image': self.fixtures['100.gif'],
})
assert 'too small' in response.context['adminform'].form.errors['image'][0]
assert not os.path.exists(os.path.join(IMG_DIR, '100.gif'))


Expand Down

0 comments on commit 4c7194a

Please sign in to comment.