Skip to content

Commit 06ffe13

Browse files
committed
Refactored admin history changes to allow overriding
Leaving no easy way for users to customize this - particularly at what point the characters are truncated - is perhaps not such a good idea.
1 parent 21eaaee commit 06ffe13

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

simple_history/admin.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
from django.contrib.auth import get_permission_codename, get_user_model
88
from django.core.exceptions import PermissionDenied
99
from django.shortcuts import get_object_or_404, render
10+
from django.template.defaultfilters import truncatechars
1011
from django.urls import re_path, reverse
1112
from django.utils.encoding import force_str
1213
from django.utils.html import mark_safe
1314
from django.utils.text import capfirst
1415
from django.utils.translation import gettext as _
1516

17+
from .models import ModelChange
1618
from .utils import get_history_manager_for_model, get_history_model_for_model
1719

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

27+
max_displayed_history_change_chars = 100
28+
2529
def get_urls(self):
2630
"""Returns the additional urls used by the Reversion admin."""
2731
urls = super().get_urls()
@@ -83,7 +87,9 @@ def history_view(self, request, object_id, extra_context=None):
8387
# except the first (oldest) one
8488
for i in range(len(action_list) - 1):
8589
delta = action_list[i].diff_against(action_list[i + 1])
86-
action_list[i].history_delta_changes = delta.changes
90+
action_list[i].history_delta_changes = [
91+
self.format_history_delta_change(change) for change in delta.changes
92+
]
8793

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

119+
def format_history_delta_change(self, change: ModelChange) -> dict:
120+
"""
121+
Override this to customize the displayed values in the "Changes" column of
122+
the object history page.
123+
"""
124+
return {
125+
"field": change.field,
126+
"old": truncatechars(change.old, self.max_displayed_history_change_chars),
127+
"new": truncatechars(change.new, self.max_displayed_history_change_chars),
128+
}
129+
113130
def response_change(self, request, obj):
114131
if "_change_history" in request.POST and SIMPLE_HISTORY_EDIT:
115132
verbose_name = obj._meta.verbose_name

simple_history/templates/simple_history/_object_history_list.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
{% for change in action.history_delta_changes %}
4848
<li>
4949
<code>{{ change.field }}</code>:
50-
{{ change.old|truncatechars:100 }}
50+
{{ change.old }}
5151
{# Add some spacing, and prevent having the arrow point to the edge of the page if `new` is wrapped #}
52-
&nbsp;&rarr;&nbsp;&nbsp;{{ change.new|truncatechars:100 }}
52+
&nbsp;&rarr;&nbsp;&nbsp;{{ change.new }}
5353
</li>
5454
{% endfor %}
5555
</ul>

simple_history/tests/tests/test_admin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ def test_history_list_doesnt_contain_too_long_diff_changes(self):
132132

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

139139
def test_history_list_custom_fields(self):
140140
model_name = self.user._meta.model_name

0 commit comments

Comments
 (0)