sitech-django-audit_log
is a Django application and library for creates an audit history for each model operations (create, update and delete) into database, file, and many other stores automatically.
To use this library, you'll need to have the following packages installed:
- TrackingFieldsMixin
from sitech-django-models and it is a Python package that allow you to access the old values of the model fields.
You can run the pip command to install the latest version:
pip install git+https://github.com/sitmena/[email protected]
- RequestMiddleware
from sitech-django-middlewares and it is a Python package that allow you to access the Request or User Object Inside the Models, Forms, Signals, ... etc.
You can do the following to install RequestMiddleware
:
- Run the pip command to install the latest version:
pip install git+https://github.com/sitmena/[email protected]
- Add
sitech_middlewares.request.RequestMiddleware
toMIDDLEWARE
in settings.py:
MIDDLEWARE = (
...
'sitech_middlewares.request.RequestMiddleware',
)
- Run the pip command to install the latest version:
pip install git+https://github.com/sitmena/[email protected]
- Add
sitech_audit_log
to yourINSTALLED_APPS
in settings.py:
INSTALLED_APPS = (
...
'sitech_audit_log',
)
- Add the follwoing line in settings.py:
JSONFIELD_ENCODER_CLASS = 'django.core.serializers.json.DjangoJSONEncoder'
- Run the migration command:
python manage.py migrate
A neat feature of this package is that it can automatically log operations such as when a model is created, updated and deleted. To make this work all you need to add AuditLogMixin
and TrackingFieldsMixin
to your model.
Here's an example:
from django.db import models
from sitech_models import TrackingFieldsMixin
from sitech_audit_log import AuditLogMixin
class Profile(models.Model, AuditLogMixin, TrackingFieldsMixin):
phone = models.CharField(max_length=255, verbose_name='Phone')
address = models.TextField(max_length=512,verbose_name='Address')
Sitech Django Audit Log with several log storages backends. With the exception of the 'DatabaseBackend' (which is the default). If you have special log storage requirements, you can write your own log storage backend.
To specify your Log Storages backend, put the following in your settings:
AUDIT_LOG_STORAGES = [
'sitech_audit_log.backends.database.DatabaseBackend'
]
The AUDIT_LOG_STORAGES
should be a list of Python path names that point to Log Storages classes.
By default the package will log the created
, updated
, deleted
operations. You can modify this behaviour by setting the log_operation
property on your model.
from django.db import models
from sitech_models import TrackingFieldsMixin
from sitech_audit_log import AuditLogMixin
class Profile(models.Model, AuditLogMixin, TrackingFieldsMixin):
log_operation = ['updated']
phone = models.CharField(max_length=255, verbose_name='Phone')
address = models.TextField(max_length=512,verbose_name='Address')
If your model contains fields whose change don't need to trigger an activity being logged you can use ignore_changed_fields
from django.db import models
from sitech_models import TrackingFieldsMixin
from sitech_audit_log import AuditLogMixin
class Profile(models.Model, AuditLogMixin, TrackingFieldsMixin):
ignore_changed_fields = ['phone']
phone = models.CharField(max_length=255, verbose_name='Phone')
address = models.TextField(max_length=512,verbose_name='Address')
Changing phone
will not trigger an audit being logged.
Custom log storage backends should subclass BaseLoggingBackend
that is located in the sitech_audit_log.backends.base
module. A custom log storage backend must implement the save(self, audit_log)
method. This method receives an instance of AuditLog
class.
Here’s an example:
from sitech_audit_log.backends.base import BaseLoggingBackend
class TestBackend(BaseLoggingBackend):
def save(self, audit_log):
"""
Save the given audit_log.
"""
print(audit_log.operation, audit_log.values)