Skip to content

Commit 4492802

Browse files
committed
Merge pull request #197 from treyhunner/django19
Add Django 1.9 support
2 parents 6d9b12e + 9317c48 commit 4492802

File tree

17 files changed

+127
-80
lines changed

17 files changed

+127
-80
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ charset = utf-8
88
indent_style = space
99
end_of_line = lf
1010
insert_final_newline = true
11+
max_line_length = 79
1112
trim_trailing_whitespace = true
1213

1314
[*.{py,rst,ini}]

.travis.yml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ language: python
33
sudo: false
44

55
python:
6-
- 2.6
76
- 2.7
87
- 3.2
98
- 3.3
@@ -13,30 +12,36 @@ env:
1312
- DJANGO="Django>=1.6,<1.7"
1413
- DJANGO="Django>=1.7,<1.8"
1514
- DJANGO="Django>=1.8,<1.9"
15+
- DJANGO="Django>=1.9a,<1.10"
1616

1717
install:
18-
- pip install -U 'coverage<4' codecov $DJANGO
18+
- pip install -U 'coverage<4' codecov
19+
- pip install -U --pre $DJANGO
1920
- python -c 'from __future__ import print_function; import django; print("Django " + django.get_version())'
2021

2122
script: coverage run setup.py test
2223

2324
matrix:
2425
exclude:
25-
- python: 2.6
26-
env: DJANGO="Django>=1.6,<1.7"
27-
- python: 2.6
28-
env: DJANGO="Django>=1.7,<1.8"
29-
- python: 2.6
30-
env: DJANGO="Django>=1.8,<1.9"
3126
- python: 3.2
3227
env: DJANGO="Django>=1.4,<1.5"
3328
- python: 3.3
3429
env: DJANGO="Django>=1.4,<1.5"
30+
- python: 3.2
31+
env: DJANGO="Django>=1.9a,<1.10"
32+
- python: 3.3
33+
env: DJANGO="Django>=1.9a,<1.10"
3534

3635
include:
36+
- python: 2.6
37+
env: DJANGO="Django>=1.4,<1.5"
3738
- python: 3.4
3839
env: DJANGO="Django>=1.7,<1.8"
3940
- python: 3.4
4041
env: DJANGO="Django>=1.8,<1.9"
42+
- python: 3.5
43+
env: DJANGO="Django>=1.8,<1.9"
44+
- python: 3.5
45+
env: DJANGO="Django>=1.9a,<1.10"
4146

4247
after_success: codecov

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ tip (unreleased)
55
----------------
66
- Add ability to list history in admin when the object instance is deleted. (gh-72)
77
- Add ability to change history through the admin. (Enabled with the `SIMPLE_HISTORY_EDIT` setting.)
8+
- Add Django 1.9 support.
89

910
1.6.3 (2015-07-30)
1011
------------------

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ django-simple-history
2020

2121
django-simple-history stores Django model state on every create/update/delete.
2222

23-
This app requires Django 1.4.2 or greater and Python 2.6 or greater.
23+
This app requires Django 1.4.2, 1.6, or greater and Python 2.6 or greater.
2424

2525
Getting Help
2626
------------

docs/conf.py

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

9696
# The theme to use for HTML and HTML Help pages. See the documentation for
9797
# a list of builtin themes.
98-
html_theme = 'default'
98+
# html_theme = 'default'
9999

100100
# Theme options are theme-specific and customize the look and feel of a theme
101101
# further. For a list of options available for each theme, see the

docs/usage.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ settings:
4343
]
4444
4545
If you do not want to use the middleware, you can explicitly indicate
46-
the user making the change as documented in :ref:`recording_user`.
46+
the user making the change as documented in :doc:`/advanced`.
4747

4848
Models
4949
~~~~~~
@@ -121,7 +121,7 @@ An example of admin integration for the ``Poll`` and ``Choice`` models:
121121
admin.site.register(Poll, SimpleHistoryAdmin)
122122
admin.site.register(Choice, SimpleHistoryAdmin)
123123
124-
Changing a history-tracked model from the admin interface will automatically record the user who made the change (see :ref:`recording_user`).
124+
Changing a history-tracked model from the admin interface will automatically record the user who made the change (see :doc:`/advanced`).
125125

