diff --git a/src/metax_api/api/rest/base/views/common_view.py b/src/metax_api/api/rest/base/views/common_view.py index 14de18ca..f5f0be75 100755 --- a/src/metax_api/api/rest/base/views/common_view.py +++ b/src/metax_api/api/rest/base/views/common_view.py @@ -8,6 +8,7 @@ import logging from os import path +from django.conf import settings from django.http import Http404, HttpResponse from rest_framework import status from rest_framework.exceptions import APIException, MethodNotAllowed, PermissionDenied @@ -141,7 +142,10 @@ def handle_exception(self, exc): try: error_json = ApiErrorSerializerV2.request_to_json(self.request, response) response.data["error_identifier"] = error_json["identifier"] - rabbitmq.publish(error_json, exchange="apierrors") + if settings.ENABLE_API_ERROR_OBJECTS: + rabbitmq.publish(error_json, exchange="apierrors") + else: + _logger.error(f"api error: {str(error_json)}") except Exception as e: _logger.error(f"could not send api error to rabbitmq. Error: {e}") @@ -375,7 +379,10 @@ def _check_and_store_bulk_error(self, request, response): try: error_json = ApiErrorSerializerV2.request_to_json(self.request, response, other={"bulk_request": True}) response.data["error_identifier"] = error_json["identifier"] - rabbitmq.publish(error_json, exchange="apierrors") + if settings.ENABLE_API_ERROR_OBJECTS: + rabbitmq.publish(error_json, exchange="apierrors") + else: + _logger.error(f"api error: {str(error_json)}") except Exception as e: _logger.error(f"could not send api error to rabbitmq. Error: {e}") diff --git a/src/metax_api/onappstart.py b/src/metax_api/onappstart.py index 2a0346c6..a54b4a61 100755 --- a/src/metax_api/onappstart.py +++ b/src/metax_api/onappstart.py @@ -40,7 +40,8 @@ def ready(self): # pragma: no cover # because the "django apps" have not been loaded yet. import json - import metax_api.signals # noqa + if settings.ENABLE_SIGNALS: + import metax_api.signals # noqa from metax_api.services import RabbitMQService as rabbitmq from metax_api.services.redis_cache_service import RedisClient diff --git a/src/metax_api/settings/__init__.py b/src/metax_api/settings/__init__.py index c9950152..6dc65e87 100755 --- a/src/metax_api/settings/__init__.py +++ b/src/metax_api/settings/__init__.py @@ -28,7 +28,9 @@ ELASTIC_SEARCH_HOSTS=(list, ["localhost"]), ELASTIC_SEARCH_PORT=(int, 9200), ELASTIC_SEARCH_USE_SSL=(bool, False), + ENABLE_API_ERROR_OBJECTS=(bool, False), ENABLE_DELETED_OBJECTS_SAVING=(bool, False), + ENABLE_SIGNALS=(bool, False), ENABLE_V1_ENDPOINTS=(bool, True), ENABLE_V2_ENDPOINTS=(bool, True), ENABLE_DJANGO_WATCHMAN=(bool, False), diff --git a/src/metax_api/settings/components/common.py b/src/metax_api/settings/components/common.py index 972d5272..e422aeda 100755 --- a/src/metax_api/settings/components/common.py +++ b/src/metax_api/settings/components/common.py @@ -29,6 +29,8 @@ ERROR_FILES_PATH = env("ERROR_FILES_PATH") ENABLE_DELETED_OBJECTS_SAVING = env("ENABLE_DELETED_OBJECTS_SAVING") +ENABLE_SIGNALS = env("ENABLE_SIGNALS") +ENABLE_API_ERROR_OBJECTS = env("ENABLE_API_ERROR_OBJECTS") # Allow only specific hosts to access the app diff --git a/src/metax_api/signals/__init__.py b/src/metax_api/signals/__init__.py index 3c389606..f53b3d98 100644 --- a/src/metax_api/signals/__init__.py +++ b/src/metax_api/signals/__init__.py @@ -1,2 +1,5 @@ -from .post_delete import * -from .request_finished import * \ No newline at end of file +from django.conf import settings + +if settings.ENABLE_SIGNALS: + from .post_delete import * + from .request_finished import * \ No newline at end of file diff --git a/src/metax_api/tests/api/rest/v2/views/apierrors/read.py b/src/metax_api/tests/api/rest/v2/views/apierrors/read.py index ce8965e7..5d2d0d06 100755 --- a/src/metax_api/tests/api/rest/v2/views/apierrors/read.py +++ b/src/metax_api/tests/api/rest/v2/views/apierrors/read.py @@ -5,6 +5,7 @@ # :author: CSC - IT Center for Science Ltd., Espoo Finland # :license: MIT import logging +import unittest from unittest.mock import patch from uuid import uuid4 @@ -14,10 +15,12 @@ from metax_api.models import ApiError from metax_api.tests.utils import TestClassUtils, test_data_file_path, testcase_log_console +from django.conf import settings _logger = logging.getLogger(__name__) +@unittest.skipIf(settings.ENABLE_API_ERROR_OBJECTS is not True, "Only run if API errors are objects") class ApiErrorReadBasicTests(APITestCase, TestClassUtils): """ diff --git a/src/metax_api/tests/models/signals.py b/src/metax_api/tests/models/signals.py index 3e3b57e3..08cf9947 100644 --- a/src/metax_api/tests/models/signals.py +++ b/src/metax_api/tests/models/signals.py @@ -6,21 +6,23 @@ # :license: MIT from datetime import date - +import unittest from django.core.management import call_command from django.test import TestCase +from django.conf import settings -from metax_api.models import CatalogRecord, CatalogRecordV2, DeletedObject, Directory, File +from metax_api.models import CatalogRecord, CatalogRecordV2, DeletedObject from metax_api.tests.utils import TestClassUtils, test_data_file_path +@unittest.skipIf(settings.ENABLE_DELETED_OBJECTS_SAVING is not True, "Only run if deleted objects are saved") class SignalTests(TestCase, TestClassUtils): def setUp(self): call_command("loaddata", test_data_file_path, verbosity=0) self.today = date.today().strftime("%d/%m/%Y") -""" def test_deleting_catalog_record_creates_new_deleted_object(self): + def test_deleting_catalog_record_creates_new_deleted_object(self): # test that deleting CatalogRecord object creates a new deleted object CatalogRecord.objects_unfiltered.get(pk=1).delete(hard=True) deleted_object = DeletedObject.objects.last() @@ -31,6 +33,6 @@ def setUp(self): CatalogRecordV2.objects_unfiltered.get(pk=2).delete(hard=True) deleted_object_v2 = DeletedObject.objects.last() self.assertEqual(deleted_object_v2.model_name, "CatalogRecordV2") - self.assertEqual(deleted_object_v2.date_deleted.strftime("%d/%m/%Y"), self.today)""" + self.assertEqual(deleted_object_v2.date_deleted.strftime("%d/%m/%Y"), self.today)