Code Monkey home page Code Monkey logo

sitech-django-audit_log's Introduction

Sitech Django Audit Log

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.


prerequisites

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:

  1. Run the pip command to install the latest version:
 pip install git+https://github.com/sitmena/[email protected]
  1. Add sitech_middlewares.request.RequestMiddleware to MIDDLEWARE in settings.py:
 MIDDLEWARE = (
    ...
    'sitech_middlewares.request.RequestMiddleware',
 )

Installation

  1. Run the pip command to install the latest version:
 pip install git+https://github.com/sitmena/[email protected]
  1. Add sitech_audit_log to your INSTALLED_APPS in settings.py:
 INSTALLED_APPS = (
    ...
    'sitech_audit_log',
 )
  1. Add the follwoing line in settings.py:
 JSONFIELD_ENCODER_CLASS = 'django.core.serializers.json.DjangoJSONEncoder'
  1. Run the migration command:
 python manage.py migrate

Usage

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')

# Specifying Log Storages:

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.


# Customizing the operations being logged

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')

# Ignoring changes to certain fields

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.


# Defining a custom log storage backend

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)

sitech-django-audit_log's People

Contributors

abdelhadi92 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.