Skip to content

Commit

Permalink
Remove attachment files from storage on Instance objects deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Feb 22, 2022
1 parent a6a49d4 commit 127a84b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
12 changes: 12 additions & 0 deletions onadata/apps/logger/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# 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
# Uncomment the lines below if you need signals
from onadata.apps.logger import signals
super().ready()
19 changes: 19 additions & 0 deletions onadata/apps/logger/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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 post_delete_asset(instance, **kwargs):
# Unfortunately, it seems that Django does not call Model.delete() on
# delete CASCADE. But this signal is called though.
# We want to delete all files when an Instance (or Attachment) object is
# deleted.
try:
instance.media_file.delete()
except Exception as e:
logger = logging.getLogger('console_logger')
logger.error(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
4 changes: 3 additions & 1 deletion onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import traceback
from datetime import date, datetime
from xml.etree import ElementTree as ET
from xml.parsers.expat import ExpatError

import pytz
Expand Down Expand Up @@ -53,7 +54,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

0 comments on commit 127a84b

Please sign in to comment.