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 3 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
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
jnm marked this conversation as resolved.
Show resolved Hide resolved
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):
jnm marked this conversation as resolved.
Show resolved Hide resolved
# 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.
jnm marked this conversation as resolved.
Show resolved Hide resolved
try:
instance.media_file.delete()
except Exception as e:
logger = logging.getLogger('console_logger')
logger.error(str(e), exc_info=True)
jnm marked this conversation as resolved.
Show resolved Hide resolved
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