2015-08-31 11:58:59 +00:00
|
|
|
from django.contrib import admin
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.templatetags.static import static
|
2015-08-31 11:58:59 +00:00
|
|
|
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
|
|
|
|
from django.contrib.admin.utils import unquote
|
2021-05-21 08:47:27 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.urls import NoReverseMatch, reverse
|
|
|
|
from django.utils.html import format_html
|
|
|
|
from django.utils.safestring import mark_safe
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-08-31 11:58:59 +00:00
|
|
|
|
2021-05-21 08:47:27 +00:00
|
|
|
from orchestra.admin.utils import admin_date, admin_link
|
2015-08-31 11:58:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LogEntryAdmin(admin.ModelAdmin):
|
|
|
|
list_display = (
|
2015-10-05 14:49:15 +00:00
|
|
|
'display_action_time', 'user_link', 'display_message',
|
2015-09-04 10:22:14 +00:00
|
|
|
)
|
|
|
|
list_filter = (
|
|
|
|
'action_flag',
|
2016-05-13 08:42:58 +00:00
|
|
|
('user', admin.RelatedOnlyFieldListFilter),
|
2015-09-04 10:22:14 +00:00
|
|
|
('content_type', admin.RelatedOnlyFieldListFilter),
|
2015-08-31 11:58:59 +00:00
|
|
|
)
|
|
|
|
date_hierarchy = 'action_time'
|
2015-09-04 10:22:14 +00:00
|
|
|
search_fields = ('object_repr', 'change_message', 'user__username')
|
2015-08-31 11:58:59 +00:00
|
|
|
fields = (
|
2015-09-04 10:22:14 +00:00
|
|
|
'user_link', 'content_object_link', 'display_action_time', 'display_action',
|
|
|
|
'change_message'
|
2015-08-31 11:58:59 +00:00
|
|
|
)
|
|
|
|
readonly_fields = (
|
|
|
|
'user_link', 'content_object_link', 'display_action_time', 'display_action',
|
|
|
|
)
|
|
|
|
actions = None
|
2015-09-04 10:22:14 +00:00
|
|
|
list_select_related = ('user', 'content_type')
|
2016-04-28 10:34:54 +00:00
|
|
|
list_display_links = None
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
user_link = admin_link('user')
|
|
|
|
display_action_time = admin_date('action_time', short_description=_("Time"))
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.display(
|
|
|
|
description=_("Message"),
|
|
|
|
ordering='action_flag',
|
|
|
|
)
|
2021-05-21 08:47:27 +00:00
|
|
|
@mark_safe
|
2015-09-04 10:22:14 +00:00
|
|
|
def display_message(self, log):
|
2021-05-21 08:47:27 +00:00
|
|
|
edit = format_html('<a href="{url}"><img src="{img}"></img></a>', **{
|
2015-09-04 10:22:14 +00:00
|
|
|
'url': reverse('admin:admin_logentry_change', args=(log.pk,)),
|
2016-05-03 12:14:59 +00:00
|
|
|
'img': static('admin/img/icon-changelink.svg'),
|
2021-05-21 08:47:27 +00:00
|
|
|
})
|
2015-09-04 10:22:14 +00:00
|
|
|
if log.is_addition():
|
|
|
|
return _('Added "%(link)s". %(edit)s') % {
|
|
|
|
'link': self.content_object_link(log),
|
|
|
|
'edit': edit
|
|
|
|
}
|
|
|
|
elif log.is_change():
|
|
|
|
return _('Changed "%(link)s" - %(changes)s %(edit)s') % {
|
|
|
|
'link': self.content_object_link(log),
|
2016-10-22 07:23:45 +00:00
|
|
|
'changes': log.get_change_message(),
|
2015-09-04 10:22:14 +00:00
|
|
|
'edit': edit,
|
|
|
|
}
|
|
|
|
elif log.is_deletion():
|
|
|
|
return _('Deleted "%(object)s." %(edit)s') % {
|
|
|
|
'object': log.object_repr,
|
|
|
|
'edit': edit,
|
|
|
|
}
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.display(
|
|
|
|
description=_("Action"),
|
|
|
|
ordering='action_flag',
|
|
|
|
)
|
2015-08-31 11:58:59 +00:00
|
|
|
def display_action(self, log):
|
|
|
|
if log.is_addition():
|
|
|
|
return _("Added")
|
|
|
|
elif log.is_change():
|
|
|
|
return _("Changed")
|
|
|
|
return _("Deleted")
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.display(
|
|
|
|
description=_("Content object"),
|
|
|
|
ordering='object_repr',
|
|
|
|
)
|
2015-08-31 11:58:59 +00:00
|
|
|
def content_object_link(self, log):
|
|
|
|
ct = log.content_type
|
2015-10-05 14:49:15 +00:00
|
|
|
view = 'admin:%s_%s_change' % (ct.app_label, ct.model)
|
2015-08-31 11:58:59 +00:00
|
|
|
try:
|
2015-10-05 14:49:15 +00:00
|
|
|
url = reverse(view, args=(log.object_id,))
|
2015-08-31 11:58:59 +00:00
|
|
|
except NoReverseMatch:
|
|
|
|
return log.object_repr
|
2021-05-21 08:47:27 +00:00
|
|
|
return format_html('<a href="{}">{}</a>', url, log.object_repr)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-10-07 11:44:30 +00:00
|
|
|
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
|
2015-08-31 11:58:59 +00:00
|
|
|
""" Add rel_opts and object to context """
|
2015-10-07 11:44:30 +00:00
|
|
|
if not add and 'edit' in request.GET.urlencode():
|
|
|
|
context.update({
|
2015-08-31 11:58:59 +00:00
|
|
|
'rel_opts': obj.content_type.model_class()._meta,
|
|
|
|
'object': obj,
|
2015-10-07 11:44:30 +00:00
|
|
|
})
|
|
|
|
return super(LogEntryAdmin, self).render_change_form(
|
|
|
|
request, context, add, change, form_url, obj)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
def response_change(self, request, obj):
|
|
|
|
""" save and continue preserve edit query string """
|
|
|
|
response = super(LogEntryAdmin, self).response_change(request, obj)
|
|
|
|
if 'edit' in request.GET.urlencode() and 'edit' not in response.url:
|
|
|
|
return HttpResponseRedirect(response.url + '?edit=True')
|
|
|
|
return response
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
def response_post_save_change(self, request, obj):
|
|
|
|
""" save redirect to object history """
|
|
|
|
if 'edit' in request.GET.urlencode():
|
|
|
|
opts = obj.content_type.model_class()._meta
|
2015-10-05 14:49:15 +00:00
|
|
|
view = 'admin:%s_%s_history' % (opts.app_label, opts.model_name)
|
|
|
|
post_url = reverse(view, args=(obj.object_id,))
|
2015-08-31 11:58:59 +00:00
|
|
|
preserved_filters = self.get_preserved_filters(request)
|
2015-10-05 14:49:15 +00:00
|
|
|
post_url = add_preserved_filters({
|
|
|
|
'preserved_filters': preserved_filters, 'opts': opts
|
|
|
|
}, post_url)
|
2015-08-31 11:58:59 +00:00
|
|
|
return HttpResponseRedirect(post_url)
|
|
|
|
return super(LogEntryAdmin, self).response_post_save_change(request, obj)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
def has_add_permission(self, *args, **kwargs):
|
|
|
|
return False
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
def has_delete_permission(self, *args, **kwargs):
|
|
|
|
return False
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
def log_addition(self, *args, **kwargs):
|
|
|
|
pass
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
def log_change(self, *args, **kwargs):
|
|
|
|
pass
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-08-31 11:58:59 +00:00
|
|
|
def log_deletion(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(admin.models.LogEntry, LogEntryAdmin)
|