0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Handle deletion of original file in form.save

This commit is contained in:
Matt Westcott 2022-07-05 17:36:51 +01:00 committed by Matt Westcott
parent 9667088e98
commit 9d7f0d16c5
4 changed files with 22 additions and 14 deletions

View File

@ -34,6 +34,10 @@ def formfield_for_dbfield(db_field, **kwargs):
class BaseDocumentForm(BaseCollectionMemberForm): class BaseDocumentForm(BaseCollectionMemberForm):
permission_policy = documents_permission_policy permission_policy = documents_permission_policy
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.original_file = self.instance.file
def save(self, commit=True): def save(self, commit=True):
if "file" in self.changed_data: if "file" in self.changed_data:
self.instance._set_document_file_metadata() self.instance._set_document_file_metadata()
@ -41,6 +45,13 @@ class BaseDocumentForm(BaseCollectionMemberForm):
super().save(commit=commit) super().save(commit=commit)
if commit: if commit:
if "file" in self.changed_data and self.original_file:
# 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...
self.original_file.storage.delete(self.original_file.name)
self.original_file = None
# Reindex the image to make sure all tags are indexed # Reindex the image to make sure all tags are indexed
search_index.insert_or_update_object(self.instance) search_index.insert_or_update_object(self.instance)

View File

@ -173,17 +173,11 @@ def edit(request, document_id):
next_url = get_valid_next_url_from_request(request) next_url = get_valid_next_url_from_request(request)
if request.method == "POST": if request.method == "POST":
original_file = doc.file
form = DocumentForm( form = DocumentForm(
request.POST, request.FILES, instance=doc, user=request.user request.POST, request.FILES, instance=doc, user=request.user
) )
if form.is_valid(): if form.is_valid():
doc = form.save() doc = form.save()
if "file" in form.changed_data:
# 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)
edit_url = reverse("wagtaildocs:edit", args=(doc.id,)) edit_url = reverse("wagtaildocs:edit", args=(doc.id,))
redirect_url = "wagtaildocs:index" redirect_url = "wagtaildocs:index"

View File

@ -38,6 +38,10 @@ def formfield_for_dbfield(db_field, **kwargs):
class BaseImageForm(BaseCollectionMemberForm): class BaseImageForm(BaseCollectionMemberForm):
permission_policy = images_permission_policy permission_policy = images_permission_policy
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.original_file = self.instance.file
def save(self, commit=True): def save(self, commit=True):
if "file" in self.changed_data: if "file" in self.changed_data:
self.instance._set_image_file_metadata() self.instance._set_image_file_metadata()
@ -45,6 +49,13 @@ class BaseImageForm(BaseCollectionMemberForm):
super().save(commit=commit) super().save(commit=commit)
if commit: if commit:
if "file" in self.changed_data and self.original_file:
# if providing a new image file, delete the old one and all renditions.
# NB Doing this via original_file.delete() clears the file field,
# which definitely isn't what we want...
self.original_file.storage.delete(self.original_file.name)
self.instance.renditions.all().delete()
# Reindex the image to make sure all tags are indexed # Reindex the image to make sure all tags are indexed
search_index.insert_or_update_object(self.instance) search_index.insert_or_update_object(self.instance)

View File

@ -190,18 +190,10 @@ def edit(request, image_id):
next_url = get_valid_next_url_from_request(request) next_url = get_valid_next_url_from_request(request)
if request.method == "POST": if request.method == "POST":
original_file = image.file
form = ImageForm(request.POST, request.FILES, instance=image, user=request.user) form = ImageForm(request.POST, request.FILES, instance=image, user=request.user)
if form.is_valid(): if form.is_valid():
form.save() form.save()
if "file" in form.changed_data:
# if providing a new image file, delete the old one and all renditions.
# 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)
image.renditions.all().delete()
edit_url = reverse("wagtailimages:edit", args=(image.id,)) edit_url = reverse("wagtailimages:edit", args=(image.id,))
redirect_url = "wagtailimages:index" redirect_url = "wagtailimages:index"
if next_url: if next_url: