Skip to content

Commit

Permalink
Ability to disable tracking for an instance; Python 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
vvangelovski committed Jan 28, 2015
1 parent b01dfe0 commit e2c7588
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 45 deletions.
15 changes: 8 additions & 7 deletions README.rst
Expand Up @@ -9,10 +9,11 @@ Tracking changes to django models.
* Abstract model class with fields ``created_by`` and ``modified_by`` fields.
* A model manager class that can automatically track changes made to a model in the database.
* Support for Django 1.6 and 1.7, South migrations, Django 1.7 migrations and custom User classes.
* Python 3 and 2.x support

`The documentation can be found here <http://django-audit-log.readthedocs.org/en/latest/index.html>`_
`The documentation can be found here <http://django-audit-log.readthedocs.org/en/latest/index.html>`_

**Tracking full model history on M2M relations Is not supported yet.**
**Tracking full model history on M2M relations is not supported yet.**
**Version 0.3.0 onwards is tested with Django 1.6. It should work with older versions of Django, but may break things unexpectedly!**


Expand Down Expand Up @@ -60,12 +61,12 @@ If you want to track full model change history you need to attach an ``AuditLog`
from audit_log.models.fields import LastUserField
from audit_log.models.managers import AuditLog


class ProductCategory(models.Model):
name = models.CharField(max_length=150, primary_key = True)
description = models.TextField()
audit_log = AuditLog()

audit_log = AuditLog()

class Product(models.Model):
name = models.CharField(max_length = 150)
Expand All @@ -82,10 +83,10 @@ You can then query the audit log::
<ProductAuditLogEntry: Product: My widget changed at 2011-02-25 06:04:24.898991>,
<ProductAuditLogEntry: Product: My Gadget super changed at 2011-02-25 06:04:15.448934>,
<ProductAuditLogEntry: Product: My Gadget changed at 2011-02-25 06:04:06.566589>,
<ProductAuditLogEntry: Product: My Gadget created at 2011-02-25 06:03:57.751222>,
<ProductAuditLogEntry: Product: My Gadget created at 2011-02-25 06:03:57.751222>,
<ProductAuditLogEntry: Product: My widget created at 2011-02-25 06:03:42.027220>]

`The documentation can be found here <http://django-audit-log.readthedocs.org/en/latest/index.html>`_
`The documentation can be found here <http://django-audit-log.readthedocs.org/en/latest/index.html>`_


*Note: This project was not maintained actively for a while. One of the reasons was that I wasn't receiving email notifications from GitHub. The other reason: We were using it just on a couple of projects that were frozen to old versions of Django. If you need any help with the project you can contact me by email directly if I don't respond to your GitHub issues. Feel free to nudge me over email if you have a patch for something. You can find my email in the AUTHORS file.*
2 changes: 1 addition & 1 deletion audit_log/__init__.py
@@ -1,4 +1,4 @@
VERSION = (0, 6, 0, 'final')
VERSION = (0, 7, 0, 'final')

if VERSION[-1] != "final": # pragma: no cover
__version__ = '.'.join(map(str, VERSION))
Expand Down
27 changes: 23 additions & 4 deletions audit_log/middleware.py
Expand Up @@ -3,6 +3,24 @@

from audit_log import registration
from audit_log.models import fields
from audit_log.models.managers import AuditLogManager

def _disable_audit_log_managers(instance):
for attr in dir(instance):
try:
if isinstance(getattr(instance, attr), AuditLogManager):
getattr(instance, attr).disable_tracking()
except AttributeError:
pass


def _enable_audit_log_managers(instance):
for attr in dir(instance):
try:
if isinstance(getattr(instance, attr), AuditLogManager):
getattr(instance, attr).enable_tracking()
except AttributeError:
pass

