-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #791 from kobotoolbox/remove-attachments
Delete attachments from storage on submissions deletion
- Loading branch information
Showing
6 changed files
with
37 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
# coding: utf-8 | ||
from django.apps import AppConfig | ||
|
||
|
||
class LoggerAppConfig(AppConfig): | ||
|
||
name = 'onadata.apps.logger' | ||
|
||
def ready(self): | ||
# Makes sure all signal handlers are connected | ||
from onadata.apps.logger import signals | ||
super().ready() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# coding: utf-8 | ||
import logging | ||
from django.db.models.signals import pre_delete | ||
from django.dispatch import receiver | ||
|
||
from onadata.apps.logger.models.attachment import Attachment | ||
|
||
|
||
@receiver(pre_delete, sender=Attachment) | ||
def pre_delete_attachment(instance, **kwargs): | ||
# "Model.delete() isn’t called on related models, but the pre_delete and | ||
# post_delete signals are sent for all deleted objects." See | ||
# https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.CASCADE | ||
# We want to delete all files when an Instance (or Attachment) object is | ||
# deleted. | ||
|
||
# `instance` here means "model instance", and no, it is not allowed to | ||
# change the name of the parameter | ||
attachment = instance | ||
try: | ||
attachment.media_file.delete() | ||
except Exception as e: | ||
logging.error('Failed to delete attachment: ' + str(e), exc_info=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters