diff --git a/onadata/apps/logger/__init__.py b/onadata/apps/logger/__init__.py index 57d631c3f..f63bf156a 100644 --- a/onadata/apps/logger/__init__.py +++ b/onadata/apps/logger/__init__.py @@ -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() diff --git a/onadata/apps/logger/signals.py b/onadata/apps/logger/signals.py new file mode 100644 index 000000000..04a1ad066 --- /dev/null +++ b/onadata/apps/logger/signals.py @@ -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) diff --git a/onadata/apps/logger/xform_instance_parser.py b/onadata/apps/logger/xform_instance_parser.py index 246c9fcd3..46abccf88 100644 --- a/onadata/apps/logger/xform_instance_parser.py +++ b/onadata/apps/logger/xform_instance_parser.py @@ -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 diff --git a/onadata/libs/utils/logger_tools.py b/onadata/libs/utils/logger_tools.py index 1e44af288..7b671ec6d 100644 --- a/onadata/libs/utils/logger_tools.py +++ b/onadata/libs/utils/logger_tools.py @@ -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 diff --git a/onadata/settings/base.py b/onadata/settings/base.py index e4290da80..8baa88a1e 100644 --- a/onadata/settings/base.py +++ b/onadata/settings/base.py @@ -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', diff --git a/onadata/settings/dev.py b/onadata/settings/dev.py index 154a64276..8db111d6c 100644 --- a/onadata/settings/dev.py +++ b/onadata/settings/dev.py @@ -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")