Login

Extended logging module

Author:
onlinehero
Posted:
July 9, 2009
Language:
Python
Version:
1.0
Score:
3 (after 3 ratings)

The django_admin_log only logs changes, not simple requests.

Sometimes it can be useful to log when a user of your admin interface is checking out important data, for instance if you are making a system with personal sensitive data, that needs to comply with government / company policies.

This will log such hits to the django_admin_log by overriding the change_view method in ModelAdmin.

So you must override this method in all classes you want to have logged.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
xlogging.py:

from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import force_unicode

VIEW = 4

def log_view(request, object):
    """    Log that an object is being viewed by the user.
    """
    from django.contrib.admin.models import LogEntry
    LogEntry.objects.log_action(
        user_id         = request.user.pk,
        content_type_id = ContentType.objects.get_for_model(object).pk,
        object_id       = object.pk,
        object_repr     = force_unicode(object),
        action_flag     = VIEW,
        change_message  = "Accessed object %s in %s." % (force_unicode(object), ContentType.objects.get_for_model(object))
    )

admin.py

from some.application.module.xlogging import log_view

class SomeAdmin(admin.ModelAdmin):
    def change_view(self, request, object_id, extra_context=None):
        object = self.queryset(request).get(pk=unquote(object_id))
        log_view(request, object)
        return super(SomeAdmin, self).change_view(request, object_id, extra_context)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.