|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | + |
| 5 | +""" |
| 6 | +@version: ?? |
| 7 | +@author: liangliangyy |
| 8 | +@license: MIT Licence |
| 9 | + |
| 10 | +@site: https://www.lylinux.net/ |
| 11 | +@software: PyCharm |
| 12 | +@file: logentryadmin.py |
| 13 | +@time: 2018/5/24 下午10:37 |
| 14 | +""" |
| 15 | +from django.contrib.contenttypes.models import ContentType |
| 16 | +from django.urls import reverse, NoReverseMatch |
| 17 | +from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION |
| 18 | +from django.contrib.auth import get_user_model |
| 19 | +from django.contrib.contenttypes.models import ContentType |
| 20 | +from django.utils.encoding import force_text |
| 21 | +from django.utils.html import escape |
| 22 | +from django.utils.translation import pgettext_lazy, ugettext_lazy as _ |
| 23 | +from django.utils.safestring import mark_safe |
| 24 | +from django.contrib import admin |
| 25 | + |
| 26 | +action_names = { |
| 27 | + ADDITION: pgettext_lazy('logentry_admin:action_type', 'Addition'), |
| 28 | + DELETION: pgettext_lazy('logentry_admin:action_type', 'Deletion'), |
| 29 | + CHANGE: pgettext_lazy('logentry_admin:action_type', 'Change'), |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +class LogEntryAdmin(admin.ModelAdmin): |
| 34 | + date_hierarchy = 'action_time' |
| 35 | + |
| 36 | + readonly_fields = ([f.name for f in LogEntry._meta.fields] + |
| 37 | + ['object_link', 'action_description', 'user_link', |
| 38 | + 'get_change_message']) |
| 39 | + |
| 40 | + fieldsets = ( |
| 41 | + (_('Metadata'), { |
| 42 | + 'fields': ( |
| 43 | + 'action_time', |
| 44 | + 'user_link', |
| 45 | + 'action_description', |
| 46 | + 'object_link', |
| 47 | + ) |
| 48 | + }), |
| 49 | + (_('Details'), { |
| 50 | + 'fields': ( |
| 51 | + 'get_change_message', |
| 52 | + 'content_type', |
| 53 | + 'object_id', |
| 54 | + 'object_repr', |
| 55 | + ) |
| 56 | + }), |
| 57 | + ) |
| 58 | + |
| 59 | + list_filter = [ |
| 60 | + 'content_type' |
| 61 | + ] |
| 62 | + |
| 63 | + search_fields = [ |
| 64 | + 'object_repr', |
| 65 | + 'change_message' |
| 66 | + ] |
| 67 | + |
| 68 | + list_display_links = [ |
| 69 | + 'action_time', |
| 70 | + 'get_change_message', |
| 71 | + ] |
| 72 | + list_display = [ |
| 73 | + 'action_time', |
| 74 | + 'user_link', |
| 75 | + 'content_type', |
| 76 | + 'object_link', |
| 77 | + 'action_description', |
| 78 | + 'get_change_message', |
| 79 | + ] |
| 80 | + |
| 81 | + def has_add_permission(self, request): |
| 82 | + return False |
| 83 | + |
| 84 | + def has_change_permission(self, request, obj=None): |
| 85 | + return ( |
| 86 | + request.user.is_superuser or |
| 87 | + request.user.has_perm('admin.change_logentry') |
| 88 | + ) and request.method != 'POST' |
| 89 | + |
| 90 | + def has_delete_permission(self, request, obj=None): |
| 91 | + return False |
| 92 | + |
| 93 | + def object_link(self, obj): |
| 94 | + object_link = escape(obj.object_repr) |
| 95 | + content_type = obj.content_type |
| 96 | + |
| 97 | + if obj.action_flag != DELETION and content_type is not None: |
| 98 | + # try returning an actual link instead of object repr string |
| 99 | + try: |
| 100 | + url = reverse( |
| 101 | + 'admin:{}_{}_change'.format(content_type.app_label, |
| 102 | + content_type.model), |
| 103 | + args=[obj.object_id] |
| 104 | + ) |
| 105 | + object_link = '<a href="{}">{}</a>'.format(url, object_link) |
| 106 | + except NoReverseMatch: |
| 107 | + pass |
| 108 | + return mark_safe(object_link) |
| 109 | + |
| 110 | + object_link.admin_order_field = 'object_repr' |
| 111 | + object_link.short_description = _('object') |
| 112 | + |
| 113 | + def user_link(self, obj): |
| 114 | + content_type = ContentType.objects.get_for_model(type(obj.user)) |
| 115 | + user_link = escape(force_text(obj.user)) |
| 116 | + try: |
| 117 | + # try returning an actual link instead of object repr string |
| 118 | + url = reverse( |
| 119 | + 'admin:{}_{}_change'.format(content_type.app_label, |
| 120 | + content_type.model), |
| 121 | + args=[obj.user.pk] |
| 122 | + ) |
| 123 | + user_link = '<a href="{}">{}</a>'.format(url, user_link) |
| 124 | + except NoReverseMatch: |
| 125 | + pass |
| 126 | + return mark_safe(user_link) |
| 127 | + |
| 128 | + user_link.admin_order_field = 'user' |
| 129 | + user_link.short_description = _('user') |
| 130 | + |
| 131 | + def get_queryset(self, request): |
| 132 | + queryset = super(LogEntryAdmin, self).get_queryset(request) |
| 133 | + return queryset.prefetch_related('content_type') |
| 134 | + |
| 135 | + def get_actions(self, request): |
| 136 | + actions = super(LogEntryAdmin, self).get_actions(request) |
| 137 | + if 'delete_selected' in actions: |
| 138 | + del actions['delete_selected'] |
| 139 | + return actions |
| 140 | + |
| 141 | + def action_description(self, obj): |
| 142 | + return action_names[obj.action_flag] |
| 143 | + |
| 144 | + action_description.short_description = _('action') |
| 145 | + |
| 146 | + def get_change_message(self, obj): |
| 147 | + return obj.get_change_message() |
| 148 | + |
| 149 | + get_change_message.short_description = _('change message') |
0 commit comments