Skip to content

Commit

Permalink
Refactored admin history changes to allow overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
ddabble committed Feb 18, 2024
1 parent 21eaaee commit e3388cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
19 changes: 18 additions & 1 deletion simple_history/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
from django.contrib.auth import get_permission_codename, get_user_model
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, render
from django.template.defaultfilters import truncatechars
from django.urls import re_path, reverse
from django.utils.encoding import force_str
from django.utils.html import mark_safe
from django.utils.text import capfirst
from django.utils.translation import gettext as _

from .models import ModelChange
from .utils import get_history_manager_for_model, get_history_model_for_model

SIMPLE_HISTORY_EDIT = getattr(settings, "SIMPLE_HISTORY_EDIT", False)
Expand All @@ -22,6 +24,8 @@ class SimpleHistoryAdmin(admin.ModelAdmin):
object_history_template = "simple_history/object_history.html"
object_history_form_template = "simple_history/object_history_form.html"

max_displayed_history_change_chars = 100

def get_urls(self):
"""Returns the additional urls used by the Reversion admin."""
urls = super().get_urls()
Expand Down Expand Up @@ -83,7 +87,9 @@ def history_view(self, request, object_id, extra_context=None):
# except the first (oldest) one
for i in range(len(action_list) - 1):
delta = action_list[i].diff_against(action_list[i + 1])
action_list[i].history_delta_changes = delta.changes
action_list[i].history_delta_changes = [
self.format_history_delta_change(change) for change in delta.changes
]

context = {
"title": self.history_view_title(request, obj),
Expand All @@ -110,6 +116,17 @@ def history_view_title(self, request, obj):
else:
return _("Change history: %s") % force_str(obj)

def format_history_delta_change(self, change: ModelChange):
"""
Override this to customize the displayed values in the "Changes" column of
the object history page.
"""
return {
"field": change.field,
"old": truncatechars(change.old, self.max_displayed_history_change_chars),
"new": truncatechars(change.new, self.max_displayed_history_change_chars),
}

def response_change(self, request, obj):
if "_change_history" in request.POST and SIMPLE_HISTORY_EDIT:
verbose_name = obj._meta.verbose_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
{% for change in action.history_delta_changes %}
<li>
<code>{{ change.field }}</code>:
{{ change.old|truncatechars:100 }}
{{ change.old }}
{# Add some spacing, and prevent having the arrow point to the edge of the page if `new` is wrapped #}
&nbsp;&rarr;&nbsp;&nbsp;{{ change.new|truncatechars:100 }}
&nbsp;&rarr;&nbsp;&nbsp;{{ change.new }}
</li>
{% endfor %}
</ul>
Expand Down
6 changes: 3 additions & 3 deletions simple_history/tests/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def test_history_list_doesnt_contain_too_long_diff_changes(self):

response = self.client.get(get_history_url(poll))
self.assertContains(response, "question")
# The limit should be 100 characters in total (including the ellipsis)
self.assertContains(response, f"{'A' * 99}…")
self.assertContains(response, f"{'B' * 99}…")
expected_num_chars = SimpleHistoryAdmin.max_displayed_history_change_chars - 1
self.assertContains(response, f"{'A' * expected_num_chars}…")
self.assertContains(response, f"{'B' * expected_num_chars}…")

def test_history_list_custom_fields(self):
model_name = self.user._meta.model_name
Expand Down

0 comments on commit e3388cf

Please sign in to comment.