Skip to content

Commit

Permalink
Admin history changes displays field verbose_name
Browse files Browse the repository at this point in the history
This should be more readable to website users, in contrast to displaying
the field's name from the code.
  • Loading branch information
ddabble committed Feb 18, 2024
1 parent 06ffe13 commit afb3827
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion simple_history/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ def format_history_delta_change(self, change: ModelChange) -> dict:
Override this to customize the displayed values in the "Changes" column of
the object history page.
"""
field_meta = self.model._meta.get_field(change.field)
return {
"field": change.field,
"field": capfirst(field_meta.verbose_name),
"old": truncatechars(change.old, self.max_displayed_history_change_chars),
"new": truncatechars(change.new, self.max_displayed_history_change_chars),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<ul>
{% for change in action.history_delta_changes %}
<li>
<code>{{ change.field }}</code>:
<strong>{{ change.field }}:</strong>
{{ 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 }}
Expand Down
14 changes: 7 additions & 7 deletions simple_history/tests/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,27 @@ def test_history_list_contains_diff_changes(self):
self.assertContains(response, "Changes")
# The poll hasn't had any of its fields changed after creation,
# so these values should not be present
self.assertNotContains(response, "question")
self.assertNotContains(response, "Question")
self.assertNotContains(response, "why?")
self.assertNotContains(response, "pub_date")
self.assertNotContains(response, "Date published")

poll.question = "how?"
poll.save()
response = self.client.get(poll_history_url)
self.assertContains(response, "question")
self.assertContains(response, "Question")
self.assertContains(response, "why?")
self.assertContains(response, "how?")
self.assertNotContains(response, "pub_date")
self.assertNotContains(response, "Date published")

poll.question = "when?"
poll.pub_date = parse_datetime("2024-04-04 04:04:04")
poll.save()
response = self.client.get(poll_history_url)
self.assertContains(response, "question")
self.assertContains(response, "Question")
self.assertContains(response, "why?")
self.assertContains(response, "how?")
self.assertContains(response, "when?")
self.assertContains(response, "pub_date")
self.assertContains(response, "Date published")
self.assertContains(response, "2021-01-01 10:00:00")
self.assertContains(response, "2024-04-04 04:04:04")

Expand All @@ -131,7 +131,7 @@ def test_history_list_doesnt_contain_too_long_diff_changes(self):
poll.save()

response = self.client.get(get_history_url(poll))
self.assertContains(response, "question")
self.assertContains(response, "Question")
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}…")
Expand Down

0 comments on commit afb3827

Please sign in to comment.