Skip to content

Commit

Permalink
Merge pull request #791 from kobotoolbox/remove-attachments
Browse files Browse the repository at this point in the history
Delete attachments from storage on submissions deletion
  • Loading branch information
jnm authored Feb 22, 2022
2 parents a6a49d4 + 9979941 commit 295338f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
11 changes: 11 additions & 0 deletions onadata/apps/logger/__init__.py
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()
23 changes: 23 additions & 0 deletions onadata/apps/logger/signals.py
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)
1 change: 0 additions & 1 deletion onadata/apps/logger/xform_instance_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ def _set_attributes(self):
logger = logging.getLogger("console_logger")
logger.debug("Skipping duplicate attribute: %s"
" with value %s" % (key, value))
logger.debug(str(all_attributes))
else:
self._attributes[key] = value

Expand Down
3 changes: 2 additions & 1 deletion onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
clean_and_parse_xml,
get_uuid_from_xml,
get_deprecated_uuid_from_xml,
get_submission_date_from_xml)
get_submission_date_from_xml,
)
from onadata.apps.main.models import UserProfile
from onadata.apps.viewer.models.data_dictionary import DataDictionary
from onadata.apps.viewer.models.parsed_instance import ParsedInstance
Expand Down
2 changes: 1 addition & 1 deletion onadata/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def skip_suspicious_operations(record):
'rest_framework.authtoken',
'taggit',
'readonly',
'onadata.apps.logger',
'onadata.apps.logger.LoggerAppConfig',
'onadata.apps.viewer',
'onadata.apps.main',
'onadata.apps.restservice',
Expand Down
5 changes: 0 additions & 5 deletions onadata/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
# Django Framework settings #
################################

LOGGING['root'] = {
'handlers': ['console'],
'level': 'DEBUG'
}

SESSION_ENGINE = "redis_sessions.session"
SESSION_REDIS = RedisHelper.config(default="redis://redis_cache:6380/2")

Expand Down

0 comments on commit 295338f

Please sign in to comment.