From 7af3dfb7ecdee0172299e82f763daebaab5a6f9e Mon Sep 17 00:00:00 2001 From: Dmytro Ferens Date: Mon, 20 Mar 2023 22:30:28 +0200 Subject: [PATCH] Refactoring: added `easyaudit.utils.should_propagate_exceptions` helper --- easyaudit/settings.py | 3 --- easyaudit/signals/auth_signals.py | 10 +++++----- easyaudit/signals/model_signals.py | 12 ++++++------ easyaudit/utils.py | 8 ++++++++ 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/easyaudit/settings.py b/easyaudit/settings.py index 7e78ae5..2d5a367 100644 --- a/easyaudit/settings.py +++ b/easyaudit/settings.py @@ -31,9 +31,6 @@ def get_model_list(class_list): WATCH_REQUEST_EVENTS = getattr(settings, 'DJANGO_EASY_AUDIT_WATCH_REQUEST_EVENTS', True) REMOTE_ADDR_HEADER = getattr(settings, 'DJANGO_EASY_AUDIT_REMOTE_ADDR_HEADER', 'REMOTE_ADDR') -# Should Django Easy Audit propagate signal handler exceptions when Django's `DEBUG` is enabled -DEBUG_SIGNALS = getattr(settings, 'DJANGO_EASY_AUDIT_DEBUG_SIGNALS', False) - USER_DB_CONSTRAINT = bool(getattr(settings, 'DJANGO_EASY_AUDIT_USER_DB_CONSTRAINT', True)) # logging backend settings diff --git a/easyaudit/signals/auth_signals.py b/easyaudit/signals/auth_signals.py index 1cf1e44..4c79c7c 100644 --- a/easyaudit/signals/auth_signals.py +++ b/easyaudit/signals/auth_signals.py @@ -1,4 +1,3 @@ -from django.conf import settings from django.contrib.auth import signals, get_user_model from django.db import transaction from django.utils.module_loading import import_string @@ -6,7 +5,8 @@ from easyaudit.middleware.easyaudit import get_current_request from easyaudit.models import LoginEvent from easyaudit.settings import REMOTE_ADDR_HEADER, WATCH_AUTH_EVENTS, LOGGING_BACKEND, \ - DATABASE_ALIAS, DEBUG_SIGNALS + DATABASE_ALIAS +from easyaudit.utils import should_propagate_exceptions audit_logger = import_string(LOGGING_BACKEND)() @@ -21,7 +21,7 @@ def user_logged_in(sender, request, user, **kwargs): 'remote_ip': request.META[REMOTE_ADDR_HEADER] }) except Exception: - if settings.DEBUG and DEBUG_SIGNALS: + if should_propagate_exceptions(): raise @@ -35,7 +35,7 @@ def user_logged_out(sender, request, user, **kwargs): 'remote_ip': request.META[REMOTE_ADDR_HEADER] }) except Exception: - if settings.DEBUG and DEBUG_SIGNALS: + if should_propagate_exceptions(): raise @@ -50,7 +50,7 @@ def user_login_failed(sender, credentials, **kwargs): 'remote_ip': request.META[REMOTE_ADDR_HEADER] }) except Exception: - if settings.DEBUG and DEBUG_SIGNALS: + if should_propagate_exceptions(): raise diff --git a/easyaudit/signals/model_signals.py b/easyaudit/signals/model_signals.py index 082c4df..93e4a1a 100644 --- a/easyaudit/signals/model_signals.py +++ b/easyaudit/signals/model_signals.py @@ -18,8 +18,8 @@ from easyaudit.models import CRUDEvent from easyaudit.settings import REGISTERED_CLASSES, UNREGISTERED_CLASSES, \ WATCH_MODEL_EVENTS, CRUD_DIFFERENCE_CALLBACKS, LOGGING_BACKEND, \ - DATABASE_ALIAS, DEBUG_SIGNALS -from easyaudit.utils import get_m2m_field_name, model_delta + DATABASE_ALIAS +from easyaudit.utils import get_m2m_field_name, model_delta, should_propagate_exceptions logger = logging.getLogger(__name__) audit_logger = import_string(LOGGING_BACKEND)() @@ -137,7 +137,7 @@ def crud_flow(): except Exception: logger.exception('easy audit had a pre-save exception.') - if settings.DEBUG and DEBUG_SIGNALS: + if should_propagate_exceptions(): raise @@ -202,7 +202,7 @@ def crud_flow(): except Exception: logger.exception('easy audit had a post-save exception.') - if settings.DEBUG and DEBUG_SIGNALS: + if should_propagate_exceptions(): raise @@ -306,7 +306,7 @@ def crud_flow(): except Exception: logger.exception('easy audit had an m2m-changed exception.') - if settings.DEBUG and DEBUG_SIGNALS: + if should_propagate_exceptions(): raise @@ -359,7 +359,7 @@ def crud_flow(): except Exception: logger.exception('easy audit had a post-delete exception.') - if settings.DEBUG and DEBUG_SIGNALS: + if should_propagate_exceptions(): raise diff --git a/easyaudit/utils.py b/easyaudit/utils.py index 5f9e5fa..a43d14c 100644 --- a/easyaudit/utils.py +++ b/easyaudit/utils.py @@ -79,3 +79,11 @@ def get_m2m_field_name(model, instance): for x in model._meta.related_objects: if x.related_model().__class__ == instance.__class__: return x.remote_field.name + + +def should_propagate_exceptions(): + """ + Should Django Easy Audit propagate signal handler exceptions. + :rtype: bool + """ + return settings.DEBUG and getattr(settings, 'DJANGO_EASY_AUDIT_DEBUG_SIGNALS')