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:
parent
9667088e98
commit
9d7f0d16c5
@ -34,6 +34,10 @@ def formfield_for_dbfield(db_field, **kwargs):
|
||||
class BaseDocumentForm(BaseCollectionMemberForm):
|
||||
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):
|
||||
if "file" in self.changed_data:
|
||||
self.instance._set_document_file_metadata()
|
||||
@ -41,6 +45,13 @@ class BaseDocumentForm(BaseCollectionMemberForm):
|
||||
super().save(commit=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
|
||||
search_index.insert_or_update_object(self.instance)
|
||||
|
||||
|
@ -173,17 +173,11 @@ def edit(request, document_id):
|
||||
next_url = get_valid_next_url_from_request(request)
|
||||
|
||||
if request.method == "POST":
|
||||
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:
|
||||
# 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,))
|
||||
redirect_url = "wagtaildocs:index"
|
||||
|
@ -38,6 +38,10 @@ def formfield_for_dbfield(db_field, **kwargs):
|
||||
class BaseImageForm(BaseCollectionMemberForm):
|
||||
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):
|
||||
if "file" in self.changed_data:
|
||||
self.instance._set_image_file_metadata()
|
||||
@ -45,6 +49,13 @@ class BaseImageForm(BaseCollectionMemberForm):
|
||||
super().save(commit=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
|
||||
search_index.insert_or_update_object(self.instance)
|
||||
|
||||
|
@ -190,18 +190,10 @@ def edit(request, image_id):
|
||||
next_url = get_valid_next_url_from_request(request)
|
||||
|
||||
if request.method == "POST":
|
||||
original_file = image.file
|
||||
form = ImageForm(request.POST, request.FILES, instance=image, user=request.user)
|
||||
if form.is_valid():
|
||||
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,))
|
||||
redirect_url = "wagtailimages:index"
|
||||
if next_url:
|
||||
|
Loading…
Reference in New Issue
Block a user