Merge branch 'kaedroho-image-file-size'

pull/1534/head
Matt Westcott 2015-07-20 14:40:55 +01:00
commit 4f53b0b0ed
6 zmienionych plików z 101 dodań i 9 usunięć

Wyświetl plik

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('tests', '0005_streampage'),
]
operations = [
migrations.AddField(
model_name='customimagewithadminformfields',
name='file_size',
field=models.PositiveIntegerField(null=True, editable=False),
),
migrations.AddField(
model_name='customimagewithoutadminformfields',
name='file_size',
field=models.PositiveIntegerField(null=True, editable=False),
),
]

Wyświetl plik

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('wagtailimages', '0006_add_verbose_names'),
]
operations = [
migrations.AddField(
model_name='image',
name='file_size',
field=models.PositiveIntegerField(editable=False, null=True),
),
]

Wyświetl plik

@ -73,6 +73,20 @@ class AbstractImage(models.Model, TagSearchable):
focal_point_width = models.PositiveIntegerField(null=True, blank=True)
focal_point_height = models.PositiveIntegerField(null=True, blank=True)
file_size = models.PositiveIntegerField(null=True, editable=False)
def get_file_size(self):
if self.file_size is None:
try:
self.file_size = self.file.size
except OSError:
# File doesn't exist
return
self.save(update_fields=['file_size'])
return self.file_size
def get_usage(self):
return get_object_usage(self)

Wyświetl plik

@ -86,6 +86,9 @@ class TestImageAddView(TestCase, WagtailTestUtils):
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",
@ -151,6 +154,25 @@ class TestImageEditView(TestCase, WagtailTestUtils):
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)
@ -330,6 +352,7 @@ class TestMultipleImageUploader(TestCase, WagtailTestUtils):
# 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)

Wyświetl plik

@ -1,3 +1,4 @@
import os
import json
from django.shortcuts import render, redirect, get_object_or_404
@ -96,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
@ -118,21 +123,24 @@ def edit(request, image_id):
except NoReverseMatch:
url_generator_enabled = False
# Get file size
try:
filesize = image.file.size
except OSError:
# File doesn't exist
filesize = None
messages.error(request, _("The source image file could not be found. Please change the source or delete the image.").format(image.title), buttons=[
messages.button(reverse('wagtailimages_delete_image', args=(image.id,)), _('Delete'))
])
local_path = image.file.path
except NotImplementedError:
# Image is hosted externally (eg, S3)
local_path = None
if local_path:
# Give error if image file doesn't exist
if not os.path.isfile(local_path):
messages.error(request, _("The source image file could not be found. Please change the source or delete the image.").format(image.title), buttons=[
messages.button(reverse('wagtailimages_delete_image', args=(image.id,)), _('Delete'))
])
return render(request, "wagtailimages/images/edit.html", {
'image': image,
'form': form,
'url_generator_enabled': url_generator_enabled,
'filesize': filesize,
'filesize': image.get_file_size(),
})
@ -234,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

Wyświetl plik

@ -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