From addbf63fa41423b405c6d3718151e1550852a7c8 Mon Sep 17 00:00:00 2001 From: Sergey Date: Mon, 29 Oct 2018 17:04:24 +0300 Subject: [PATCH] Pass extra_context to history_form_view --- AUTHORS.rst | 3 +- CHANGES.rst | 1 + simple_history/admin.py | 3 +- simple_history/tests/tests/test_admin.py | 57 ++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 6ef8534e6..93db638bb 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -73,9 +73,10 @@ Authors - Ross Lote - Ross Mechanic (`rossmechanic `_) - Ross Rogers +- Sergey Ozeranskiy (`ozeranskiy `_) - Shane Engelman -- Steven Klass - Steeve Chailloux +- Steven Klass - Tommy Beadle (`tbeadle `_) - Trey Hunner (`treyhunner `_) - Ulysses Vilela diff --git a/CHANGES.rst b/CHANGES.rst index 782ca5c7d..fa49f6161 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,6 +10,7 @@ Unreleased - Raise warning if HistoricalRecords(inherit=False) is in an abstract model (gh-341) - Ensure custom arguments for fields are included in historical models' fields (gh-431) - Add german translations +- Add `extra_context` parameter to history_form_view (gh-467) 2.5.1 (2018-10-19) ------------------ diff --git a/simple_history/admin.py b/simple_history/admin.py index 870c00ade..e8d805d8f 100644 --- a/simple_history/admin.py +++ b/simple_history/admin.py @@ -109,7 +109,7 @@ def response_change(self, request, obj): else: return super(SimpleHistoryAdmin, self).response_change(request, obj) - def history_form_view(self, request, object_id, version_id): + def history_form_view(self, request, object_id, version_id, extra_context=None): request.current_app = self.admin_site.name original_opts = self.model._meta model = getattr( @@ -190,6 +190,7 @@ def history_form_view(self, request, object_id, version_id): "root_path": getattr(self.admin_site, "root_path", None), } context.update(self.admin_site.each_context(request)) + context.update(extra_context or {}) extra_kwargs = {} return render( request, self.object_history_form_template, context, **extra_kwargs diff --git a/simple_history/tests/tests/test_admin.py b/simple_history/tests/tests/test_admin.py index 5ea2ca127..4881cee74 100644 --- a/simple_history/tests/tests/test_admin.py +++ b/simple_history/tests/tests/test_admin.py @@ -630,3 +630,60 @@ def test_history_form_view_getting_history_abstract_external(self): mock_render.assert_called_once_with( request, admin.object_history_form_template, context ) + + def test_history_form_view_accepts_additional_context(self): + request = RequestFactory().post("/") + request.session = "session" + request._messages = FallbackStorage(request) + request.user = self.user + + poll = Poll.objects.create(question="why?", pub_date=today) + poll.question = "how?" + poll.save() + history = poll.history.all()[0] + + admin_site = AdminSite() + admin = SimpleHistoryAdmin(Poll, admin_site) + + with patch("simple_history.admin.render") as mock_render: + admin.history_form_view( + request, + poll.id, + history.pk, + extra_context={"anything_else": "will be merged into context"}, + ) + + context = { + # Verify this is set for original object + "anything_else": "will be merged into context", + "original": poll, + "change_history": False, + "title": "Revert %s" % force_text(poll), + "adminform": ANY, + "object_id": poll.id, + "is_popup": False, + "media": ANY, + "errors": ANY, + "app_label": "tests", + "original_opts": ANY, + "changelist_url": "/admin/tests/poll/", + "change_url": ANY, + "history_url": "/admin/tests/poll/1/history/", + "add": False, + "change": True, + "has_add_permission": admin.has_add_permission(request), + "has_change_permission": admin.has_change_permission(request, poll), + "has_delete_permission": admin.has_delete_permission(request, poll), + "has_file_field": True, + "has_absolute_url": False, + "form_url": "", + "opts": ANY, + "content_type_id": ANY, + "save_as": admin.save_as, + "save_on_top": admin.save_on_top, + "root_path": getattr(admin_site, "root_path", None), + } + context.update(admin_site.each_context(request)) + mock_render.assert_called_once_with( + request, admin.object_history_form_template, context + )