0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Add a _set_document_file_metadata helper method

This commit is contained in:
Matt Westcott 2022-07-05 15:58:34 +01:00 committed by Matt Westcott
parent a9aa3cdc21
commit fcb2776383
4 changed files with 14 additions and 31 deletions

View File

@ -134,6 +134,15 @@ class AbstractDocument(CollectionMember, index.Indexed, models.Model):
return self.file_hash
def _set_document_file_metadata(self):
# Set new document file size
self.file_size = self.file.size
# Set new document file hash
self.file.seek(0)
self._set_file_hash(self.file.read())
self.file.seek(0)
def __str__(self):
return self.title

View File

@ -194,13 +194,7 @@ class DocumentChooserUploadView(
def save_form(self, form):
document = form.instance
document.file_size = document.file.size
# Set new document file hash
document.file.seek(0)
document._set_file_hash(document.file.read())
document.file.seek(0)
document._set_document_file_metadata()
form.save()
# Reindex the document to make sure all tags are indexed

View File

@ -133,13 +133,7 @@ def add(request):
request.POST, request.FILES, instance=doc, user=request.user
)
if form.is_valid():
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._set_document_file_metadata()
form.save()
# Reindex the document to make sure all tags are indexed
@ -191,12 +185,7 @@ def edit(request, document_id):
if form.is_valid():
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._set_document_file_metadata()
doc.save()
form.save_m2m()

View File

@ -46,13 +46,7 @@ class AddView(BaseAddView):
def save_object(self, form):
doc = form.save(commit=False)
doc.uploaded_by_user = self.request.user
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._set_document_file_metadata()
doc.save()
return doc
@ -124,11 +118,8 @@ class CreateFromUploadedDocumentView(BaseCreateFromUploadView):
os.path.basename(self.upload.file.name), self.upload.file.file, save=False
)
self.object.uploaded_by_user = self.request.user
self.object.file_size = self.object.file.size
self.object.file.open()
self.object.file.seek(0)
self.object._set_file_hash(self.object.file.read())
self.object.file.seek(0)
self.object._set_document_file_metadata()
form.save()
# Reindex the document to make sure all tags are indexed