mirror of
https://github.com/jazzband/django-auditlog.git
synced 2026-05-25 07:13:54 +00:00
Our dependency jsonfield broke compatibility with python2.7 recently, and having no real reasons to support python2.7 we just drop it now.
61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
import json
|
|
|
|
from auditlog.diff import model_instance_diff
|
|
from auditlog.models import LogEntry
|
|
|
|
|
|
def log_create(sender, instance, created, **kwargs):
|
|
"""
|
|
Signal receiver that creates a log entry when a model instance is first saved to the database.
|
|
|
|
Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
|
|
"""
|
|
if created:
|
|
changes = model_instance_diff(None, instance)
|
|
|
|
log_entry = LogEntry.objects.log_create(
|
|
instance,
|
|
action=LogEntry.Action.CREATE,
|
|
changes=json.dumps(changes),
|
|
)
|
|
|
|
|
|
def log_update(sender, instance, **kwargs):
|
|
"""
|
|
Signal receiver that creates a log entry when a model instance is changed and saved to the database.
|
|
|
|
Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
|
|
"""
|
|
if instance.pk is not None:
|
|
try:
|
|
old = sender.objects.get(pk=instance.pk)
|
|
except sender.DoesNotExist:
|
|
pass
|
|
else:
|
|
new = instance
|
|
|
|
changes = model_instance_diff(old, new)
|
|
|
|
# Log an entry only if there are changes
|
|
if changes:
|
|
log_entry = LogEntry.objects.log_create(
|
|
instance,
|
|
action=LogEntry.Action.UPDATE,
|
|
changes=json.dumps(changes),
|
|
)
|
|
|
|
|
|
def log_delete(sender, instance, **kwargs):
|
|
"""
|
|
Signal receiver that creates a log entry when a model instance is deleted from the database.
|
|
|
|
Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
|
|
"""
|
|
if instance.pk is not None:
|
|
changes = model_instance_diff(instance, None)
|
|
|
|
log_entry = LogEntry.objects.log_create(
|
|
instance,
|
|
action=LogEntry.Action.DELETE,
|
|
changes=json.dumps(changes),
|
|
)
|