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
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
4 changes: 4 additions & 0 deletions easyaudit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ def get_model_list(class_list):
CRUD_EVENT_SEARCH_FIELDS = getattr(settings, 'DJANGO_EASY_AUDIT_CRUD_EVENT_SEARCH_FIELDS', ['=object_id', 'object_json_repr', ])
LOGIN_EVENT_SEARCH_FIELDS = getattr(settings, 'DJANGO_EASY_AUDIT_LOGIN_EVENT_SEARCH_FIELDS', ['=remote_ip', 'username', ])
REQUEST_EVENT_SEARCH_FIELDS = getattr(settings, 'DJANGO_EASY_AUDIT_REQUEST_EVENT_SEARCH_FIELDS', ['=remote_ip', 'user__username', 'url', 'query_string', ])

# JSON object representation serializer override
CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE = getattr(settings, 'DJANGO_EASY_AUDIT_CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE', None)
CRUD_OBJECT_MODEL_DELTA_CALLBACK = getattr(settings, 'DJANGO_EASY_AUDIT_CRUD_OBJECT_MODEL_DELTA_CALLBACK', None)
16 changes: 13 additions & 3 deletions easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, CRUD_OBJECT_MODEL_DELTA_CALLBACK
from easyaudit.utils import model_delta

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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
Expand All @@ -67,6 +71,8 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):
if not created:
old_model = sender.objects.get(pk=instance.pk)
delta = model_delta(old_model, instance)
if CRUD_OBJECT_MODEL_DELTA_CALLBACK != None:
delta = CRUD_OBJECT_MODEL_DELTA_CALLBACK(old_model, instance, delta)
changed_fields = json.dumps(delta)
event_type = CRUDEvent.UPDATE

Expand Down Expand Up @@ -122,7 +128,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:
Expand Down