Skip to content

Commit

Permalink
Set file size on upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedroho committed Jul 1, 2015
1 parent 4c85c39 commit 5018aa1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions wagtail/wagtailimages/tests/test_admin_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def test_add(self):
self.assertEqual(image.width, 640)
self.assertEqual(image.height, 480)

# Test that the file_size field was set
self.assertTrue(image.file_size)

def test_add_no_file_selected(self):
response = self.post({
'title': "Test image",
Expand Down Expand Up @@ -151,6 +154,25 @@ def test_edit(self):
image = Image.objects.get(id=self.image.id)
self.assertEqual(image.title, "Edited")

def test_edit_with_new_image_file(self):
file_content = get_test_image_file().file.getvalue()

# Change the file size of the image
self.image.file_size = 100000
self.image.save()

response = self.post({
'title': "Edited",
'file': SimpleUploadedFile('new.png', file_content),
})

# Should redirect back to index
self.assertRedirects(response, reverse('wagtailimages_index'))

# Check that the image file size changed (assume it changed to the correct value)
image = Image.objects.get(id=self.image.id)
self.assertNotEqual(image.file_size, 100000)

def test_with_missing_image_file(self):
self.image.file.delete(False)

Expand Down Expand Up @@ -330,6 +352,7 @@ def test_add_post(self):
# Check image
self.assertIn('image', response.context)
self.assertEqual(response.context['image'].title, 'test.png')
self.assertTrue(response.context['image'].file_size)

# Check form
self.assertIn('form', response.context)
Expand Down
7 changes: 7 additions & 0 deletions wagtail/wagtailimages/views/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def edit(request, image_id):
# which definitely isn't what we want...
original_file.storage.delete(original_file.name)
image.renditions.all().delete()

# Set new image file size
image.file_size = image.file.size

form.save()

# Reindex the image to make sure all tags are indexed
Expand Down Expand Up @@ -238,6 +242,9 @@ def add(request):
image = ImageModel(uploaded_by_user=request.user)
form = ImageForm(request.POST, request.FILES, instance=image)
if form.is_valid():
# Set image file size
image.file_size = image.file.size

form.save()

# Reindex the image to make sure all tags are indexed
Expand Down
1 change: 1 addition & 0 deletions wagtail/wagtailimages/views/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def add(request):
# Save it
image = form.save(commit=False)
image.uploaded_by_user = request.user
image.file_size = image.file.size
image.save()

# Success! Send back an edit form for this image to the user
Expand Down

0 comments on commit 5018aa1

Please sign in to comment.