Skip to content

Commit 751c7f4

Browse files
committed
Fix version checking for Django < 1.8
1 parent a857d58 commit 751c7f4

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

runtests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
import logging
3+
from os import listdir, unlink
34
from os.path import abspath, dirname, join
45
from shutil import rmtree
56
import sys
@@ -50,6 +51,14 @@
5051

5152

5253
def main():
54+
# reset the test app migrations
55+
for migration_path in [
56+
join(dirname(__file__), 'simple_history', 'tests', 'migrations'),
57+
join(dirname(__file__), 'simple_history', 'tests', 'migration_test_app', 'migrations'),
58+
]:
59+
for migration_file_path in listdir(migration_path):
60+
unlink(join(migration_path, migration_file_path))
61+
5362
if not settings.configured:
5463
settings.configure(**DEFAULT_SETTINGS)
5564
if hasattr(django, 'setup'):

simple_history/admin.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
from django.contrib.admin.utils import unquote
1919
except ImportError: # Django < 1.7
2020
from django.contrib.admin.util import unquote
21+
try:
22+
from django.utils.version import get_complete_version
23+
except ImportError:
24+
from django import VERSION
25+
get_complete_version = lambda: VERSION
2126

2227
USER_NATURAL_KEY = tuple(
2328
key.lower() for key in settings.AUTH_USER_MODEL.split('.', 1))
@@ -75,7 +80,10 @@ def history_view(self, request, object_id, extra_context=None):
7580
'admin_user_view': admin_user_view
7681
}
7782
context.update(extra_context or {})
78-
return render(request, self.object_history_template, context)
83+
extra_kwargs = {}
84+
if get_complete_version() < (1, 8):
85+
extra_kwargs['current_app'] = request.current_app
86+
return render(request, self.object_history_template, context, **extra_kwargs)
7987

8088
def response_change(self, request, obj):
8189
if '_change_history' in request.POST and SIMPLE_HISTORY_EDIT:
@@ -177,7 +185,10 @@ def history_form_view(self, request, object_id, version_id):
177185
'save_on_top': self.save_on_top,
178186
'root_path': getattr(self.admin_site, 'root_path', None),
179187
}
180-
return render(request, self.object_history_form_template, context)
188+
extra_kwargs = {}
189+
if get_complete_version() < (1, 8):
190+
extra_kwargs['current_app'] = request.current_app
191+
return render(request, self.object_history_form_template, context, **extra_kwargs)
181192

182193
def save_model(self, request, obj, form, change):
183194
"""Set special model attribute to user for reference after save"""

simple_history/tests/tests/test_admin.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from django.contrib.messages.storage.fallback import FallbackStorage
77
from django.test.utils import override_settings
88
from django.test.client import RequestFactory
9-
from django import VERSION
109
from django.core.urlresolvers import reverse
1110
from django.conf import settings
1211
from django.utils.encoding import force_text
@@ -22,13 +21,17 @@
2221
from django.contrib.admin.util import quote
2322

2423
from simple_history.models import HistoricalRecords
25-
from simple_history.admin import SimpleHistoryAdmin
24+
from simple_history.admin import SimpleHistoryAdmin, get_complete_version
2625
from ..models import Book, Person, Poll, State, Employee
2726

2827

2928
today = datetime(2021, 1, 1, 10, 0)
3029
tomorrow = today + timedelta(days=1)
3130

31+
extra_kwargs = {}
32+
if get_complete_version() < (1, 8):
33+
extra_kwargs = {'current_app': 'admin'}
34+
3235

3336
def get_history_url(obj, history_index=None, site="admin"):
3437
app, model = obj._meta.app_label, obj._meta.model_name
@@ -107,12 +110,8 @@ def test_history_form(self):
107110
response.form['pub_date_0'] = "2021-01-02"
108111
response = response.form.submit()
109112
self.assertEqual(response.status_code, 302)
110-
if VERSION < (1, 4, 0):
111-
self.assertTrue(response.headers['location']
112-
.endswith(get_history_url(poll)))
113-
else:
114-
self.assertTrue(response.headers['location']
115-
.endswith(reverse('admin:tests_poll_changelist')))
113+
self.assertTrue(response.headers['location']
114+
.endswith(reverse('admin:tests_poll_changelist')))
116115

117116
# Ensure form for second version is correct
118117
response = self.app.get(get_history_url(poll, 1))
@@ -424,9 +423,8 @@ def test_history_form_view_without_getting_history(self):
424423
'save_on_top': admin.save_on_top,
425424
'root_path': getattr(admin_site, 'root_path', None),
426425
}
427-
428426
mock_render.assert_called_once_with(
429-
request, admin.object_history_form_template, context)
427+
request, admin.object_history_form_template, context, **extra_kwargs)
430428

431429
def test_history_form_view_getting_history(self):
432430
request = RequestFactory().post('/')
@@ -480,9 +478,8 @@ def test_history_form_view_getting_history(self):
480478
'save_on_top': admin.save_on_top,
481479
'root_path': getattr(admin_site, 'root_path', None),
482480
}
483-
484481
mock_render.assert_called_once_with(
485-
request, admin.object_history_form_template, context)
482+
request, admin.object_history_form_template, context, **extra_kwargs)
486483

487484
def test_history_form_view_getting_history_with_setting_off(self):
488485
request = RequestFactory().post('/')
@@ -535,6 +532,5 @@ def test_history_form_view_getting_history_with_setting_off(self):
535532
'save_on_top': admin.save_on_top,
536533
'root_path': getattr(admin_site, 'root_path', None),
537534
}
538-
539535
mock_render.assert_called_once_with(
540-
request, admin.object_history_form_template, context)
536+
request, admin.object_history_form_template, context, **extra_kwargs)

0 commit comments

Comments
 (0)