Skip to content

Commit

Permalink
Make history form fields readonly if SIMPLE_HISTORY_EDIT is not set…
Browse files Browse the repository at this point in the history
… or set to False (#641)

Co-authored-by: Ross Mechanic <ross.mechanic@icloud.com>
  • Loading branch information
partizaans and Ross Mechanic committed Apr 23, 2020
1 parent 184ecdd commit 6f0d05e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Authors
- Prakash Venkatraman (`dopatraman <https://github.com/dopatraman>`_)
- Rajesh Pappula
- Ray Logel
- Reza Pourmeshki (`partizaans <https://github.com/partizaans>`_)
- Roberto Aguilar
- Rod Xavier Bondoc
- Ross Lote
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Unreleased
- import model `ContentType` in `SimpleHistoryAdmin` using `django_apps.get_model`
to avoid possible `AppRegistryNotReady` exception (gh-630)
- Fix `utils.update_change_reason` when user specifies excluded_fields
- Render fields as readonly in history detail view if `SIMPLE_HISTORY_EDIT` is not set
`True`
- settings.SIMPLE_HISTORY_REVERT_DISABLED if True removes the Revert
button from the history form for all historical models (gh-632))

Expand Down
4 changes: 3 additions & 1 deletion simple_history/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ def history_form_view(self, request, object_id, version_id, extra_context=None):
form,
self.get_fieldsets(request, obj),
self.prepopulated_fields,
self.get_readonly_fields(request, obj),
self.get_readonly_fields(request, obj)
if change_history
else self.get_fields(request, obj),
model_admin=self,
)

Expand Down
22 changes: 21 additions & 1 deletion simple_history/tests/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def test_history_form_permission(self):
def test_invalid_history_form(self):
self.login()
poll = Poll.objects.create(question="why?", pub_date=today)
response = self.client.post(get_history_url(poll, 0), data={"question": ""})
with patch("simple_history.admin.SIMPLE_HISTORY_EDIT", True):
response = self.client.post(get_history_url(poll, 0), data={"question": ""})
self.assertEqual(response.status_code, 200)
self.assertContains(response, "This field is required")

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

def test_readonly_history_form_without_setting_simple_history_edit(self):
self.login()
poll = Poll.objects.create(question="why?", pub_date=today)
poll.question = "how?"
poll.save()
response = self.client.get(get_history_url(poll, 0))
readonly_fields = response.context["adminform"].readonly_fields
self.assertCountEqual(["question", "pub_date"], readonly_fields)

def test_readonly_history_form_with_enabled_simple_history_edit(self):
self.login()
poll = Poll.objects.create(question="why?", pub_date=today)
poll.question = "how?"
poll.save()
with patch("simple_history.admin.SIMPLE_HISTORY_EDIT", True):
response = self.client.get(get_history_url(poll, 0))
readonly_fields = response.context["adminform"].readonly_fields
self.assertEqual(0, len(readonly_fields))

def test_history_user_on_save_in_admin(self):
self.login()

Expand Down

0 comments on commit 6f0d05e

Please sign in to comment.