Skip to content

Commit 6d9b12e

Browse files
committed
Merge pull request #187 from buddylindsey/add-edit-history
Add ability to edit history
2 parents 089c5d4 + a3dd0ee commit 6d9b12e

File tree

7 files changed

+293
-13
lines changed

7 files changed

+293
-13
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Authors
33

44
- Aleksey Kladov
55
- bradford281
6+
- Buddy Lindsey, Jr.
67
- Brian Dixon
78
- Corey Bertram
89
- Damien Nozay

CHANGES.rst

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

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

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from setuptools import setup
22
import simple_history
33

4-
tests_require = ["Django>=1.4", "webtest==2.0.6", "django-webtest==1.7"]
4+
tests_require = [
5+
"Django>=1.4", "webtest==2.0.6", "django-webtest==1.7", "mock==1.0.1"
6+
]
57
try:
68
from unittest import skipUnless
79
except ImportError: # Python 2.6 compatibility

simple_history/admin.py

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

3+
from django import http
34
from django.core.exceptions import PermissionDenied
45
from django.conf.urls import patterns, url
5-
from django.contrib import admin
6-
from django.contrib.admin import helpers
6+
from django.contrib.admin import helpers, ModelAdmin
77
from django.contrib.contenttypes.models import ContentType
88
from django.core.urlresolvers import reverse
9-
from django.http import Http404
109
from django.shortcuts import get_object_or_404, render
1110
from django.utils.text import capfirst
1211
from django.utils.html import mark_safe
@@ -25,8 +24,10 @@
2524

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

27+
SIMPLE_HISTORY_EDIT = getattr(settings, 'SIMPLE_HISTORY_EDIT', False)
2828

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

@@ -63,7 +64,7 @@ def history_view(self, request, object_id, extra_context=None):
6364
try:
6465
obj = action_list.latest('history_date').instance
6566
except action_list.model.DoesNotExist:
66-
raise Http404
67+
raise http.Http404
6768
content_type = ContentType.objects.get_by_natural_key(
6869
*USER_NATURAL_KEY)
6970
admin_user_view = 'admin:%s_%s_change' % (content_type.app_label,
@@ -82,6 +83,23 @@ def history_view(self, request, object_id, extra_context=None):
8283
return render(request, template_name=self.object_history_template,
8384
dictionary=context, current_app=self.admin_site.name)
8485

86+
def response_change(self, request, obj):
87+
if '_change_history' in request.POST and SIMPLE_HISTORY_EDIT:
88+
verbose_name = obj._meta.verbose_name
89+
90+
msg = _('The %(name)s "%(obj)s" was changed successfully.') % {
91+
'name': force_text(verbose_name),
92+
'obj': force_text(obj)
93+
}
94+
95+
self.message_user(
96+
request, "%s - %s" % (msg, _("You may edit it again below")))
97+
98+
return http.HttpResponseRedirect(request.path)
99+
else:
100+
return super(SimpleHistoryAdmin, self).response_change(
101+
request, obj)
102+
85103
def history_form_view(self, request, object_id, version_id):
86104
original_opts = self.model._meta
87105
model = getattr(
@@ -96,6 +114,14 @@ def history_form_view(self, request, object_id, version_id):
96114
if not self.has_change_permission(request, obj):
97115
raise PermissionDenied
98116

117+
if SIMPLE_HISTORY_EDIT:
118+
change_history = True
119+
else:
120+
change_history = False
121+
122+
if '_change_history' in request.POST and SIMPLE_HISTORY_EDIT:
123+
obj = obj.history.get(pk=version_id)
124+
99125
formsets = []
100126
form_class = self.get_form(request, obj)
101127
if request.method == 'POST':
@@ -141,6 +167,8 @@ def history_form_view(self, request, object_id, version_id):
141167
args=(obj.pk,)),
142168
'history_url': reverse('%s:%s_%s_history' % url_triplet,
143169
args=(obj.pk,)),
170+
'change_history': change_history,
171+
144172
# Context variables copied from render_change_form
145173
'add': False,
146174
'change': True,

simple_history/templates/simple_history/object_history_form.html

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
</div>
1414
{% endblock %}
1515

16-
{% comment %}Hack to remove "Save as New" and "Save and Continue" buttons {% endcomment %}
17-
{% block content %}
18-
{% with 1 as is_popup %}
19-
{{block.super}}
20-
{% endwith %}
16+
{% block submit_buttons_bottom %}
17+
{% include "simple_history/submit_line.html" %}
2118
{% endblock %}
2219

2320
{% block form_top %}
24-
<p>{% blocktrans %}Press the save button below to revert to this version of the object.{% 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>
2522
{% endblock %}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% load i18n %}
2+
<div class="submit-row">
3+
<input type="submit" value="{% trans 'Revert' %}" class="default" name="_save" {{ onclick_attrib }}/>
4+
{% if change_history %}<input type="submit" value="{% trans 'Change History' %}" class="default" name="_change_history" {{ onclick_attrib }}/>{% endif %}
5+
</div>

0 commit comments

Comments
 (0)