django-audit-log Documentation

Similar documents
Bricks Documentation. Release 1.0. Germano Guerrini

django-auditlog Documentation

Django IPRestrict Documentation

django-model-utils Documentation

Django Phantom Theme Documentation

CE419 Web Programming. Session 15: Django Web Framework

wagtailtrans Documentation

silk Documentation Release 0.3 Michael Ford

django SHOP Release dev0

Django starting guide

Django Admin Sortable Documentation

django-templation Documentation

django-teamwork Documentation

MIT AITI Python Software Development Lab DJ1:

Python Schema Generator Documentation

Django CBTools Documentation

django-slim Documentation

Tangent MicroServices Documentation

django-cron Documentation

django-openid Documentation

Graphene Documentation

Django-CSP Documentation

The Django Web Framework Part VI

django-facetools Documentation

django-embed-video Documentation

django-audiofield Documentation

Django-Select2 Documentation. Nirupam Biswas

django-embed-video Documentation

django-allauth-2fa Documentation

RapidSMS Rwanda Documentation

django-conduit Documentation

Easy-select2 Documentation

django-subdomains Documentation

Accelerating Information Technology Innovation

Django Concurrency Documentation

MIT Global Startup Labs México 2013

django-stored-messages Documentation

dango-ct Documentation

MapEntity Documentation

django-osm-field Release 0.3.1

staff Documentation Release 1.2

django-celery Documentation

Django Image Tools Documentation

tally Documentation Release Dan Watson

django-autocomplete-light Documentation

django-embed-video Documentation

alphafilter Documentation

Tuesday, July 2, 13. intentionally left blank

django-generic-aggregation Documentation

django-model-report Documentation

Django MFA Documentation

Django-frontend-notification Documentation

fragapy Documentation

Django Localized Recurrence Documentation

django simple pagination Documentation

django-responsive2 Documentation

Django design patterns Documentation

django-autocomplete-light Documentation

django-scaffold Documentation

django-reinhardt Documentation

django-intercom Documentation

django-konfera Documentation

open-helpdesk Documentation

Django File Picker Documentation

django-dajax Documentation

django-selenium Documentation

django-debreach Documentation

Django Extras Documentation

Django Map Widgets Documentation

django-secure Documentation

Django Groups Manager Documentation

django-parler Documentation

django-facebook-graph Documentation

cmsplugin-blog Release post 0 Øyvind Saltvik

Gargoyle Documentation

Vittles Documentation

DDF Documentation. Release Paulo Cheque

Django eztables Documentation

django-renderit Documentation

django-dajaxice Documentation

Django File Picker Documentation

django-messages Documentation

Django Map Widgets Documentation

django-oscar-paypal Documentation

django-import-export Documentation

Bishop Blanchet Intranet Documentation

django-photologue Documentation

django-getpaid Documentation

django-intranet Documentation

django-modeltranslation Documentation

django-image-cropping Documentation

django-oauth2-provider Documentation

Bambu API Documentation

django-geoip Documentation

Django Groups Manager Documentation

canary Documentation Release 0.1 Branton K. Davis

django-notifier Documentation

django-waffle Documentation

django-antispam Documentation

django-cas Documentation

Transcription:

django-audit-log Documentation Release 0.8.0 Vasil Vangelovski (Atomidata) Jul 21, 2017

Contents 1 Installation 3 2 Tracking Users that Created/Modified a Model 5 2.1 Tracking Who Created a Model..................................... 6 2.2 Tracking Who Made the Last Changes to a Model........................... 6 3 Tracking full model history 9 3.1 Querying the audit log.......................................... 9 3.2 M2M Relations.............................................. 10 3.3 Abstract Base Models.......................................... 10 3.4 Disabling/Enabling Tracking on a Model Instance........................... 11 4 Indices and tables 13 i

ii

Adds support for tracking who changed what models through your Django application. Tracking creators and modifiers of your model instances. Tracking full model history. Contents: Contents 1

2 Contents

