Skip to content

Commit 12f515b

Browse files
authored
Merge pull request #259 from treyhunner/django110
Add Django 1.10 support
2 parents 6580dde + dcfcb89 commit 12f515b

File tree

22 files changed

+310
-233
lines changed

22 files changed

+310
-233
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ env:
1313
- DJANGO="Django>=1.7,<1.8"
1414
- DJANGO="Django>=1.8,<1.9"
1515
- DJANGO="Django>=1.9,<1.10"
16+
- DJANGO="Django>=1.10,<1.11"
1617

1718
install:
1819
- pip install -U coverage codecov
@@ -31,5 +32,7 @@ matrix:
3132
env: DJANGO="Django>=1.7,<1.8"
3233
- python: 3.3
3334
env: DJANGO="Django>=1.9,<1.10"
35+
- python: 3.3
36+
env: DJANGO="Django>=1.10,<1.11"
3437

3538
after_success: codecov

runtests.py

Lines changed: 6 additions & 3 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
@@ -16,7 +17,7 @@
1617
'simple_history.tests',
1718
'simple_history.tests.custom_user',
1819
'simple_history.tests.external',
19-
'simple_history.tests.migration_test_app',
20+
'simple_history.registry_tests.migration_test_app',
2021

2122
'simple_history',
2223

@@ -50,6 +51,7 @@
5051

5152

5253
def main():
54+
5355
if not settings.configured:
5456
settings.configure(**DEFAULT_SETTINGS)
5557
if hasattr(django, 'setup'):
@@ -59,9 +61,10 @@ def main():
5961
except ImportError:
6062
from django.test.simple import DjangoTestSuiteRunner
6163
failures = DjangoTestSuiteRunner(failfast=False).run_tests(['tests'])
64+
failures |= DjangoTestSuiteRunner(failfast=False).run_tests(['registry_tests'])
6265
else:
63-
failures = DiscoverRunner(failfast=False).run_tests(
64-
['simple_history.tests'])
66+
failures = DiscoverRunner(failfast=False).run_tests(['simple_history.tests'])
67+
failures |= DiscoverRunner(failfast=False).run_tests(['simple_history.registry_tests'])
6568
sys.exit(failures)
6669

6770

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
tests_require = [
55
'Django>=1.4', 'WebTest==2.0.18', 'django-webtest==1.7.8', 'mock==1.0.1']
66
try:
7-
from unittest import skipUnless
7+
from unittest import skipUnless # noqa
88
except ImportError: # Python 2.6 compatibility
99
tests_require.append("unittest2")
1010

simple_history/admin.py

Lines changed: 13 additions & 4 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,8 +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, template_name=self.object_history_template,
79-
dictionary=context, current_app=request.current_app)
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)
8087

8188
def response_change(self, request, obj):
8289
if '_change_history' in request.POST and SIMPLE_HISTORY_EDIT:
@@ -178,8 +185,10 @@ def history_form_view(self, request, object_id, version_id):
178185
'save_on_top': self.save_on_top,
179186
'root_path': getattr(self.admin_site, 'root_path', None),
180187
}
181-
return render(request, template_name=self.object_history_form_template,
182-
dictionary=context, current_app=request.current_app)
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)
183192

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

simple_history/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
django-simple-history exceptions and warnings classes.
33
"""
44

5+
56
class MultipleRegistrationsError(Exception):
67
"""The model has been registered to have history tracking more than once"""
78
pass

simple_history/management/commands/populate_history.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,29 @@ class Command(BaseCommand):
2727
EXISTING_HISTORY_FOUND = "Existing history found, skipping model"
2828
INVALID_MODEL_ARG = "An invalid model was specified"
2929

30-
option_list = BaseCommand.option_list + (
31-
make_option(
30+
if hasattr(BaseCommand, 'option_list'): # Django < 1.8
31+
option_list = BaseCommand.option_list + (
32+
make_option('--auto', action='store_true', dest='auto', default=False),
33+
)
34+
35+
def add_arguments(self, parser):
36+
super(Command, self).add_arguments(parser)
37+
parser.add_argument('models', nargs='*', type=str)
38+
parser.add_argument(
3239
'--auto',
3340
action='store_true',
3441
dest='auto',
3542
default=False,
36-
help="Automatically search for models with the "
37-
"HistoricalRecords field type",
38-
),
39-
)
43+
help='Automatically search for models with the '
44+
'HistoricalRecords field type',
45+
)
4046

4147
def handle(self, *args, **options):
4248
to_process = set()
49+
model_strings = options.get('models', []) or args
4350

44-
if args:
45-
for model_pair in self._handle_model_list(*args):
51+
if model_strings:
52+
for model_pair in self._handle_model_list(*model_strings):
4653
to_process.add(model_pair)
4754

4855
elif options['auto']:

simple_history/middleware.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
from . models import HistoricalRecords
22

3+
try:
4+
from django.utils.deprecation import MiddlewareMixin as MiddlewareBase
5+
except ImportError: # Django < 1.10
6+
MiddlewareBase = object
37

4-
class HistoryRequestMiddleware(object):
8+
9+
class HistoryRequestMiddleware(MiddlewareBase):
510
"""Expose request to HistoricalRecords.
611
712
This middleware sets request as a local thread variable, making it

simple_history/models.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ def finalize(self, sender, **kwargs):
7878
pass
7979
else:
8080
if hint_class is not sender: # set in concrete
81-
if not (self.inherit and issubclass(sender, hint_class)): # set in abstract
82-
return
81+
if not (self.inherit and issubclass(sender, hint_class)):
82+
return # set in abstract
8383
if hasattr(sender._meta, 'simple_history_manager_attribute'):
84-
raise exceptions.MultipleRegistrationsError('{}.{} registered multiple times for history tracking.'.format(
85-
sender._meta.app_label,
86-
sender._meta.object_name,
87-
))
84+
raise exceptions.MultipleRegistrationsError(
85+
'{}.{} registered multiple times for history tracking.'.format(
86+
sender._meta.app_label,
87+
sender._meta.object_name,
88+
)
89+
)
8890
history_model = self.create_history_model(sender)
8991
module = importlib.import_module(self.module)
9092
setattr(module, history_model.__name__, history_model)

0 commit comments

Comments
 (0)