Skip to content

Commit 4c7194a

Browse files
authored
Set file stream curstor to zero position after running validators (#218)
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.
1 parent 7eeb13a commit 4c7194a

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

stdimage/validators.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ def __call__(self, value):
2828
def clean(value):
2929
value.seek(0)
3030
stream = BytesIO(value.read())
31-
img = Image.open(stream)
32-
return img.size
31+
size = Image.open(stream).size
32+
value.seek(0)
33+
return size
3334

3435

3536
class MaxSizeValidator(BaseSizeValidator):

tests/test_models.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,17 @@ def test_render_variations_overwrite(self, db, image_upload_file):
227227

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

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

241243

0 commit comments

Comments
 (0)