CHAPTER 1 Installation Install from PyPI with easy_install or pip: pip install django-audit-log to hack on the code you can symlink the package in your site-packages from the source tree: python setup.py develop The package audit_log doesn t need to be in your INSTALLED_APPS. The only thing you need to modify in your settings.py is add audit_log.middleware.userloggingmiddleware to the MIDDLEWARE_CLASSES tupple: MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.auth.middleware.sessionauthenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware', 'audit_log.middleware.jwtauthmiddleware', 'audit_log.middleware.userloggingmiddleware', ) For users of django-rest-framework-jwt you should also include a special middleware that fixes a compatibility problem with that library: MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.auth.middleware.sessionauthenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware', 3

) 'audit_log.middleware.jwtauthmiddleware', 'audit_log.middleware.userloggingmiddleware', Note that in that case rest_framework_jwt.authentication.jsonwebtokenauthentication should be at the top of DEFAULT_AUTHENTICATION_CLASSES. 4 Chapter 1. Installation

CHAPTER 2 Tracking Users that Created/Modified a Model AuthStampedModel is an abstract model base class in the vein of TimeStampedModel from django-extensions. It has 4 fields used for tracking the user and the session key with which a model instance was created/modified: from audit_log.models import AuthStampedModel class WarehouseEntry(AuthStampedModel): product = models.foreignkey(product) quantity = models.decimalfield(max_digits = 10, decimal_places = 2) This will add 4 fields to the WarehouseEntry model: created_by - A foreign key to the user that created the model instance. created_with_session_key - Stores the session key with which the model instance was first created. modified_by - A foreign key to the user that last saved a model instance. modified_with_session_key - Stores the session key with which the model instance was last saved. The related names for the created_by and modified_by fields are created_%(class)s_set and modified_%(class)s_set respectively: In [3]: admin = User.objects.get(username = 'admin') In [4]: admin.created_warehouseentry_set.all() Out[4]: [<WarehouseEntry: WarehouseEntry object>, <WarehouseEntry: WarehouseEntry object>] In [5]: vasil = User.objects.get(username = 'vasil') In [6]: vasil.modified_warehouseentry_set.all() 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. This is very useful when used in conjuction with TimeStampedModel from django-extensions: 5

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")) currency = models.foreignkey(currency, verbose_name = _("currency")) invoice_number = models.charfield(_("invoice number"), blank = False, max_length = 15) date_issued = models.datefield(_("date issued")) date_due = models.datefield(verbose_name = _("date due")) comment = models.textfield(_("comment"), blank = True) is_paid = models.booleanfield(_("is paid"), default = False) date_paid = models.datefield(_("date paid"), blank = True, null = True) Tracking Who Created a Model You can track user information when model instances get created with the CreatingUserField and CreatingSessionKeyField. For example: from audit_log.models.fields import CreatingUserField, CreatingSessionKeyField class ProductCategory(models.Model): created_by = CreatingUserField(related_name = "created_categories") created_with_session_key = CreatingSessionKeyField() name = models.charfield(max_length=15) This is useful for tracking owners of model objects within your app. Tracking Who Made the Last Changes to a Model LastUserField and LastSessionKeyField will store the user and session key with which a model instance was last saved: 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 class ProductRating(models.Model): user = LastUserField() session = LastSessionKeyField() product = models.foreignkey(product) rating = models.positiveintegerfield() 6 Chapter 2. Tracking Users that Created/Modified a Model

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 session key will be stored in the session field. 2.2. Tracking Who Made the Last Changes to a Model 7

8 Chapter 2. Tracking Users that Created/Modified a Model

CHAPTER 3 Tracking full model history 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() 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) 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. 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, 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 particular model class: 9

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 widget created at 2011-02-25 06:03:42. 027220>] Accordingly you can get the changes made to a particular model instance like so: In [4]: Product.objects.all()[0].audit_log.all() Out[4]: [<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 widget created at 2011-02-25 06:03:42. 027220>] 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 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: action_id - Primary key for the log entry. action_date - The point in time when the logged action was performed. action_user - The user that performed the logged action. action_type - The type of the action (Created/Changed/Deleted) Any field of the original X model that is tracked by the audit log. M2M Relations Tracking changes on M2M Relations doesn t work for now. If you really need to track changes on M2M relations with this package, explicitly define the table with another model instead of declaring the M2M relation. 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. 10 Chapter 3. Tracking full model history

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. 3.4. Disabling/Enabling Tracking on a Model Instance 11

12 Chapter 3. Tracking full model history

CHAPTER 4 Indices and tables genindex modindex search 13