Skip to content

Commit c2ef597

Browse files
committed
Add SIMPLE_HISTORY_EDIT setting to turn on and off history editing
1 parent 6b976dd commit c2ef597

File tree

4 files changed

+121
-17
lines changed

4 files changed

+121
-17
lines changed

simple_history/admin.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from django.core.exceptions import PermissionDenied
44
from django.conf.urls import patterns, url
5-
from django.contrib import admin
65
from django.contrib.admin import helpers, ModelAdmin
76
from django.contrib.contenttypes.models import ContentType
87
from django.core.urlresolvers import reverse
@@ -25,6 +24,8 @@
2524

2625
USER_NATURAL_KEY = tuple(key.lower() for key in USER_NATURAL_KEY.split('.', 1))
2726

27+
settings.SIMPLE_HISTORY_EDIT = getattr(settings, 'SIMPLE_HISTORY_EDIT', False)
28+
2829

2930
class SimpleHistoryAdmin(ModelAdmin):
3031
object_history_template = "simple_history/object_history.html"
@@ -77,7 +78,7 @@ def history_view(self, request, object_id, extra_context=None):
7778
dictionary=context, current_app=self.admin_site.name)
7879

7980
def response_change(self, request, obj):
80-
if '_change_history' in request.POST:
81+
if '_change_history' in request.POST and settings.SIMPLE_HISTORY_EDIT:
8182
verbose_name = obj._meta.verbose_name
8283

8384
msg = _('The %(name)s "%(obj)s" was changed successfully.') % {
@@ -86,8 +87,7 @@ def response_change(self, request, obj):
8687
}
8788

8889
self.message_user(
89-
request, "%s - %s" % (msg, _("You may edit it again below")),
90-
fail_silently=True)
90+
request, "%s - %s" % (msg, _("You may edit it again below")))
9191

9292
return HttpResponseRedirect(request.path)
9393
else:
@@ -108,7 +108,12 @@ def history_form_view(self, request, object_id, version_id):
108108
if not self.has_change_permission(request, obj):
109109
raise PermissionDenied
110110

111-
if '_change_history' in request.POST:
111+
if settings.SIMPLE_HISTORY_EDIT:
112+
change_history = True
113+
else:
114+
change_history = False
115+
116+
if '_change_history' in request.POST and settings.SIMPLE_HISTORY_EDIT:
112117
obj = obj.history.get(pk=version_id)
113118

114119
formsets = []
@@ -156,6 +161,8 @@ def history_form_view(self, request, object_id, version_id):
156161
args=(obj.pk,)),
157162
'history_url': reverse('%s:%s_%s_history' % url_triplet,
158163
args=(obj.pk,)),
164+
'change_history': change_history,
165+
159166
# Context variables copied from render_change_form
160167
'add': False,
161168
'change': True,

simple_history/templates/simple_history/object_history_form.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
{% endblock %}
1919

2020
{% block form_top %}
21-
<p>{% blocktrans %}Press the 'Revert' button below to revert to this version of the object. Or press the 'Change History' button to edit the history.{% endblocktrans %}</p>
21+
<p>{% blocktrans %}Press the 'Revert' button below to revert to this version of the object.{% endblocktrans %} {% if change_history %}{% blocktrans %}Or press the 'Change History' button to edit the history.{% endblocktrans %}{% endif %}</p>
2222
{% endblock %}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% load i18n %}
22
<div class="submit-row">
33
<input type="submit" value="{% trans 'Revert' %}" class="default" name="_save" {{ onclick_attrib }}/>
4-
<input type="submit" value="{% trans 'Change History' %}" class="default" name="_change_history" {{ onclick_attrib }}/>
4+
{% if change_history %}<input type="submit" value="{% trans 'Change History' %}" class="default" name="_change_history" {{ onclick_attrib }}/>{% endif %}
55
</div>

simple_history/tests/tests/test_admin.py

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ def test_deleteting_user(self):
248248
self.assertEqual(historical_poll.history_user, None)
249249

250250
def test_missing_one_to_one(self):
251-
"""A relation to a missing one-to-one model should still show history"""
251+
"""
252+
A relation to a missing one-to-one model should still show history
253+
"""
252254
self.login()
253255
manager = Employee.objects.create()
254256
employee = Employee.objects.create(manager=manager)
@@ -259,6 +261,10 @@ def test_missing_one_to_one(self):
259261
self.assertEqual(response.status_code, 200)
260262

261263
def test_response_change(self):
264+
"""
265+
Test the response_change method that it works with a _change_history
266+
in the POST and settings.SIMPLE_HISTORY_EDIT set to True
267+
"""
262268
request = RequestFactory().post('/')
263269
request.POST = {'_change_history': True}
264270
request.session = 'session'
@@ -269,12 +275,42 @@ def test_response_change(self):
269275
poll.question = "how?"
270276
poll.save()
271277

272-
admin = SimpleHistoryAdmin(Poll, 'admin')
278+
admin_site = AdminSite()
279+
admin = SimpleHistoryAdmin(Poll, admin_site)
273280

274-
response = admin.response_change(request, poll)
281+
with override_settings(SIMPLE_HISTORY_EDIT=True):
282+
response = admin.response_change(request, poll)
275283

276284
self.assertEqual(response.url, '/awesome/url/')
277285

