Skip to content

Commit 4c60246

Browse files
blawsonRoss Mechanic
authored andcommitted
Set attribute on history list entries, from evaluated admin methods (#390)
* Set attribute on history list entries, from evaluated admin methods * Added test * Remove trailing commas * Fix line width in test * Add change * More descriptive names
1 parent 149911e commit 4c60246

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Authors
88
=======
99

1010
- Aleksey Kladov
11+
- Ben Lawson
1112
- bradford281
1213
- Buddy Lindsey, Jr.
1314
- Brian Dixon

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Unreleased
55
----------
66
- Add ability to specify custom history_id field (gh-368)
77
- Add HistoricalRecord instance properties `prev_record` and `next_record` (gh-365)
8+
- Can set admin methods as attributes on object history change list template (gh-390)
89

910
2.0 (2018-04-05)
1011
----------------

simple_history/admin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ def history_view(self, request, object_id, extra_context=None):
6161
if not self.has_change_permission(request, obj):
6262
raise PermissionDenied
6363

64+
# Set attribute on each action_list entry from admin methods
65+
for history_list_entry in history_list_display:
66+
value_for_entry = getattr(self, history_list_entry, None)
67+
if value_for_entry and callable(value_for_entry):
68+
for list_entry in action_list:
69+
setattr(list_entry, history_list_entry,
70+
value_for_entry(list_entry))
71+
6472
content_type = ContentType.objects.get_by_natural_key(
6573
*USER_NATURAL_KEY)
6674
admin_user_view = 'admin:%s_%s_change' % (content_type.app_label,

simple_history/tests/admin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ConcreteExternal,
1010
Document,
1111
Employee,
12+
FileModel,
1213
Paper,
1314
Person,
1415
Poll
@@ -24,6 +25,14 @@ class ChoiceAdmin(SimpleHistoryAdmin):
2425
history_list_display = ['votes']
2526

2627

28+
class FileModelAdmin(SimpleHistoryAdmin):
29+
30+
def test_method(self, obj):
31+
return 'test_method_value'
32+
33+
history_list_display = ['title', 'test_method']
34+
35+
2736
admin.site.register(Poll, SimpleHistoryAdmin)
2837
admin.site.register(Choice, ChoiceAdmin)
2938
admin.site.register(Person, PersonAdmin)
@@ -32,3 +41,4 @@ class ChoiceAdmin(SimpleHistoryAdmin):
3241
admin.site.register(Paper, SimpleHistoryAdmin)
3342
admin.site.register(Employee, SimpleHistoryAdmin)
3443
admin.site.register(ConcreteExternal, SimpleHistoryAdmin)
44+
admin.site.register(FileModel, FileModelAdmin)

simple_history/tests/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def save(self, *args, **kwargs):
119119

120120

121121
class FileModel(models.Model):
122+
title = models.CharField(max_length=100)
122123
file = models.FileField(upload_to='files')
123124
history = HistoricalRecords()
124125

simple_history/tests/tests/test_admin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Choice,
2020
ConcreteExternal,
2121
Employee,
22+
FileModel,
2223
Person,
2324
Poll,
2425
State
@@ -103,6 +104,25 @@ def test_history_list_custom_fields(self):
103104
self.assertIn("12", response.unicode_normal_body)
104105
self.assertIn("15", response.unicode_normal_body)
105106

107+
def test_history_list_custom_admin_methods(self):
108+
model_name = self.user._meta.model_name
109+
self.assertEqual(model_name, 'customuser')
110+
self.login()
111+
file_model = FileModel(title='Title 1')
112+
file_model._history_user = self.user
113+
file_model.save()
114+
file_model.title = 'Title 2'
115+
file_model.save()
116+
response = self.app.get(get_history_url(file_model))
117+
self.assertIn(get_history_url(file_model, 0),
118+
response.unicode_normal_body)
119+
self.assertIn("FileModel object", response.unicode_normal_body)
120+
self.assertIn("Created", response.unicode_normal_body)
121+
self.assertIn(self.user.username, response.unicode_normal_body)
122+
self.assertIn("test_method_value", response.unicode_normal_body)
123+
self.assertIn("Title 1", response.unicode_normal_body)
124+
self.assertIn("Title 2", response.unicode_normal_body)
125+
106126
def test_history_view_permission(self):
107127
self.login()
108128
person = Person.objects.create(name='Sandra Hale')

0 commit comments

Comments
 (0)