mirror of
https://github.com/jazzband/django-auditlog.git
synced 2026-03-16 22:20:26 +00:00
Merge remote-tracking branch 'origin/master'
Conflicts: src/auditlog/middleware.py
This commit is contained in:
commit
ec051d11f0
3 changed files with 28 additions and 6 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from django.conf import settings
|
||||
from django.db.models.signals import pre_save
|
||||
from django.utils.functional import curry
|
||||
from django.db.models.loading import get_model
|
||||
from auditlog.models import LogEntry
|
||||
|
||||
|
||||
|
|
@ -37,5 +38,10 @@ class AuditlogMiddleware(object):
|
|||
Signal receiver with an extra, required 'user' kwarg. This method becomes a real (valid) signal receiver when
|
||||
it is curried with the actor.
|
||||
"""
|
||||
if sender == LogEntry and isinstance(user, settings.AUTH_USER_MODEL) and instance.actor is None:
|
||||
try:
|
||||
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
|
||||
auth_user_model = get_model(app_label, model_name)
|
||||
except:
|
||||
auth_user_model = get_model('auth', 'user')
|
||||
if sender == LogEntry and isinstance(user, auth_user_model) and instance.actor is None:
|
||||
instance.actor = user
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import json
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes import generic
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
|
@ -40,6 +42,11 @@ class LogEntryManager(models.Manager):
|
|||
|
||||
return self.create(**kwargs)
|
||||
return None
|
||||
|
||||
def get_entries_from_model(self, model):
|
||||
content_type = ContentType.objects.get_for_model(model)
|
||||
object_id = model.pk
|
||||
return self.get_query_set().filter(content_type=content_type, object_id=object_id)
|
||||
|
||||
|
||||
class LogEntry(models.Model):
|
||||
|
|
@ -93,6 +100,13 @@ class LogEntry(models.Model):
|
|||
|
||||
return fstring.format(repr=self.object_repr)
|
||||
|
||||
def html_formated_changes(self):
|
||||
changes_result = []
|
||||
changes_dict = json.loads(self.changes.encode('utf-8'))
|
||||
for field, changes in changes_dict.items():
|
||||
changes_result.append('<strong>%s</strong> from %s <strong>to</strong> %s' % (field, changes[0], changes[1]))
|
||||
return changes_result
|
||||
|
||||
|
||||
class AuditlogHistoryField(generic.GenericRelation):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -35,11 +35,13 @@ def log_update(sender, instance, **kwargs):
|
|||
|
||||
changes = model_instance_diff(old, new)
|
||||
|
||||
log_entry = LogEntry.objects.log_create(
|
||||
instance,
|
||||
action=LogEntry.Action.UPDATE,
|
||||
changes=json.dumps(changes),
|
||||
)
|
||||
# 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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue