Skip to content

Commit 5863d95

Browse files
authored
Pass extra_context to history_form_view (#499)
1 parent 4c6c7bd commit 5863d95

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

AUTHORS.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ Authors
7373
- Ross Lote
7474
- Ross Mechanic (`rossmechanic <https://github.com/rossmechanic>`_)
7575
- Ross Rogers
76+
- Sergey Ozeranskiy (`ozeranskiy <https://github.com/ozeranskiy>`_)
7677
- Shane Engelman
77-
- Steven Klass
7878
- Steeve Chailloux
79+
- Steven Klass
7980
- Tommy Beadle (`tbeadle <https://github.com/tbeadle>`_)
8081
- Trey Hunner (`treyhunner <https://github.com/treyhunner>`_)
8182
- Ulysses Vilela

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Unreleased
1010
- Raise warning if HistoricalRecords(inherit=False) is in an abstract model (gh-341)
1111
- Ensure custom arguments for fields are included in historical models' fields (gh-431)
1212
- Add german translations
13+
- Add `extra_context` parameter to history_form_view (gh-467)
1314

1415
2.5.1 (2018-10-19)
1516
------------------

simple_history/admin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def response_change(self, request, obj):
109109
else:
110110
return super(SimpleHistoryAdmin, self).response_change(request, obj)
111111

112-
def history_form_view(self, request, object_id, version_id):
112+
def history_form_view(self, request, object_id, version_id, extra_context=None):
113113
request.current_app = self.admin_site.name
114114
original_opts = self.model._meta
115115
model = getattr(
@@ -190,6 +190,7 @@ def history_form_view(self, request, object_id, version_id):
190190
"root_path": getattr(self.admin_site, "root_path", None),
191191
}
192192
context.update(self.admin_site.each_context(request))
193+
context.update(extra_context or {})
193194
extra_kwargs = {}
194195
return render(
195196
request, self.object_history_form_template, context, **extra_kwargs

simple_history/tests/tests/test_admin.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,60 @@ def test_history_form_view_getting_history_abstract_external(self):
630630
mock_render.assert_called_once_with(
631631
request, admin.object_history_form_template, context
632632
)
633+
634+
def test_history_form_view_accepts_additional_context(self):
635+
request = RequestFactory().post("/")
636+
request.session = "session"
637+
request._messages = FallbackStorage(request)
638+
request.user = self.user
639+
640+
poll = Poll.objects.create(question="why?", pub_date=today)
641+
poll.question = "how?"
642+
poll.save()
643+
history = poll.history.all()[0]
644+
645+
admin_site = AdminSite()
646+
admin = SimpleHistoryAdmin(Poll, admin_site)
647+
648+
with patch("simple_history.admin.render") as mock_render:
649+
admin.history_form_view(
650+
request,
651+
poll.id,
652+
history.pk,
653+
extra_context={"anything_else": "will be merged into context"},
654+
)
655+
656+
context = {
657+
# Verify this is set for original object
658+
"anything_else": "will be merged into context",
659+
"original": poll,
660+
"change_history": False,
661+
"title": "Revert %s" % force_text(poll),
662+
"adminform": ANY,
663+
"object_id": poll.id,
664+
"is_popup": False,
665+
"media": ANY,
666+
"errors": ANY,
667+
"app_label": "tests",
668+
"original_opts": ANY,
669+
"changelist_url": "/admin/tests/poll/",
670+
"change_url": ANY,
671+
"history_url": "/admin/tests/poll/1/history/",
672+
"add": False,
673+
"change": True,
674+
"has_add_permission": admin.has_add_permission(request),
675+
"has_change_permission": admin.has_change_permission(request, poll),
676+
"has_delete_permission": admin.has_delete_permission(request, poll),
677+
"has_file_field": True,
678+
"has_absolute_url": False,
679+
"form_url": "",
680+
"opts": ANY,
681+
"content_type_id": ANY,
682+
"save_as": admin.save_as,
683+
"save_on_top": admin.save_on_top,
684+
"root_path": getattr(admin_site, "root_path", None),
685+
}
686+
context.update(admin_site.each_context(request))
687+
mock_render.assert_called_once_with(
688+
request, admin.object_history_form_template, context
689+
)

0 commit comments

Comments
 (0)