Based on: View all log entries in the admin
Improvements:
- No crash on
object_link
when reverse route missing, - Filter by users present in the log AND in database currently
- Other filters on users - only superusers / only staff (modify to fit your needs)
EDIT:
Incorporated action_description
from django-logentry-admin.
- The list filter now has human-friendly action names, and the table also.
- Refactored list filter class hierarchy.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | from django.contrib import admin
from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
from django.utils.html import escape
from django.core.urlresolvers import reverse, NoReverseMatch
from django.contrib.auth.models import User
action_names = {
ADDITION: 'Addition',
CHANGE: 'Change',
DELETION: 'Deletion',
}
class FilterBase(admin.SimpleListFilter):
def queryset(self, request, queryset):
if self.value():
dictionary = dict(((self.parameter_name, self.value()),))
return queryset.filter(**dictionary)
class ActionFilter(FilterBase):
title = 'action'
parameter_name = 'action_flag'
def lookups(self, request, model_admin):
return action_names.items()
class UserFilter(FilterBase):
"""Use this filter to only show current users, who appear in the log."""
title = 'user'
parameter_name = 'user_id'
def lookups(self, request, model_admin):
return tuple((u.id, u.username)
for u in User.objects.filter(pk__in =
LogEntry.objects.values_list('user_id').distinct())
)
class AdminFilter(UserFilter):
"""Use this filter to only show current Superusers."""
title = 'admin'
def lookups(self, request, model_admin):
return tuple((u.id, u.username) for u in User.objects.filter(is_superuser=True))
class StaffFilter(UserFilter):
"""Use this filter to only show current Staff members."""
title = 'staff'
def lookups(self, request, model_admin):
return tuple((u.id, u.username) for u in User.objects.filter(is_staff=True))
class LogEntryAdmin(admin.ModelAdmin):
date_hierarchy = 'action_time'
readonly_fields = LogEntry._meta.get_all_field_names()
list_filter = [
UserFilter,
ActionFilter,
'content_type',
# 'user',
]
search_fields = [
'object_repr',
'change_message'
]
list_display = [
'action_time',
'user',
'content_type',
'object_link',
'action_flag',
'action_description',
'change_message',
]
def has_add_permission(self, request):
return False
def has_change_permission(self, request, obj=None):
return request.user.is_superuser and request.method != 'POST'
def has_delete_permission(self, request, obj=None):
return False
def object_link(self, obj):
ct = obj.content_type
repr_ = escape(obj.object_repr)
try:
href = reverse('admin:%s_%s_change' % (ct.app_label, ct.model), args=[obj.object_id])
link = u'<a href="%s">%s</a>' % (href, repr_)
except NoReverseMatch:
link = repr_
return link if obj.action_flag != DELETION else repr_
object_link.allow_tags = True
object_link.admin_order_field = 'object_repr'
object_link.short_description = u'object'
def queryset(self, request):
return super(LogEntryAdmin, self).queryset(request) \
.prefetch_related('content_type')
def action_description(self, obj):
return action_names[obj.action_flag]
action_description.short_description = 'Action'
admin.site.register(LogEntry, LogEntryAdmin)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
It works beautifully. Easy to implement. Thanks a lot.
#
Hi! I'm a complete newbie using Djancg-cms and I need to get all the Recents changes at my app and put it at my homepage. Where should I put this code to work?
Sorry for this silly question...
#
how can i add filter in queryset on the basis of created time to get only last one year log entries.
#
A small fix, the import of reverse is now:
from django.urls import reverse
#
Please login first before commenting.