Skip to content

Commit 184ecdd

Browse files
authored
Enable/ disable revert using only a settings attribute (#632)
* show revert text / buttons based on value of revert_disabled. * add tests for revert disabled functionality * add SIMPLE_HISTORY_REVERT_DISABLED as attr to control visibility of Revert button. * capture --tag from args * correct variable name to revert_disabled * update docs * CHANGES.rst * remove unused test tags * fix minor formatting issues in templates * fix, only show text if revert is NOT disabled * use random file name in tests (uuid)
1 parent a9d5493 commit 184ecdd

File tree

13 files changed

+139
-23
lines changed

13 files changed

+139
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ htmlcov/
1616
MANIFEST
1717
test_files/
1818
venv/
19+
.DS_Store

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Unreleased
99
- import model `ContentType` in `SimpleHistoryAdmin` using `django_apps.get_model`
1010
to avoid possible `AppRegistryNotReady` exception (gh-630)
1111
- Fix `utils.update_change_reason` when user specifies excluded_fields
12+
- settings.SIMPLE_HISTORY_REVERT_DISABLED if True removes the Revert
13+
button from the history form for all historical models (gh-632))
1214

1315
2.8.0 (2019-12-02)
1416
------------------

docs/admin.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,16 @@ admin class
6868
6969
7070
.. image:: screens/5_history_list_display.png
71+
72+
Disabling the option to revert an object
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+
75+
By default, an object can be reverted to its previous version. To disable this option update your settings with the following:
76+
77+
.. code-block:: python
78+
79+
SIMPLE_HISTORY_REVERT_DISABLED=True
80+
81+
When ``SIMPLE_HISTORY_REVERT_DISABLED`` is set to ``True``, the revert button is removed from the form.
82+
83+
.. image:: screens/10_revert_disabled.png

docs/screens/10_revert_disabled.png

86.3 KB
Loading

runtests.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ def __getitem__(self, item):
7373

7474

7575
def main():
76-
7776
if not settings.configured:
7877
settings.configure(**DEFAULT_SETTINGS)
7978
django.setup()
80-
failures = DiscoverRunner(failfast=False).run_tests(["simple_history.tests"])
81-
failures |= DiscoverRunner(failfast=False).run_tests(
79+
tags = [t.split("=")[1] for t in sys.argv if t.startswith("--tag")]
80+
failures = DiscoverRunner(failfast=False, tags=tags).run_tests(
81+
["simple_history.tests"]
82+
)
83+
failures |= DiscoverRunner(failfast=False, tags=tags).run_tests(
8284
["simple_history.registry_tests"]
8385
)
8486
sys.exit(failures)

simple_history/admin.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def history_view(self, request, object_id, extra_context=None):
8888
content_type.model,
8989
)
9090
context = {
91-
"title": _("Change history: %s") % force_str(obj),
91+
"title": self.history_view_title(obj),
9292
"action_list": action_list,
9393
"module_name": capfirst(force_str(opts.verbose_name_plural)),
9494
"object": obj,
@@ -97,6 +97,7 @@ def history_view(self, request, object_id, extra_context=None):
9797
"opts": opts,
9898
"admin_user_view": admin_user_view,
9999
"history_list_display": history_list_display,
100+
"revert_disabled": self.revert_disabled,
100101
}
101102
context.update(self.admin_site.each_context(request))
102103
context.update(extra_context or {})
@@ -105,6 +106,12 @@ def history_view(self, request, object_id, extra_context=None):
105106
request, self.object_history_template, context, **extra_kwargs
106107
)
107108

109+
def history_view_title(self, obj):
110+
if self.revert_disabled and not SIMPLE_HISTORY_EDIT:
111+
return _("View history: %s") % force_str(obj)
112+
else:
113+
return _("Change history: %s") % force_str(obj)
114+
108115
def response_change(self, request, obj):
109116
if "_change_history" in request.POST and SIMPLE_HISTORY_EDIT:
110117
verbose_name = obj._meta.verbose_name
@@ -175,7 +182,7 @@ def history_form_view(self, request, object_id, version_id, extra_context=None):
175182
model_name = original_opts.model_name
176183
url_triplet = self.admin_site.name, original_opts.app_label, model_name
177184
context = {
178-
"title": _("Revert %s") % force_str(obj),
185+
"title": self.history_form_view_title(obj),
179186
"adminform": admin_form,
180187
"object_id": object_id,
181188
"original": obj,
@@ -188,6 +195,7 @@ def history_form_view(self, request, object_id, version_id, extra_context=None):
188195
"change_url": reverse("%s:%s_%s_change" % url_triplet, args=(obj.pk,)),
189196
"history_url": reverse("%s:%s_%s_history" % url_triplet, args=(obj.pk,)),
190197
"change_history": change_history,
198+
"revert_disabled": self.revert_disabled,
191199
# Context variables copied from render_change_form
192200
"add": False,
193201
"change": True,
@@ -212,6 +220,12 @@ def history_form_view(self, request, object_id, version_id, extra_context=None):
212220
request, self.object_history_form_template, context, **extra_kwargs
213221
)
214222

223+
def history_form_view_title(self, obj):
224+
if self.revert_disabled:
225+
return _("View %s") % force_str(obj)
226+
else:
227+
return _("Revert %s") % force_str(obj)
228+
215229
def render_history_view(self, request, template, context, **kwargs):
216230
"""Catch call to render, to allow overriding."""
217231
return render(request, template, context, **kwargs)
@@ -226,3 +240,7 @@ def content_type_model_cls(self):
226240
"""Returns the ContentType model class.
227241
"""
228242
return django_apps.get_model("contenttypes.contenttype")
243+
244+
@property
245+
def revert_disabled(self):
246+
return getattr(settings, "SIMPLE_HISTORY_REVERT_DISABLED", False)

simple_history/templates/simple_history/object_history.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
{% block content %}
99
<div id="content-main">
10-
11-
<p>{% blocktrans %}Choose a date from the list below to revert to a previous version of this object.{% endblocktrans %}</p>
12-
10+
{% if not revert_disabled %}<p>
11+
{% blocktrans %}Choose a date from the list below to revert to a previous version of this object.{% endblocktrans %}</p>{% endif %}
1312
<div class="module">
1413
{% if action_list %}
1514
{% display_list %}
@@ -19,4 +18,3 @@
1918
</div>
2019
</div>
2120
{% endblock %}
22-

simple_history/templates/simple_history/object_history_form.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<a href="{{changelist_url}}">{{opts.verbose_name_plural|capfirst}}</a> &rsaquo;
1010
<a href="{{change_url}}">{{original|truncatewords:"18"}}</a> &rsaquo;
1111
<a href="../">{% trans "History" %}</a> &rsaquo;
12-
{% blocktrans with original_opts.verbose_name as verbose_name %}Revert {{verbose_name}}{% endblocktrans %}
12+
{% if revert_disabled %}{% blocktrans with original_opts.verbose_name as verbose_name %}View {{verbose_name}}{% endblocktrans %}{% else %}{% blocktrans with original_opts.verbose_name as verbose_name %}Revert {{verbose_name}}{% endblocktrans %}{% endif %}
1313
</div>
1414
{% endblock %}
1515

@@ -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.{% endblocktrans %} {% if change_history %}{% blocktrans %}Or press the 'Change History' button to edit the history.{% endblocktrans %}{% endif %}</p>
21+
<p>{% if not revert_disabled %}{% blocktrans %}Press the 'Revert' button below to revert to this version of the object. {% endblocktrans %}{% endif %}{% if change_history %}{% blocktrans %}Press the 'Change History' button below to edit the history.{% endblocktrans %}{% endif %}</p>
2222
{% endblock %}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{% load i18n %}
22
<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 %}
3+
{% if not revert_disabled %}
4+
<input type="submit" value="{% trans 'Revert' %}" class="default" name="_save" {{ onclick_attrib }}/>{% endif %}
5+
{% if change_history %}
6+
<input type="submit" value="{% trans 'Change History' %}" class="default" name="_change_history" {{ onclick_attrib }}/>{% endif %}
7+
<a href="{{ history_url }}" class="closelink">{% trans 'Close' %}</a>
58
</div>

simple_history/tests/admin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
FileModel,
1414
Paper,
1515
Person,
16+
Planet,
1617
Poll,
1718
)
1819

@@ -33,6 +34,13 @@ def test_method(self, obj):
3334
history_list_display = ["title", "test_method"]
3435

3536

37+
class PlanetAdmin(SimpleHistoryAdmin):
38+
def test_method(self, obj):
39+
return "test_method_value"
40+
41+
history_list_display = ["title", "test_method"]
42+
43+
3644
admin.site.register(Poll, SimpleHistoryAdmin)
3745
admin.site.register(Choice, ChoiceAdmin)
3846
admin.site.register(Person, PersonAdmin)
@@ -43,3 +51,4 @@ def test_method(self, obj):
4351
admin.site.register(ConcreteExternal, SimpleHistoryAdmin)
4452
admin.site.register(ExternalModelWithCustomUserIdField, SimpleHistoryAdmin)
4553
admin.site.register(FileModel, FileModelAdmin)
54+
admin.site.register(Planet, PlanetAdmin)

0 commit comments

Comments
 (0)