Update document size & hash when changing document file

Resolves #5704
pull/5679/head
Andreas Bernacca 2019-11-13 20:10:35 +01:00 zatwierdzone przez LB
rodzic 5a8f468c2d
commit 4945acf2db
4 zmienionych plików z 36 dodań i 2 usunięć

Wyświetl plik

@ -23,6 +23,7 @@ Changelog
* Fix: Fixed incorrect HTML escaping of Jinja2 form templates for StructBlocks (Brady Moe)
* Fix: All templates with wagtailsettings and modeladmin now use `block.super` for `extra_js` & `extra_css` (Timothy Bautista)
* Fix: Layout issue when using FieldRowPanel with a heading (Andreas Bernacca)
* Fix: `file_size` and `file_hash` not updated when Document file changed (Andreas Bernacca)
2.7 LTS (06.11.2019)
~~~~~~~~~~~~~~~~~~~~

Wyświetl plik

@ -37,6 +37,7 @@ Bug fixes
* Fixed incorrect HTML escaping of Jinja2 form templates for StructBlocks (Brady Moe)
* All templates with wagtailsettings and modeladmin now use ``block.super`` for ``extra_js`` & ``extra_css`` (Timothy Bautista)
* Layout issue when using FieldRowPanel with a heading (Andreas Bernacca)
* ``file_size`` and ``file_hash`` not updated when Document file changed (Andreas Bernacca)
Upgrade considerations

Wyświetl plik

@ -321,6 +321,34 @@ class TestDocumentEditView(TestCase, WagtailTestUtils):
self.assertContains(response, self.document.usage_url)
self.assertContains(response, 'Used 0 times')
def test_reupload_different_file_size_and_file_hash(self):
"""
Checks that reuploading the document file with a different file
changes the file size and file hash (see #5704).
"""
# Build a fake file, and create it through the admin view
# since self.document doesn't have a file_size set.
fake_file = SimpleUploadedFile('some_file.txt', b'this is the content')
post_data = {
'title': "My doc",
'file': fake_file,
}
self.client.post(reverse('wagtaildocs:add'), post_data)
document = models.Document.objects.get(title="My doc")
old_file_size, old_file_hash = document.file_size, document.file_hash
new_file = SimpleUploadedFile(document.filename, b'less content')
self.client.post(reverse('wagtaildocs:edit', args=(document.pk,)), {
'title': document.title, 'file': new_file,
})
document.refresh_from_db()
self.assertNotEqual(document.file_size, old_file_size)
self.assertNotEqual(document.file_hash, old_file_hash)
def test_reupload_same_name(self):
"""
Checks that reuploading the document file with the same file name

Wyświetl plik

@ -140,23 +140,27 @@ def edit(request, document_id):
original_file = doc.file
form = DocumentForm(request.POST, request.FILES, instance=doc, user=request.user)
if form.is_valid():
doc = form.save()
if 'file' in form.changed_data:
doc = form.save(commit=False)
doc.file_size = doc.file.size
# Set new document file hash
doc.file.seek(0)
doc._set_file_hash(doc.file.read())
doc.file.seek(0)
doc.save()
# if providing a new document file, delete the old one.
# If providing a new document file, delete the old one.
# NB Doing this via original_file.delete() clears the file field,
# which definitely isn't what we want...
original_file.storage.delete(original_file.name)
else:
doc = form.save()
# Reindex the document to make sure all tags are indexed
search_index.insert_or_update_object(doc)
messages.success(request, _("Document '{0}' updated").format(doc.title), buttons=[
messages.button(reverse('wagtaildocs:edit', args=(doc.id,)), _('Edit'))
])