Skip to content

Commit 12ca6c9

Browse files
committed
Expose change reason to admin form
Fixes #853
1 parent 4b22c58 commit 12ca6c9

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Authors
9090
- Lucas Wiman
9191
- Maciej "RooTer" Urbański
9292
- Marcelo Canina (`marcanuy <https://github.com/marcanuy>`_)
93+
- Marco Sirabella
9394
- Mark Davidoff
9495
- Martin Bachwerk
9596
- Marty Alchin

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Unreleased
2424
``HistoricalRecords.context.request``) under some circumstances (gh-1188)
2525
- Made ``HistoryRequestMiddleware`` async-capable (gh-1209)
2626
- Fixed error when setting ``table_name`` with ``inherit=True`` (gh-1195)
27+
- Allow setting change reason on admin panel modifications (gh-??)
2728

2829
3.3.0 (2023-03-08)
2930
------------------

simple_history/admin.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from django import http
1+
from django import forms, http
22
from django.apps import apps as django_apps
33
from django.conf import settings
44
from django.contrib import admin
55
from django.contrib.admin import helpers
66
from django.contrib.admin.utils import unquote
77
from django.contrib.auth import get_permission_codename, get_user_model
88
from django.core.exceptions import PermissionDenied
9+
from django.forms import fields
910
from django.shortcuts import get_object_or_404, render
1011
from django.urls import re_path, reverse
1112
from django.utils.encoding import force_str
@@ -224,9 +225,31 @@ def render_history_view(self, request, template, context, **kwargs):
224225

225226
def save_model(self, request, obj, form, change):
226227
"""Set special model attribute to user for reference after save"""
228+
obj._change_reason = form.cleaned_data["change_reason"]
227229
obj._history_user = request.user
228230
super().save_model(request, obj, form, change)
229231

232+
class form(forms.ModelForm):
233+
change_reason = forms.CharField(required=False)
234+
235+
def get_fields(self, request, obj=None):
236+
return [
237+
field
238+
for field in super().get_fields(request, obj)
239+
if field != "change_reason"
240+
]
241+
242+
def get_fieldsets(self, request, obj=None):
243+
return super().get_fieldsets(request, obj) + [
244+
(
245+
"History",
246+
{
247+
"classes": ["collapse"],
248+
"fields": ["change_reason"],
249+
},
250+
)
251+
]
252+
230253
@property
231254
def content_type_model_cls(self):
232255
"""Returns the ContentType model class."""

simple_history/tests/tests/test_admin.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,20 @@ def test_history_user_on_save_in_admin(self):
218218
[p.history_user for p in Poll.history.all()], [self.user, self.user]
219219
)
220220

221+
def test_history_change_reason_on_save_in_admin(self):
222+
self.login()
223+
224+
change_reason = "New change reason"
225+
# Ensure polls created via admin interface save correct user
226+
poll_data = {
227+
"question": "new poll?",
228+
"pub_date_0": "2012-01-01",
229+
"pub_date_1": "10:00:00",
230+
"change_reason": change_reason,
231+
}
232+
self.client.post(reverse("admin:tests_poll_add"), data=poll_data)
233+
self.assertEqual(Poll.history.get().history_change_reason, change_reason)
234+
221235
def test_underscore_in_pk(self):
222236
self.login()
223237
book = Book(isbn="9780147_513731")

0 commit comments

Comments
 (0)