class UserLoggingMiddleware(object):
def process_request(self, request):
Expand Down Expand Up @@ -44,14 +62,15 @@ def _update_post_save_info(self, user, session, sender, instance, created, **kwa
if sender in registry:
for field in registry.get_fields(sender):
setattr(instance, field.name, user)
setattr(instance, "_audit_log_ignore_update", True)
_disable_audit_log_managers(instance)
instance.save()
instance._audit_log_ignore_update = False
_enable_audit_log_managers(instance)


registry = registration.FieldRegistry(fields.CreatingSessionKeyField)
if sender in registry:
for field in registry.get_fields(sender):
setattr(instance, field.name, session)
setattr(instance, "_audit_log_ignore_update", True)
_disable_audit_log_managers(instance)
instance.save()
instance._audit_log_ignore_update = False
_enable_audit_log_managers(instance)
50 changes: 39 additions & 11 deletions audit_log/models/managers.py
Expand Up @@ -22,6 +22,7 @@ class LogEntryObjectDescriptor(object):
def __init__(self, model):
self.model = model


def __get__(self, instance, owner):
kwargs = dict((f.attname, getattr(instance, f.attname))
for f in self.model._meta.fields
Expand All @@ -30,10 +31,34 @@ def __get__(self, instance, owner):


class AuditLogManager(models.Manager):
def __init__(self, model, instance = None):
def __init__(self, model, attname, instance = None, ):
super(AuditLogManager, self).__init__()
self.model = model
self.instance = instance
self.attname = attname
#set a hidden attribute on the instance to control wether we should track changes
if instance is not None and not hasattr(instance, '__is_%s_enabled'%attname):
setattr(instance, '__is_%s_enabled'%attname, True)



def enable_tracking(self):
if self.instance is None:
raise ValueError("Tracking can only be enabled or disabled "
"per model instance, not on a model class")
setattr(self.instance, '__is_%s_enabled'%self.attname, True)

def disable_tracking(self):
if self.instance is None:
raise ValueError("Tracking can only be enabled or disabled "
"per model instance, not on a model class")
setattr(self.instance, '__is_%s_enabled'%self.attname, False)

def is_tracking_enabled(self):
if self.instance is None:
raise ValueError("Tracking can only be enabled or disabled "
"per model instance, not on a model class")
return getattr(self.instance, '__is_%s_enabled'%self.attname)

def get_queryset(self):
if self.instance is None:
Expand All @@ -44,14 +69,16 @@ def get_queryset(self):


class AuditLogDescriptor(object):
def __init__(self, model, manager_class):
def __init__(self, model, manager_class, attname):
self.model = model
self._manager_class = manager_class
self.manager_class = manager_class
self.attname = attname

def __get__(self, instance, owner):
if instance is None:
return self._manager_class(self.model)
return self._manager_class(self.model, instance)
return self.manager_class(self.model, self.attname)
return self.manager_class(self.model, self.attname, instance)


class AuditLog(object):

Expand All @@ -60,6 +87,7 @@ class AuditLog(object):
def __init__(self, exclude = []):
self._exclude = exclude


def contribute_to_class(self, cls, name):
self.manager_name = name
models.signals.class_prepared.connect(self.finalize, sender = cls)
Expand All @@ -74,15 +102,15 @@ def create_log_entry(self, instance, action_type):
manager.create(action_type = action_type, **attrs)

def post_save(self, instance, created, **kwargs):
#_audit_log_ignore_update gets attached right before a save on an instance
#gets performed in the middleware
#TODO I don't like how this is done
if not getattr(instance, "_audit_log_ignore_update", False) or created:
#ignore if it is disabled
if getattr(instance, self.manager_name).is_tracking_enabled():
self.create_log_entry(instance, created and 'I' or 'U')


def post_delete(self, instance, **kwargs):
self.create_log_entry(instance, 'D')
#ignore if it is disabled
if getattr(instance, self.manager_name).is_tracking_enabled():
self.create_log_entry(instance, 'D')


def finalize(self, sender, **kwargs):
Expand All @@ -91,7 +119,7 @@ def finalize(self, sender, **kwargs):
models.signals.post_save.connect(self.post_save, sender = sender, weak = False)
models.signals.post_delete.connect(self.post_delete, sender = sender, weak = False)

descriptor = AuditLogDescriptor(log_entry_model, self.manager_class)
descriptor = AuditLogDescriptor(log_entry_model, self.manager_class, self.manager_name)
setattr(sender, self.manager_name, descriptor)

def copy_fields(self, model):
Expand Down
41 changes: 41 additions & 0 deletions audit_log/tests/audit_log_tests/test_manager.py
@@ -0,0 +1,41 @@
from django.test import TestCase
from django.db import models
from .models import (Product, WarehouseEntry, ProductCategory, ExtremeWidget,
SaleInvoice, Employee, ProductRating, Property, PropertyOwner)


class DisablingTrackingTest(TestCase):


def test_disable_enable_instance(self):
ProductCategory.objects.create(name = 'test category', description = 'test')
ProductCategory.objects.create(name = 'test category2', description = 'test')
c1 = ProductCategory.objects.get(name = 'test category')
c2 = ProductCategory.objects.get(name = 'test category2')
self.assertTrue(c1.audit_log.is_tracking_enabled())
c1.audit_log.disable_tracking()
self.assertFalse(c1.audit_log.is_tracking_enabled())
self.assertTrue(c2.audit_log.is_tracking_enabled())


def test_disable_enable_class(self):
self.assertRaises(ValueError, ProductCategory.audit_log.disable_tracking)
self.assertRaises(ValueError, ProductCategory.audit_log.enable_tracking)
self.assertRaises(ValueError, ProductCategory.audit_log.is_tracking_enabled)

def test_disabled_not_tracking(self):
ProductCategory(name = 'test category', description = 'test').save()
ProductCategory(name = 'test category2', description = 'test').save()
c1 = ProductCategory.objects.get(name = 'test category')
c2 = ProductCategory.objects.get(name = 'test category2')
c1.description = 'best'
c1.audit_log.disable_tracking()
c1.save()
self.assertEquals(c1.audit_log.all().count(), 1)
c1.audit_log.enable_tracking()
c1.description = 'new desc'
c1.save()
self.assertEquals(c1.audit_log.all().count(), 2)
c1.audit_log.disable_tracking()
c1.delete()
self.assertEquals(ProductCategory.audit_log.all().count(), 3)
12 changes: 5 additions & 7 deletions docs/change_tracking.rst
Expand Up @@ -29,14 +29,14 @@ The related names for the ``created_by`` and ``modified_by`` fields are ``create
Out[6]: [<WarehouseEntry: WarehouseEntry object>]

This was done to keep in line with Django's naming for the ``related_name``. If you want to change that or other things you can
create your own abstract base class with the proviced fields.
create your own abstract base class with the proviced fields.

This is very useful when used in conjuction with ``TimeStampedModel`` from ``django-extensions``::

from django_extensions.db.models import TimeStampedModel
from audit_log.models import AuthStampedModel


class Invoice(TimeStampedModel, AuthStampedModel):
group = models.ForeignKey(InvoiceGroup, verbose_name = _("group"))
client = models.ForeignKey(ClientContact, verbose_name = _("client"))
Expand Down Expand Up @@ -70,13 +70,13 @@ Tracking Who Made the Last Changes to a Model

from django.db import models
from audit_log.models.fields import LastUserField, LastSessionKeyField

class Product(models.Model):
name = models.CharField(max_length = 150)
description = models.TextField()
price = models.DecimalField(max_digits = 10, decimal_places = 2)
category = models.ForeignKey(ProductCategory)

def __unicode__(self):
return self.name

Expand All @@ -87,7 +87,5 @@ Tracking Who Made the Last Changes to a Model
rating = models.PositiveIntegerField()

Anytime someone makes changes to the ``ProductRating`` model through the web interface
the reference to the user that made the change will be stored in the user field and
the reference to the user that made the change will be stored in the user field and
the session key will be stored in the session field.


41 changes: 27 additions & 14 deletions docs/model_history.rst
Expand Up @@ -2,20 +2,20 @@
Tracking full model history
===============================

In order to enable historic tracking on a model, the model needs to have a
In order to enable historic tracking on a model, the model needs to have a
property of type ``audit_log.models.managers.AuditLog`` attached::


from django.db import models
from audit_log.models.fields import LastUserField
from audit_log.models.managers import AuditLog


class ProductCategory(models.Model):
name = models.CharField(max_length=150, primary_key = True)
description = models.TextField()
audit_log = AuditLog()

audit_log = AuditLog()

class Product(models.Model):
name = models.CharField(max_length = 150)
Expand All @@ -26,27 +26,27 @@ property of type ``audit_log.models.managers.AuditLog`` attached::
audit_log = AuditLog()


Each time you add an instance of AuditLog to any of your models you need to run
``python manage.py syncdb`` so that the database table that keeps the actual
audit log for the given model gets created.
Each time you add an instance of AuditLog to any of your models you need to run
``python manage.py syncdb`` so that the database table that keeps the actual
audit log for the given model gets created.


Querying the audit log
-------------------------------

An instance of ``audit_log.models.managers.AuditLog`` will behave much like a
standard manager in your model. Assuming the above model
configuration you can go ahead and create/edit/delete instances of Product,
An instance of ``audit_log.models.managers.AuditLog`` will behave much like a
standard manager in your model. Assuming the above model
configuration you can go ahead and create/edit/delete instances of Product,
to query all the changes that were made to the products table
you would need to retrieve all the entries for the audit log for that
you would need to retrieve all the entries for the audit log for that
particular model class::

In [2]: Product.audit_log.all()
Out[2]: [<ProductAuditLogEntry: Product: My widget changed at 2011-02-25 06:04:29.292363>,
<ProductAuditLogEntry: Product: My widget changed at 2011-02-25 06:04:24.898991>,
<ProductAuditLogEntry: Product: My Gadget super changed at 2011-02-25 06:04:15.448934>,
<ProductAuditLogEntry: Product: My Gadget changed at 2011-02-25 06:04:06.566589>,
<ProductAuditLogEntry: Product: My Gadget created at 2011-02-25 06:03:57.751222>,
<ProductAuditLogEntry: Product: My Gadget created at 2011-02-25 06:03:57.751222>,
<ProductAuditLogEntry: Product: My widget created at 2011-02-25 06:03:42.027220>]

Accordingly you can get the changes made to a particular model instance like so::
Expand All @@ -58,7 +58,7 @@ Accordingly you can get the changes made to a particular model instance like so:

Instances of ``AuditLog`` behave like django model managers and can be queried in the same fashion.

The querysets yielded by ``AuditLog`` managers are querysets for models
The querysets yielded by ``AuditLog`` managers are querysets for models
of type ``[X]AuditLogEntry``, where X is the tracked model class.
An instance of ``XAuditLogEntry`` represents a log entry for a particular model
instance and will have the following fields that are of relevance:
Expand All @@ -80,4 +80,17 @@ Abstract Base Models
--------------------------

For now just attaching the ``AuditLog`` manager to an abstract base model won't make it automagically attach itself on the child
models. Just attach it to every child separately.
models. Just attach it to every child separately.

Disabling/Enabling Tracking on a Model Instance
-------------------------------------------------
There may be times when you want a certain ``save()`` or ``delete()`` on a model instance to be ignored by the audit log.
To disable tracking on a model instance you simply call::

modelinstance.audit_log.disable_tracking()

To re-enable it do::

modelinstance.audit_log.enable_tracking()

Note that this only works on instances, trying to do that on a model class will raise an exception.
4 changes: 4 additions & 0 deletions setup.cfg
@@ -1,2 +1,6 @@
[wheel]
universal = 1

[bdist_wheel]
universal = 1

9 changes: 8 additions & 1 deletion setup.py
Expand Up @@ -30,12 +30,19 @@ def get_readme():
download_url = 'https://github.com/Atomidata/django-audit-log/downloads',
include_package_data = True,
zip_safe = False,

classifiers = STATUS + [
'Environment :: Plugins',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Topic :: Software Development :: Libraries :: Python Modules',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
],
)

0 comments on commit e2c7588

Please sign in to comment.