Commit 8a1f52c6 authored by SVN-Git Migration's avatar SVN-Git Migration

Imported Upstream version 0.3.3

parent 176bf2d9
Metadata-Version: 1.1
Name: django-adminaudit
Version: 0.3.2
Version: 0.3.3
Summary: Extends Django's admin logging capabilities
Home-page: https://launchpad.net/django-adminaudit
Author: Łukasz Czyżykowski
......
# Copyright 2010-2012 Canonical Ltd. This software is licensed under
# the GNU Lesser General Public License version 3 (see the file LICENSE).
from django.contrib.admin import site
from adminaudit.models import AuditLog
class AdminAuditMixin(object):
def _flatten(self, lst):
result = []
for item in lst:
if isinstance(item, list):
result.extend(self._flatten(item))
else:
result.append(item)
return result
def _collect_deleted_objects(self, obj):
result = []
try:
# This is for Django up to 1.2
from django.db.models.query_utils import CollectedObjects
seen_objs = CollectedObjects()
obj._collect_sub_objects(seen_objs)
for cls, subobjs in seen_objs.iteritems():
for subobj in subobjs.values():
result.append(subobj)
except ImportError:
# Django 1.3 solution, those imports needs to be here, because
# otherwise they will fail on Django < 1.3.
from django.contrib.admin.util import NestedObjects
from django.db import router
using = router.db_for_write(obj)
collector = NestedObjects(using=using)
collector.collect([obj])
result = self._flatten(collector.nested())
return result
def log_addition(self, request, obj, *args, **kwargs):
AuditLog.create(request.user, obj, 'create')
super(AdminAuditMixin, self).log_addition(request, obj, *args, **kwargs)
def log_deletion(self, request, obj, *args, **kwargs):
for subobj in self._collect_deleted_objects(obj):
AuditLog.create(request.user, subobj, 'delete')
super(AdminAuditMixin, self).log_deletion(request, obj, *args, **kwargs)
def save_model(self, request, new_obj, form, change):
if change:
# This is so that we'll get the values of the object before the
# change
old_obj = new_obj.__class__.objects.get(pk=new_obj.pk)
AuditLog.create(request.user, old_obj, 'update', new_object=new_obj)
super(AdminAuditMixin, self).save_model(
request, new_obj, form, change)
def audit_install():
# import here to avoid triggering django imports during package import
from django.contrib.admin import site
from .models import AdminAuditMixin, AuditLog
for model, model_admin in site._registry.items():
if (model is AuditLog or isinstance(model_admin, AdminAuditMixin)):
# Do not mingle with our own model
......
......@@ -3,14 +3,14 @@
from django.core import serializers
from django.db import models
from django.utils import simplejson
from django.db.models.fields.files import FileField
from django.utils import simplejson
class AuditLog(models.Model):
"""
Records of all changes made via Django admin interface.
"""
username = models.CharField(max_length=255)
user_id = models.IntegerField()
......@@ -51,3 +51,56 @@ class AuditLog(models.Model):
representation=unicode(obj),
change=change,
)
class AdminAuditMixin(object):
def _flatten(self, lst):
result = []
for item in lst:
if isinstance(item, list):
result.extend(self._flatten(item))
else:
result.append(item)
return result
def _collect_deleted_objects(self, obj):
result = []
try:
# This is for Django up to 1.2
from django.db.models.query_utils import CollectedObjects
seen_objs = CollectedObjects()
obj._collect_sub_objects(seen_objs)
for cls, subobjs in seen_objs.iteritems():
for subobj in subobjs.values():
result.append(subobj)
except ImportError:
# Django 1.3 solution, those imports needs to be here, because
# otherwise they will fail on Django < 1.3.
from django.contrib.admin.util import NestedObjects
from django.db import router
using = router.db_for_write(obj)
collector = NestedObjects(using=using)
collector.collect([obj])
result = self._flatten(collector.nested())
return result
def log_addition(self, request, obj, *args, **kwargs):
AuditLog.create(request.user, obj, 'create')
super(AdminAuditMixin, self).log_addition(request, obj, *args, **kwargs)
def log_deletion(self, request, obj, *args, **kwargs):
for subobj in self._collect_deleted_objects(obj):
AuditLog.create(request.user, subobj, 'delete')
super(AdminAuditMixin, self).log_deletion(request, obj, *args, **kwargs)
def save_model(self, request, new_obj, form, change):
if change:
# This is so that we'll get the values of the object before the
# change
old_obj = new_obj.__class__.objects.get(pk=new_obj.pk)
AuditLog.create(request.user, old_obj, 'update', new_object=new_obj)
super(AdminAuditMixin, self).save_model(
request, new_obj, form, change)
# configglue schema to enable projects using configglue to use adminaudit
# this schema represents all adminaudit available configuration settings
from configglue.schema import (
ListOption,
Schema,
Section,
StringOption,
)
class AdminAuditSchema(Schema):
class adminaudit(Section):
adminaudit_emails_recipients = ListOption(
item=StringOption(),
help='List of email addresss to send reports to.')
adminaudit_summary_subject = StringOption(
default='Admin Audit Summary',
help='Email report subject.')
adminaudit_email_from = StringOption(
help='Email address from which to send reports.')
......@@ -39,7 +39,7 @@ class TestCommand(Command):
setup(
name='django-adminaudit',
version='0.3.2',
version='0.3.3',
description="Extends Django's admin logging capabilities",
url='https://launchpad.net/django-adminaudit',
author='Łukasz Czyżykowski',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment