Skip to content

Commit

Permalink
Implemented DJANGO_EASY_AUDIT_DEBUG_SIGNALS setting
Browse files Browse the repository at this point in the history
  • Loading branch information
dferens authored and jheld committed Apr 19, 2023
1 parent 8eaeb75 commit 792a8d0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ Below are some of the settings you may want to use. These should be defined in y
this is necessary in order to keep database atomicity concerns in check during signal handlers.

To clarify, this is only _truly_ necessary for the model signals.


* `DJANGO_EASY_AUDIT_DEBUG_SIGNALS`

Default is `False`. When set to `True` and Django's `DEBUG` is enabled, easyaudit will propagate exceptions occurred in own signal handlers. This is useful, unless you are using `DEBUG` in production.

* `DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIP`

By default this is `False`, but this allows the calling project not to save `CRUDEvent` if the changed fields as
Expand Down
3 changes: 3 additions & 0 deletions easyaudit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ 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
18 changes: 11 additions & 7 deletions easyaudit/signals/auth_signals.py
Original file line number Diff line number Diff line change
@@ -1,11 +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
DATABASE_ALIAS, DEBUG_SIGNALS

audit_logger = import_string(LOGGING_BACKEND)()

Expand All @@ -19,8 +20,9 @@ def user_logged_in(sender, request, user, **kwargs):
'user_id': getattr(user, 'id', None),
'remote_ip': request.META[REMOTE_ADDR_HEADER]
})
except:
pass
except Exception:
if settings.DEBUG and DEBUG_SIGNALS:
raise


def user_logged_out(sender, request, user, **kwargs):
Expand All @@ -32,8 +34,9 @@ def user_logged_out(sender, request, user, **kwargs):
'user_id': getattr(user, 'id', None),
'remote_ip': request.META[REMOTE_ADDR_HEADER]
})
except:
pass
except Exception:
if settings.DEBUG and DEBUG_SIGNALS:
raise


def user_login_failed(sender, credentials, **kwargs):
Expand All @@ -46,8 +49,9 @@ def user_login_failed(sender, credentials, **kwargs):
'username': credentials[user_model.USERNAME_FIELD],
'remote_ip': request.META[REMOTE_ADDR_HEADER]
})
except:
pass
except Exception:
if settings.DEBUG and DEBUG_SIGNALS:
raise


if WATCH_AUTH_EVENTS:
Expand Down
24 changes: 23 additions & 1 deletion easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from easyaudit.models import CRUDEvent
from easyaudit.settings import REGISTERED_CLASSES, UNREGISTERED_CLASSES, \
WATCH_MODEL_EVENTS, CRUD_DIFFERENCE_CALLBACKS, LOGGING_BACKEND, \
DATABASE_ALIAS
DATABASE_ALIAS, DEBUG_SIGNALS
from easyaudit.utils import get_m2m_field_name, model_delta

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -127,13 +127,19 @@ def crud_flow():
instance, instance.pk))
except Exception:
pass

raise e

if getattr(settings, "TEST", False):
crud_flow()
else:
transaction.on_commit(crud_flow, using=using)
except Exception:
logger.exception('easy audit had a pre-save exception.')

if settings.DEBUG and DEBUG_SIGNALS:
raise


def post_save(sender, instance, created, raw, using, update_fields, **kwargs):
"""https://docs.djangoproject.com/es/1.10/ref/signals/#post-save"""
Expand Down Expand Up @@ -186,13 +192,19 @@ def crud_flow():
instance, instance.pk))
except Exception:
pass

raise e

if getattr(settings, "TEST", False):
crud_flow()
else:
transaction.on_commit(crud_flow, using=using)
except Exception:
logger.exception('easy audit had a post-save exception.')

if settings.DEBUG and DEBUG_SIGNALS:
raise


def _m2m_rev_field_name(model1, model2):
"""Gets the name of the reverse m2m accessor from `model1` to `model2`
Expand Down Expand Up @@ -285,13 +297,18 @@ def crud_flow():
except Exception:
pass

raise e

if getattr(settings, "TEST", False):
crud_flow()
else:
transaction.on_commit(crud_flow, using=using)
except Exception:
logger.exception('easy audit had an m2m-changed exception.')

if settings.DEBUG and DEBUG_SIGNALS:
raise


def post_delete(sender, instance, using, **kwargs):
"""https://docs.djangoproject.com/es/1.10/ref/signals/#post-delete"""
Expand Down Expand Up @@ -333,13 +350,18 @@ def crud_flow():
except Exception:
pass

raise e

if getattr(settings, "TEST", False):
crud_flow()
else:
transaction.on_commit(crud_flow, using=using)
except Exception:
logger.exception('easy audit had a post-delete exception.')

if settings.DEBUG and DEBUG_SIGNALS:
raise


if WATCH_MODEL_EVENTS:
signals.post_save.connect(post_save, dispatch_uid='easy_audit_signals_post_save')
Expand Down

0 comments on commit 792a8d0

Please sign in to comment.