126126

127127
Querying history

runtests.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env python
2-
import sys
3-
from shutil import rmtree
2+
import logging
43
from os.path import abspath, dirname, join
4+
from shutil import rmtree
5+
import sys
6+
import warnings
57

68
import django
79
from django.conf import settings
810

9-
1011
sys.path.insert(0, abspath(dirname(__file__)))
1112

12-
1313
media_root = join(abspath(dirname(__file__)), 'test_files')
1414
rmtree(media_root, ignore_errors=True)
1515

@@ -39,6 +39,10 @@
3939
'django.contrib.auth.middleware.AuthenticationMiddleware',
4040
'django.contrib.messages.middleware.MessageMiddleware',
4141
],
42+
TEMPLATES=[{
43+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
44+
'APP_DIRS': True,
45+
}],
4246
)
4347

4448
if django.VERSION >= (1, 5):
@@ -62,4 +66,5 @@ def main():
6266

6367

6468
if __name__ == "__main__":
69+
logging.basicConfig()
6570
main()

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import simple_history
33

44
tests_require = [
5-
"Django>=1.4", "webtest==2.0.6", "django-webtest==1.7", "mock==1.0.1"
6-
]
5+
'Django>=1.4', 'WebTest==2.0.18', 'django-webtest==1.7.8', 'mock==1.0.1']
76
try:
87
from unittest import skipUnless
98
except ImportError: # Python 2.6 compatibility
@@ -21,7 +20,9 @@
2120
author_email='[email protected]',
2221
maintainer='Trey Hunner',
2322
url='https://github.com/treyhunner/django-simple-history',
24-
packages=["simple_history", "simple_history.management", "simple_history.management.commands"],
23+
packages=[
24+
'simple_history', 'simple_history.management',
25+
'simple_history.management.commands', 'simple_history.templatetags'],
2526
classifiers=[
2627
"Development Status :: 5 - Production/Stable",
2728
"Framework :: Django",

simple_history/admin.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from django import http
44
from django.core.exceptions import PermissionDenied
5-
from django.conf.urls import patterns, url
6-
from django.contrib.admin import helpers, ModelAdmin
5+
from django.conf.urls import url
6+
from django.contrib import admin
7+
from django.contrib.admin import helpers
78
from django.contrib.contenttypes.models import ContentType
89
from django.core.urlresolvers import reverse
910
from django.shortcuts import get_object_or_404, render
@@ -27,7 +28,7 @@
2728
SIMPLE_HISTORY_EDIT = getattr(settings, 'SIMPLE_HISTORY_EDIT', False)
2829

2930

30-
class SimpleHistoryAdmin(ModelAdmin):
31+
class SimpleHistoryAdmin(admin.ModelAdmin):
3132
object_history_template = "simple_history/object_history.html"
3233
object_history_form_template = "simple_history/object_history_form.html"
3334

@@ -40,16 +41,16 @@ def get_urls(self):
4041
info = opts.app_label, opts.model_name
4142
except AttributeError: # Django < 1.7
4243
info = opts.app_label, opts.module_name
43-
history_urls = patterns(
44-
"",
44+
history_urls = [
4545
url("^([^/]+)/history/([^/]+)/$",
4646
admin_site.admin_view(self.history_form_view),
4747
name='%s_%s_simple_history' % info),
48-
)
48+
]
4949
return history_urls + urls
5050

5151
def history_view(self, request, object_id, extra_context=None):
52-
"The 'history' admin view for this model."
52+
"""The 'history' admin view for this model."""
53+
request.current_app = self.admin_site.name
5354
model = self.model
5455
opts = model._meta
5556
app_label = opts.app_label
@@ -81,7 +82,7 @@ def history_view(self, request, object_id, extra_context=None):
8182
}
8283
context.update(extra_context or {})
8384
return render(request, template_name=self.object_history_template,
84-
dictionary=context, current_app=self.admin_site.name)
85+
dictionary=context, current_app=request.current_app)
8586

