Skip to content

Commit 089c5d4

Browse files
committed
Merge pull request #195 from treyhunner/allow-deleted-history-page
Add history listing of deleted objects
2 parents 6e09bdc + 5511334 commit 089c5d4

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
language: python
22

3+
sudo: false
4+
35
python:
46
- 2.6
57
- 2.7
@@ -13,7 +15,7 @@ env:
1315
- DJANGO="Django>=1.8,<1.9"
1416

1517
install:
16-
- pip install -U coverage codecov $DJANGO
18+
- pip install -U 'coverage<4' codecov $DJANGO
1719
- python -c 'from __future__ import print_function; import django; print("Django " + django.get_version())'
1820

1921
script: coverage run setup.py test

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changes
22
=======
33

4+
tip (unreleased)
5+
----------------
6+
- Add ability to list history in admin when the object instance is deleted. (gh-72)
7+
48
1.6.3 (2015-07-30)
59
------------------
610
- Respect `to_field` and `db_column` parameters (gh-182)

simple_history/admin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.contrib.admin import helpers
77
from django.contrib.contenttypes.models import ContentType
88
from django.core.urlresolvers import reverse
9+
from django.http import Http404
910
from django.shortcuts import get_object_or_404, render
1011
from django.utils.text import capfirst
1112
from django.utils.html import mark_safe
@@ -56,7 +57,13 @@ def history_view(self, request, object_id, extra_context=None):
5657
object_id = unquote(object_id)
5758
action_list = history.filter(**{pk_name: object_id})
5859
# If no history was found, see whether this object even exists.
59-
obj = get_object_or_404(model, pk=object_id)
60+
try:
61+
obj = model.objects.get(**{pk_name: object_id})
62+
except model.DoesNotExist:
63+
try:
64+
obj = action_list.latest('history_date').instance
65+
except action_list.model.DoesNotExist:
66+
raise Http404
6067
content_type = ContentType.objects.get_by_natural_key(
6168
*USER_NATURAL_KEY)
6269
admin_user_view = 'admin:%s_%s_change' % (content_type.app_label,

simple_history/tests/tests/test_admin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,13 @@ def test_missing_one_to_one(self):
250250
manager.delete()
251251
response = self.app.get(get_history_url(employee, 0))
252252
self.assertEqual(response.status_code, 200)
253+
254+
def test_history_deleted_instance(self):
255+
"""Ensure history page can be retrieved even for deleted instances"""
256+
self.login()
257+
employee = Employee.objects.create()
258+
employee_pk = employee.pk
259+
employee.delete()
260+
employee.pk = employee_pk
261+
response = self.app.get(get_history_url(employee))
262+
self.assertEqual(response.status_code, 200)

0 commit comments

Comments
 (0)