Skip to content

Commit 6c5bc94

Browse files
committed
Merge pull request #111 from treyhunner/allow-non-default-admin
Allow non-default admin instances
2 parents c9b2e2d + 3b7bf02 commit 6c5bc94

File tree

7 files changed

+55
-25
lines changed

7 files changed

+55
-25
lines changed

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+
- Removed some incompatibilities with non-default admin sites (gh-92)
7+
48
1.5.0 (2014-08-17)
59
------------------
610
- Extended availability of the ``as_of`` method to models as well as instances.

simple_history/admin.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import unicode_literals
22

3-
from django import template
43
from django.core.exceptions import PermissionDenied
54
try:
65
from django.conf.urls import patterns, url
@@ -10,7 +9,7 @@
109
from django.contrib.admin import helpers
1110
from django.contrib.contenttypes.models import ContentType
1211
from django.core.urlresolvers import reverse
13-
from django.shortcuts import get_object_or_404, render_to_response
12+
from django.shortcuts import get_object_or_404, render
1413
from django.contrib.admin.util import unquote
1514
from django.utils.text import capfirst
1615
from django.utils.html import mark_safe
@@ -75,10 +74,8 @@ def history_view(self, request, object_id, extra_context=None):
7574
'admin_user_view': admin_user_view
7675
}
7776
context.update(extra_context or {})
78-
context_instance = template.RequestContext(
79-
request, current_app=self.admin_site.name)
80-
return render_to_response(self.object_history_template, context,
81-
context_instance=context_instance)
77+
return render(request, template_name=self.object_history_template,
78+
dictionary=context, current_app=self.admin_site.name)
8279

8380
def history_form_view(self, request, object_id, version_id):
8481
original_model = self.model
@@ -166,12 +163,8 @@ def history_form_view(self, request, object_id, version_id):
166163
'save_on_top': self.save_on_top,
167164
'root_path': getattr(self.admin_site, 'root_path', None),
168165
}
169-
context_instance = template.RequestContext(
170-
request,
171-
current_app=self.admin_site.name,
172-
)
173-
return render_to_response(self.object_history_form_template, context,
174-
context_instance)
166+
return render(request, template_name=self.object_history_form_template,
167+
dictionary=context, current_app=self.admin_site.name)
175168

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

simple_history/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def get_extra_fields(self, model, fields):
162162

163163
@models.permalink
164164
def revert_url(self):
165+
"""URL for this change in the default admin site."""
165166
opts = model._meta
166167
try:
167168
app_label, model_name = opts.app_label, opts.module_name

simple_history/templates/simple_history/object_history.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{% extends "admin/object_history.html" %}
22
{% load i18n %}
33
{% load url from future %}
4+
{% load admin_urls %}
45

56

67
{% block content %}
@@ -22,12 +23,17 @@
2223
<tbody>
2324
{% for action in action_list %}
2425
<tr>
25-
<td><a href="{{ action.revert_url }}">{{ action.history_object }}</a></td>
26+
<td><a href="{% url opts|admin_urlname:'simple_history' object.pk action.pk %}">{{ action.history_object }}</a></td>
2627
<td>{{ action.history_date }}</td>
2728
<td>{{ action.get_history_type_display }}</td>
2829
<td>
2930
{% if action.history_user %}
30-
<a href="{% url admin_user_view action.history_user_id %}">{{ action.history_user }}</a>
31+
{% url admin_user_view action.history_user_id as admin_user_url %}
32+
{% if admin_user_url %}
33+
<a href="{{ admin_user_url }}">{{ action.history_user }}</a>
34+
{% else %}
35+
{{ action.history_user }}
36+
{% endif %}
3137
{% else %}
3238
None
3339
{% endif %}

simple_history/tests/other_admin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.contrib.admin.sites import AdminSite
2+
from simple_history.admin import SimpleHistoryAdmin
3+
from .models import State
4+
5+
site = AdminSite(name="other_admin")
6+
7+
site.register(State, SimpleHistoryAdmin)

simple_history/tests/tests/test_admin.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,28 @@
1111
from django.contrib.admin.util import quote
1212
from django.conf import settings
1313

14-
from ..models import Book, Person, Poll
14+
from ..models import Book, Person, Poll, State
1515

1616

1717
today = datetime(2021, 1, 1, 10, 0)
1818
tomorrow = today + timedelta(days=1)
1919

2020

21-
def get_history_url(model, history_index=None):
21+
def get_history_url(obj, history_index=None, site="admin"):
2222
try:
23-
info = model._meta.app_label, model._meta.module_name
23+
app, model = obj._meta.app_label, obj._meta.module_name
2424
except AttributeError:
25-
info = model._meta.app_label, model._meta.model_name
25+
app, model = obj._meta.app_label, obj._meta.model_name
2626
if history_index is not None:
27-
history = model.history.order_by('history_id')[history_index]
28-
return reverse('admin:%s_%s_simple_history' % info,
29-
args=[quote(model.pk), quote(history.history_id)])
27+
history = obj.history.order_by('history_id')[history_index]
28+
return reverse(
29+
"{site}:{app}_{model}_simple_history".format(
30+
site=site, app=app, model=model),
31+
args=[quote(obj.pk), quote(history.history_id)],
32+
)
3033
else:
31-
return reverse('admin:%s_%s_history' % info, args=[quote(model.pk)])
34+
return reverse("{site}:{app}_{model}_history".format(
35+
site=site, app=app, model=model), args=[quote(obj.pk)])
3236

3337

3438
class AdminSiteTest(WebTest):
@@ -181,3 +185,16 @@ def test_middleware_saves_user(self):
181185
self.assertEqual(historical_poll.history_user, self.user,
182186
"Middleware should make the request available to "
183187
"retrieve history_user.")
188+
189+
def test_other_admin(self):
190+
"""Test non-default admin instances.
191+
192+
Make sure non-default admin instances can resolve urls and
193+
render pages.
194+
"""
195+
self.login()
196+
state = State.objects.create()
197+
history_url = get_history_url(state, site="other_admin")
198+
self.app.get(history_url)
199+
change_url = get_history_url(state, 0, site="other_admin")
200+
self.app.get(change_url)

simple_history/tests/urls.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
from django.conf.urls.defaults import patterns, include, url
77

88
from django.contrib import admin
9+
from . import other_admin
10+
911
admin.autodiscover()
1012

11-
urlpatterns = patterns(
12-
'',
13+
urlpatterns = [
1314
url(r'^admin/', include(admin.site.urls)),
14-
)
15+
url(r'^other-admin/', include(other_admin.site.urls)),
16+
]

0 commit comments

Comments
 (0)