2007/4/20, Jason McVetta <[EMAIL PROTECTED]>:
> I need to add real audit trail and change-notification support to an
> existing Django app and to one that is under development.  The best way to
> do this, it seems to me, is to add audit support to the Django framework
> itself.  My requirements are similar to those named by Paul Childs in his
> django-users post last June:
>

Here is what I have quickly done with signals (I don't know if it can
be helpful):

## signals.py

from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
from django.contrib.contenttypes.models import ContentType

""" Those functions allow application logging.

It's a bit ugly to declare instance hidden variables (pre) but we need to be
sure that the save/delete is effective (post) before logging it.

PROTO: For the moment, it used the admin LogEntry model which is not really
safe given the fact that django admin will be changed soon to include newforms.

TODO: Add diff in change_message argument of LogEntry when this is a CHANGE.
"""

def log_pre_save(sender, instance, signal):
    try:
        initial_object = sender.objects.get(id=instance.id)
        instance._action_flag = CHANGE
    except sender.DoesNotExist:
        instance._action_flag = ADDITION

def log_post_save(sender, instance, signal):
    LogEntry.objects.log_action(
        user_id=instance.user.id,
        content_type_id=ContentType.objects.get_for_model(sender).id,
        object_id=instance.id,
        object_repr=str(sender.objects.get(id=instance.id)),
        action_flag=instance._action_flag)

def log_pre_delete(sender, instance, signal):
    instance._object_repr = str(sender.objects.get(id=instance.id))

def log_post_delete(sender, instance, signal):
    LogEntry.objects.log_action(
        user_id=instance.user.id,
        content_type_id=ContentType.objects.get_for_model(sender).id,
        object_id=instance.id,
        object_repr=instance._object_repr,
        action_flag=DELETION)

## todo/models.py
from sibiocle.misc.signals import log_pre_save, log_post_save,
log_pre_delete, log_post_delete

[...]

dispatcher.connect(log_pre_save,    signal=signals.pre_save,    sender=Todo)
dispatcher.connect(log_post_save,   signal=signals.post_save,   sender=Todo)
dispatcher.connect(log_pre_delete,  signal=signals.pre_delete,  sender=Todo)
dispatcher.connect(log_post_delete, signal=signals.post_delete, sender=Todo)

Anyway, I'm really intersted in your work, let us know your progression ;-).

Regards,
David

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to