Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions easyaudit/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@
class ModelBackend:

def request(self, request_info):
return RequestEvent.objects.create(**request_info)
try:
return RequestEvent.objects.create(**request_info)
except Exception as e:
logger.error(f"Error creating RequestEvent: {e}")
return None

def crud(self, crud_info):
return CRUDEvent.objects.create(**crud_info)
try:
return CRUDEvent.objects.create(**crud_info)
except Exception as e:
logger.error(f"Error creating CRUDEvent: {e}")
return None

def login(self, login_info):
return LoginEvent.objects.create(**login_info)
try:
return LoginEvent.objects.create(**login_info)
except Exception as e:
logger.error(f"Error creating LoginEvent: {e}")
return None
43 changes: 27 additions & 16 deletions easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging

from django.db.utils import OperationalError
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
Expand All @@ -18,7 +19,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, CRUD_EVENT_NO_CHANGED_FIELDS_SKIP
from easyaudit.utils import get_m2m_field_name, model_delta

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -73,6 +74,9 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):
try:
if not should_audit(instance):
return False

if CRUD_EVENT_NO_CHANGED_FIELDS_SKIP:
return

with transaction.atomic(using=using):
try:
Expand Down Expand Up @@ -122,17 +126,17 @@ def crud_flow():
})
except Exception as e:
try:
logger.exception(
logger.error(
"easy audit had a pre_save exception on CRUDEvent creation. instance: {}, instance pk: {}".format(
instance, instance.pk))
instance, instance.pk), exc_info=True)
except Exception:
pass
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.')
logger.error('easy audit had a pre-save exception.', exc_info=True)


def post_save(sender, instance, created, raw, using, update_fields, **kwargs):
Expand Down Expand Up @@ -181,17 +185,20 @@ def crud_flow():
})
except Exception as e:
try:
logger.exception(
logger.error(
"easy audit had a post_save exception on CRUDEvent creation. instance: {}, instance pk: {}".format(
instance, instance.pk))
instance, instance.pk), exc_info=True)
except Exception:
pass
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.')

except OperationalError as ex:
logger.error(f'Easy audit had a post-save OperationalError. {ex}', exc_info=True)
except Exception as ex:
logger.error(f'Easy audit had a post-save exception. {ex}', exc_info=True)


def _m2m_rev_field_name(model1, model2):
Expand Down Expand Up @@ -279,18 +286,20 @@ def crud_flow():
})
except Exception as e:
try:
logger.exception(
logger.error(
"easy audit had a m2m_changed exception on CRUDEvent creation. instance: {}, instance pk: {}".format(
instance, instance.pk))
instance, instance.pk), exc_info=True)
except Exception:
pass

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.')
except OperationalError as ex:
logger.error(f'Easy audit had a m2m-changed OperationalError. {ex}', exc_info=True)
except Exception as ex:
logger.error(f'Easy audit had a m2m-changed exception. {ex}', exc_info=True)


def post_delete(sender, instance, using, **kwargs):
Expand Down Expand Up @@ -327,18 +336,20 @@ def crud_flow():

except Exception as e:
try:
logger.exception(
logger.error(
"easy audit had a post_delete exception on CRUDEvent creation. instance: {}, instance pk: {}".format(
instance, instance.pk))
instance, instance.pk), exc_info=True)
except Exception:
pass

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.')
except OperationalError as ex:
logger.error(f'Easy audit had a post-delete OperationalError. {ex}', exc_info=True)
except Exception as ex:
logger.error(f'Easy audit had a post-delete exception. {ex}', exc_info=True)


if WATCH_MODEL_EVENTS:
Expand Down