- 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
- Browser-native date input field by kytta 4 weeks, 1 day ago
- Generate and render HTML Table by LLyaudet 1 month, 1 week ago
- My firs Snippets by GutemaG 1 month, 1 week ago
- FileField having auto upload_to path by junaidmgithub 2 months, 2 weeks ago
- LazyPrimaryKeyRelatedField by LLyaudet 2 months, 3 weeks ago
Comments
Please login first before commenting.