Skip to content

Commit 9f6d7e1

Browse files
committed
feat(): includes a custom list of fields
1 parent 94ca2b5 commit 9f6d7e1

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

simple_history/admin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def history_view(self, request, object_id, extra_context=None):
5757
history = getattr(model, model._meta.simple_history_manager_attribute)
5858
object_id = unquote(object_id)
5959
action_list = history.filter(**{pk_name: object_id})
60+
history_list_display = getattr(self, "history_list_display", [])
6061
# If no history was found, see whether this object even exists.
6162
try:
6263
obj = model.objects.get(**{pk_name: object_id})
@@ -77,7 +78,8 @@ def history_view(self, request, object_id, extra_context=None):
7778
'root_path': getattr(self.admin_site, 'root_path', None),
7879
'app_label': app_label,
7980
'opts': opts,
80-
'admin_user_view': admin_user_view
81+
'admin_user_view': admin_user_view,
82+
'history_list_display': history_list_display,
8183
}
8284
context.update(extra_context or {})
8385
extra_kwargs = {}

simple_history/templates/simple_history/_object_history_list.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{% load i18n %}
22
{% load url from simple_history_compat %}
33
{% load admin_urls %}
4+
{% load getattribute from getattributes %}
45

5-
{{ foo }}
66
<table id="change-history" class="table table-bordered table-striped">
77
<thead>
88
<tr>
99
<th scope="col">{% trans 'Object' %}</th>
10+
{% for column in history_list_display %}
11+
<th scope="col">{% trans column %}</th>
12+
{% endfor %}
1013
<th scope="col">{% trans 'Date/time' %}</th>
1114
<th scope="col">{% trans 'Comment' %}</th>
1215
<th scope="col">{% trans 'Changed by' %}</th>
@@ -16,6 +19,9 @@
1619
{% for action in action_list %}
1720
<tr>
1821
<td><a href="{% url opts|admin_urlname:'simple_history' object.pk action.pk %}">{{ action.history_object }}</a></td>
22+
{% for column in history_list_display %}
23+
<td scope="col">{{ action|getattribute:column }}</th>
24+
{% endfor %}
1925
<td>{{ action.history_date }}</td>
2026
<td>{{ action.get_history_type_display }}</td>
2127
<td>

simple_history/templates/simple_history/object_history.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<div class="module">
1414
{% if action_list %}
15-
{% display_list module_name %}
15+
{% display_list %}
1616
{% else %}
1717
<p>{% trans "This object doesn't have a change history." %}</p>
1818
{% endif %}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django import template
2+
3+
register = template.Library()
4+
5+
6+
def getattribute(value, arg):
7+
"""Gets an attribute of an object dynamically from a string name"""
8+
9+
if hasattr(value, str(arg)):
10+
return getattr(value, arg)
11+
12+
register.filter('getattribute', getattribute)

simple_history/templatetags/simple_history_admin_list.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@
55

66
@register.inclusion_tag("simple_history/_object_history_list.html",
77
takes_context=True)
8-
def display_list(context, cl):
9-
print str(cl)
10-
context.update({'foo': str(cl)})
8+
def display_list(context):
119
return context

0 commit comments

Comments
 (0)