Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIANXKE-380: avoid file deletion before Attachment removal #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions drf_attachments/handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from django.db.models.signals import post_delete
from django.dispatch import receiver

Expand Down
18 changes: 0 additions & 18 deletions drf_attachments/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ def set_and_validate(self):
self.set_default_context() # set the default context if yet empty (and if default is defined)
self.validate_file() # validate the file and its mime_type, extension and size
self.manage_uniqueness() # remove any other Attachments for content_objects with
self.cleanup_file() # remove the old file of a changed Attachment

def set_default_context(self):
"""Set context to settings.ATTACHMENT_DEFAULT_CONTEXT (if defined) if it's still empty"""
Expand Down Expand Up @@ -313,20 +312,3 @@ def manage_uniqueness(self):
remove_file(attachment.file.path)

to_delete.delete()

def cleanup_file(self):
"""
If an Attachment is updated and receives a new file, remove the previous file from the storage
"""
if self.pk:
try:
# on update delete the old file if a new one was inserted
# (delete_orphan only removes image on deletion of the whole attachment instance)
old_instance = Attachment.objects.get(pk=self.pk)

# on update delete the old file if a new one was inserted
if old_instance.file != self.file:
remove_file(old_instance.file.path)
except Attachment.DoesNotExist:
# Do nothing if Attachment does not yet exist in DB
pass
1 change: 1 addition & 0 deletions drf_attachments/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def remove_file(file_path, raise_exceptions=False):
def get_api_attachment_url(attachment_pk):
return reverse("attachment-download", kwargs={"pk": attachment_pk})


def get_admin_attachment_url(attachment_pk):
return reverse(
"admin:drf_attachments_attachment_download",
Expand Down
7 changes: 6 additions & 1 deletion tests/testapp/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def test_unique_upload(self):
self.assertEqual(1, attachments.count())
attachment = attachments[0]
first_attachment_file_path = attachment.file.path
self.assertTrue(os.path.isfile(first_attachment_file_path))

# check diagram model
diagram_attachments = self.diagram.attachments.all()
Expand Down Expand Up @@ -327,13 +328,17 @@ def test_attachment_object_is_deleted_with_content_object(self):
content_object=self.file,
file_name=DemoFile.PDF,
)
attachment = Attachment.objects.first()
attachment_file_path = attachment.file.path
self.assertTrue(os.path.isfile(attachment_file_path))

# delete the File model
response = self.client.delete(f"/api/file/{self.file.pk}/")
self.assertEqual(HTTP_204_NO_CONTENT, response.status_code, response.content)

# check that the attachment was deleted as well
# check that the attachment and its file were deleted as well
self.assertEqual(0, len(Attachment.objects.all()))
self.assertFalse(os.path.isfile(attachment_file_path))

def test_physical_file_is_deleted_with_content_object(self):
# add an attachment to File model
Expand Down
Loading