Merge pull request #1 from francofuji/master

Merge pull request #1 from francofuji/master
- Make AUTH_USER_MODEL work with 'app_label.model_name' format
- Some util functions
- Log an entry only if there are changes
This commit is contained in:
Jan-Jelle Kester 2013-12-11 08:03:28 -08:00
commit dac120b51a
3 changed files with 29 additions and 6 deletions

View file

@ -1,6 +1,8 @@
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
@ -24,5 +26,10 @@ class AuditLogMiddleware(object):
return response
def set_actor(self, user, sender, instance, **kwargs):
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

View file

@ -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):
"""

View file

@ -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):