Skip to content

Commit

Permalink
Refactoring: added easyaudit.utils.should_propagate_exceptions helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dferens authored and jheld committed Apr 19, 2023
1 parent 792a8d0 commit 7af3dfb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
3 changes: 0 additions & 3 deletions easyaudit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions easyaudit/signals/auth_signals.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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

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)()

Expand All @@ -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


Expand All @@ -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


Expand All @@ -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


Expand Down
12 changes: 6 additions & 6 deletions easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)()
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down
8 changes: 8 additions & 0 deletions easyaudit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 7af3dfb

Please sign in to comment.