286+
def test_response_change_change_history_setting_off(self):
287+
"""
288+
Test the response_change method that it works with a _change_history
289+
in the POST and settings.SIMPLE_HISTORY_EDIT set to False
290+
"""
291+
request = RequestFactory().post('/')
292+
request.POST = {'_change_history': True}
293+
request.session = 'session'
294+
request._messages = FallbackStorage(request)
295+
request.path = '/awesome/url/'
296+
request.user = self.user
297+
298+
poll = Poll.objects.create(question="why?", pub_date=today)
299+
poll.question = "how?"
300+
poll.save()
301+
302+
admin_site = AdminSite()
303+
admin = SimpleHistoryAdmin(Poll, admin_site)
304+
305+
response = admin.response_change(request, poll)
306+
307+
with patch(
308+
'simple_history.admin.ModelAdmin.response_change') as m_admin:
309+
m_admin.return_value = 'it was called'
310+
response = admin.response_change(request, poll)
311+
312+
self.assertEqual(response, 'it was called')
313+
278314
def test_response_change_no_change_history(self):
279315
request = RequestFactory().post('/')
280316
request.session = 'session'
@@ -285,11 +321,12 @@ def test_response_change_no_change_history(self):
285321
poll.question = "how?"
286322
poll.save()
287323

288-
admin = SimpleHistoryAdmin(Poll, 'admin')
324+
admin_site = AdminSite()
325+
admin = SimpleHistoryAdmin(Poll, admin_site)
289326

290327
with patch(
291-
'simple_history.admin.ModelAdmin.response_change') as mock_admin:
292-
mock_admin.return_value = 'it was called'
328+
'simple_history.admin.ModelAdmin.response_change') as m_admin:
329+
m_admin.return_value = 'it was called'
293330
response = admin.response_change(request, poll)
294331

295332
self.assertEqual(response, 'it was called')
@@ -305,7 +342,6 @@ def test_history_form_view_without_getting_history(self):
305342
poll.save()
306343
history = poll.history.all()[0]
307344

308-
309345
admin_site = AdminSite()
310346
admin = SimpleHistoryAdmin(Poll, admin_site)
311347

@@ -315,8 +351,9 @@ def test_history_form_view_without_getting_history(self):
315351
context = {
316352
# Verify this is set for original object
317353
'original': poll,
354+
'change_history': False,
318355

319-
'title': 'Revert {}'.format(force_text(history)),
356+
'title': 'Revert {}'.format(force_text(poll)),
320357
'adminform': ANY,
321358
'object_id': poll.id,
322359
'is_popup': False,
@@ -363,16 +400,17 @@ def test_history_form_view_getting_history(self):
363400
poll.save()
364401
history = poll.history.all()[0]
365402

366-
367403
admin_site = AdminSite()
368404
admin = SimpleHistoryAdmin(Poll, admin_site)
369405

370406
with patch('simple_history.admin.render') as mock_render:
371-
admin.history_form_view(request, poll.id, history.pk)
407+
with override_settings(SIMPLE_HISTORY_EDIT=True):
408+
admin.history_form_view(request, poll.id, history.pk)
372409

373410
context = {
374411
# Verify this is set for history object not poll object
375412
'original': history,
413+
'change_history': True,
376414

377415
'title': 'Revert {}'.format(force_text(history)),
378416
'adminform': ANY,
@@ -408,3 +446,62 @@ def test_history_form_view_getting_history(self):
408446
mock_render.assert_called_once_with(
409447
request, template_name=admin.object_history_form_template,
410448
dictionary=context, current_app=admin_site.name)
449+
450+
def test_history_form_view_getting_history_with_setting_off(self):
451+
request = RequestFactory().post('/')
452+
request.session = 'session'
453+
request._messages = FallbackStorage(request)
454+
request.user = self.user
455+
request.POST = {'_change_history': True}
456+
457+
poll = Poll.objects.create(question="why?", pub_date=today)
458+
poll.question = "how?"
459+
poll.save()
460+
history = poll.history.all()[0]
461+
462+
admin_site = AdminSite()
463+
admin = SimpleHistoryAdmin(Poll, admin_site)
464+
465+
with patch('simple_history.admin.render') as mock_render:
466+
with override_settings(SIMPLE_HISTORY_EDIT=False):
467+
admin.history_form_view(request, poll.id, history.pk)
468+
469+
context = {
470+
# Verify this is set for history object not poll object
471+
'original': poll,
472+
'change_history': False,
473+
474+
'title': 'Revert {}'.format(force_text(poll)),
475+
'adminform': ANY,
476+
'object_id': poll.id,
477+
'is_popup': False,
478+
'media': ANY,
479+
'errors': [
480+
'<ul class="errorlist"><li>This field is required.</li></ul>',
481+
'<ul class="errorlist"><li>This field is required.</li></ul>'
482+
],
483+
'app_label': 'tests',
484+
'original_opts': ANY,
485+
'changelist_url': '/admin/tests/poll/',
486+
'change_url': '/admin/tests/poll/1/',
487+
'history_url': '/admin/tests/poll/1/history/',
488+
'add': False,
489+
'change': True,
490+
'has_add_permission': admin.has_add_permission(request),
491+
'has_change_permission': admin.has_change_permission(
492+
request, poll),
493+
'has_delete_permission': admin.has_delete_permission(
494+
request, poll),
495+
'has_file_field': True,
496+
'has_absolute_url': False,
497+
'form_url': '',
498+
'opts': ANY,
499+
'content_type_id': ANY,
500+
'save_as': admin.save_as,
501+
'save_on_top': admin.save_on_top,
502+
'root_path': getattr(admin_site, 'root_path', None),
503+
}
504+
505+
mock_render.assert_called_once_with(
506+
request, template_name=admin.object_history_form_template,
507+
dictionary=context, current_app=admin_site.name)

0 commit comments

Comments
 (0)