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

Delete attachments from storage when their submission are deleted #791

Merged
merged 5 commits into from
Feb 22, 2022
Merged
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
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