diff --git a/simple_history/admin.py b/simple_history/admin.py index 2383eaa4..a4cfbd54 100644 --- a/simple_history/admin.py +++ b/simple_history/admin.py @@ -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) @@ -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() @@ -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), @@ -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 diff --git a/simple_history/templates/simple_history/_object_history_list.html b/simple_history/templates/simple_history/_object_history_list.html index 4dbcd5a2..a71b7666 100644 --- a/simple_history/templates/simple_history/_object_history_list.html +++ b/simple_history/templates/simple_history/_object_history_list.html @@ -47,9 +47,9 @@ {% for change in action.history_delta_changes %}
{{ change.field }}
:
- {{ 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 #}
- → {{ change.new|truncatechars:100 }}
+ → {{ change.new }}