Giter Club home page Giter Club logo

django-table-change-logger's Introduction

Django Table Change Logger

Quick Description

A python package which logs each change made to a Django model instance.

How to set up?

  1. Add tablechangelogger to your INSTALLED_APPS
  2. Run python manage.py migrate to initialize the model
  3. Add TABLE_CHANGE_LOG_CONFIG to your settings.py file.
  4. Additionaly, you can add TABLE_CHANGE_LOG_ENABLED to enable/disable table change logging. If not provided, the default value is True.
TABLE_CHANGE_LOG_CONFIG={
    'LOGGABLE_APPS': {
        'drivers': {
            'yourapp.models.Drivers': {
                'fields': ['driver_id', 'driver_name'],
                'callbacks': ('yourapp.app.callbacks.your_method_name')
            }
        },
        'cars': {
            'yourapp.models.Car': {
                'fields': ['license_plate', 'car_model'],
                'callbacks': ('yourapp.app.callbacks.your_method_name')
            }
        }
    },
    'TABLE_CHANGE_LOG_ENABLED': True
}

The keys of TABLE_CHANGE_LOG_CONFIG indicate your app label, while the nested mappings contain relative project path of your model mapped to a dictionary containing loggable fields of that respective model.

You can optionally specify a callback function path in your configuration. The best practice is to place your callback function in yourapp/callbacks.py

How it works?

  • Obtains the TABLE_CHANGE_LOG_CONFIG from your respective settings file based on your environment.
  • Initializes LOGGABLE_MODELS with the relative project paths of your models based on your configuration variable.
  • Binds to pre_save signal of each loggable model
  • For each field specified in the configuration variable, creates a record in the TableChangesLog model in each instance update.

Example

This section serves as a small example to demonstrate this package. The example takes the configuration above into account. Supposing you have a model called Driver and fields called latest_speed and driver_name and driver_id:

    driver = Driver.objects.last()
    driver.latest_speed = 5
    driver.save()  # tablechangelogger won't create a record since 'latest_speed' was not among the loggable fields

    driver.driver_name = 'John Doe'
    driver.save()  # a record with this driver is created

    # you can also use tablechangelogger.utils.get_model_name 
    model_name = driver._meta.model.__name__
    
    log = TableChangesLog.objects.filter(
        instance_id=driver.id, table_name=model_name)
    print(log.field_name, log.log.changes.get_field_new_value(log.field_name))  # prints 'driver_name, John Doe'

The model structure

This package provides you a django model which is called TableChangesLog; which tracks each change to a model instance specified in your configuration mapping. An example record is as follows:

{
 'app_label': 'drivers',
 'created_at': datetime.datetime(2019, 2, 22, 9, 1, 14, 619568, tzinfo=<UTC>),
 'field_name': 'latest_speed',
 'id': 1,
 'instance_id': 1,
 'table_name': 'Driver',
 'log': Log(changes=Change(new_value=100, old_value=75), created=<True|False>)
 }

django-table-change-logger's People

Contributors

voltlinestech avatar

Stargazers

Josh Teneycke avatar Baran Bartu Demirci avatar Can Arsoy avatar

Watchers

James Cloos avatar

Forkers

fusionking

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.