8687
def response_change(self, request, obj):
8788
if '_change_history' in request.POST and SIMPLE_HISTORY_EDIT:
@@ -101,6 +102,7 @@ def response_change(self, request, obj):
101102
request, obj)
102103

103104
def history_form_view(self, request, object_id, version_id):
105+
request.current_app = self.admin_site.name
104106
original_opts = self.model._meta
105107
model = getattr(
106108
self.model,
@@ -120,7 +122,7 @@ def history_form_view(self, request, object_id, version_id):
120122
change_history = False
121123

122124
if '_change_history' in request.POST and SIMPLE_HISTORY_EDIT:
123-
obj = obj.history.get(pk=version_id)
125+
obj = obj.history.get(pk=version_id).instance
124126

125127
formsets = []
126128
form_class = self.get_form(request, obj)
@@ -186,7 +188,7 @@ def history_form_view(self, request, object_id, version_id):
186188
'root_path': getattr(self.admin_site, 'root_path', None),
187189
}
188190
return render(request, template_name=self.object_history_form_template,
189-
dictionary=context, current_app=self.admin_site.name)
191+
dictionary=context, current_app=request.current_app)
190192

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

simple_history/models.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616
from django.utils.timezone import now
1717
from django.utils.translation import string_concat
1818

19-
try:
20-
import importlib
21-
except ImportError:
22-
from django.utils import importlib
2319
try:
2420
from django.apps import apps
2521
except ImportError: # Django < 1.7
26-
from django.db.models.loading import get_app
27-
apps = None
28-
else:
29-
get_app = apps.get_app
22+
from django.db.models import get_app
23+
try:
24+
import importlib
25+
except ImportError: # Python < 2.7
26+
from django.utils import importlib
3027
try:
3128
from south.modelsinspector import add_introspection_rules
3229
except ImportError: # south not present
@@ -103,14 +100,14 @@ def create_history_model(self, model):
103100
# registered under different app
104101
attrs['__module__'] = self.module
105102
elif app_module != self.module:
106-
if apps is None: # Django < 1.7
107-
# has meta options with app_label
108-
app = get_app(model._meta.app_label)
109-
attrs['__module__'] = app.__name__ # full dotted name
110-
else:
103+
try:
111104
# Abuse an internal API because the app registry is loading.
112105
app = apps.app_configs[model._meta.app_label]
113-
attrs['__module__'] = app.name # full dotted name
106+
except NameError: # Django < 1.7
107+
models_module = get_app(model._meta.app_label).__name__
108+
else:
109+
models_module = app.name
110+
attrs['__module__'] = models_module
114111

115112
fields = self.copy_fields(model)
116113
attrs.update(fields)
@@ -130,7 +127,10 @@ def copy_fields(self, model):
130127
fields = {}
131128
for field in model._meta.fields:
132129
field = copy.copy(field)
133-
field.rel = copy.copy(field.rel)
130+
try:
131+
field.remote_field = copy.copy(field.remote_field)
132+
except AttributeError:
133+
field.rel = copy.copy(field.rel)
134134
if isinstance(field, OrderWrt):
135135
# OrderWrt is a proxy field, switch to a plain IntegerField
136136
field.__class__ = models.IntegerField
@@ -146,7 +146,8 @@ def copy_fields(self, model):
146146
field_arguments['db_constraint'] = False
147147
if getattr(old_field, 'to_fields', []):
148148
field_arguments['to_field'] = old_field.to_fields[0]
149-
elif django.get_version() < "1.6" and old_field.rel.field_name != 'id':
149+
elif (django.get_version() < "1.6" and
150+
old_field.rel.field_name != 'id'):
150151
field_arguments['to_field'] = old_field.rel.field_name
151152
if getattr(old_field, 'db_column', None):
152153
field_arguments['db_column'] = old_field.db_column

0 commit comments

Comments
 (0)