Skip to content

Change the stream position after reading in validators #218

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

Merged
merged 1 commit into from
Apr 14, 2020
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
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