Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom object json serializer #93

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
added support for custom json object serializer
jdkizer9 committed Jun 5, 2019
commit 6f1bb68762a29c6346a450f5bb1d9cfe6737dc9c
3 changes: 3 additions & 0 deletions easyaudit/settings.py
Original file line number Diff line number Diff line change
@@ -103,3 +103,6 @@ def get_model_list(class_list):
CRUD_EVENT_LIST_FILTER = getattr(settings, 'DJANGO_EASY_AUDIT_CRUD_EVENT_LIST_FILTER', ['event_type', 'content_type', 'user', 'datetime', ])
LOGIN_EVENT_LIST_FILTER = getattr(settings, 'DJANGO_EASY_AUDIT_LOGIN_EVENT_LIST_FILTER', ['login_type', 'user', 'datetime', ])
REQUEST_EVENT_LIST_FILTER = getattr(settings, 'DJANGO_EASY_AUDIT_REQUEST_EVENT_LIST_FILTER', ['method', 'user', 'datetime', ])

# JSON object representation serializer override
CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE = getattr(settings, 'DJANGO_EASY_AUDIT_CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE', None)
14 changes: 11 additions & 3 deletions easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,8 @@
get_current_user
from easyaudit.models import CRUDEvent
from easyaudit.settings import REGISTERED_CLASSES, UNREGISTERED_CLASSES, \
WATCH_MODEL_EVENTS, CRUD_DIFFERENCE_CALLBACKS
WATCH_MODEL_EVENTS, CRUD_DIFFERENCE_CALLBACKS,\
CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE
from easyaudit.utils import model_delta

logger = logging.getLogger(__name__)
@@ -53,7 +54,10 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):
if not should_audit(instance):
return False
try:
object_json_repr = serializers.serialize("json", [instance])
if CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE != None:
object_json_repr = CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE(instance)
else:
object_json_repr = serializers.serialize("json", [instance])
except Exception:
# We need a better way for this to work. ManyToMany will fail on pre_save on create
return None
@@ -122,7 +126,11 @@ def post_save(sender, instance, created, raw, using, update_fields, **kwargs):
with transaction.atomic():
if not should_audit(instance):
return False
object_json_repr = serializers.serialize("json", [instance])

if CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE != None:
object_json_repr = CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE(instance)
else:
object_json_repr = serializers.serialize("json", [instance])

# created or updated?
if created: