Skip to content

Commit 6f0d05e

Browse files
partizaansRoss Mechanic
andauthored
Make history form fields readonly if SIMPLE_HISTORY_EDIT is not set or set to False (#641)
Co-authored-by: Ross Mechanic <[email protected]>
1 parent 184ecdd commit 6f0d05e

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Authors
8282
- Prakash Venkatraman (`dopatraman <https://github.com/dopatraman>`_)
8383
- Rajesh Pappula
8484
- Ray Logel
85+
- Reza Pourmeshki (`partizaans <https://github.com/partizaans>`_)
8586
- Roberto Aguilar
8687
- Rod Xavier Bondoc
8788
- Ross Lote

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+
- Render fields as readonly in history detail view if `SIMPLE_HISTORY_EDIT` is not set
13+
`True`
1214
- settings.SIMPLE_HISTORY_REVERT_DISABLED if True removes the Revert
1315
button from the history form for all historical models (gh-632))
1416

simple_history/admin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ def history_form_view(self, request, object_id, version_id, extra_context=None):
175175
form,
176176
self.get_fieldsets(request, obj),
177177
self.prepopulated_fields,
178-
self.get_readonly_fields(request, obj),
178+
self.get_readonly_fields(request, obj)
179+
if change_history
180+
else self.get_fields(request, obj),
179181
model_admin=self,
180182
)
181183

simple_history/tests/tests/test_admin.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def test_history_form_permission(self):
154154
def test_invalid_history_form(self):
155155
self.login()
156156
poll = Poll.objects.create(question="why?", pub_date=today)
157-
response = self.client.post(get_history_url(poll, 0), data={"question": ""})
157+
with patch("simple_history.admin.SIMPLE_HISTORY_EDIT", True):
158+
response = self.client.post(get_history_url(poll, 0), data={"question": ""})
158159
self.assertEqual(response.status_code, 200)
159160
self.assertContains(response, "This field is required")
160161

@@ -200,6 +201,25 @@ def test_history_form(self):
200201
[p.history_user for p in Poll.history.all()], [self.user, None, None]
201202
)
202203

204+
def test_readonly_history_form_without_setting_simple_history_edit(self):
205+
self.login()
206+
poll = Poll.objects.create(question="why?", pub_date=today)
207+
poll.question = "how?"
208+
poll.save()
209+
response = self.client.get(get_history_url(poll, 0))
210+
readonly_fields = response.context["adminform"].readonly_fields
211+
self.assertCountEqual(["question", "pub_date"], readonly_fields)
212+
213+
def test_readonly_history_form_with_enabled_simple_history_edit(self):
214+
self.login()
215+
poll = Poll.objects.create(question="why?", pub_date=today)
216+
poll.question = "how?"
217+
poll.save()
218+
with patch("simple_history.admin.SIMPLE_HISTORY_EDIT", True):
219+
response = self.client.get(get_history_url(poll, 0))
220+
readonly_fields = response.context["adminform"].readonly_fields
221+
self.assertEqual(0, len(readonly_fields))
222+
203223
def test_history_user_on_save_in_admin(self):
204224
self.login()
205225

0 commit comments

Comments
 (0)