Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra context to admin #499

Merged
merged 1 commit into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ Authors
- Ross Lote
- Ross Mechanic (`rossmechanic <https://github.com/rossmechanic>`_)
- Ross Rogers
- Sergey Ozeranskiy (`ozeranskiy <https://github.com/ozeranskiy>`_)
- Shane Engelman
- Steven Klass
- Steeve Chailloux
- Steven Klass
- Tommy Beadle (`tbeadle <https://github.com/tbeadle>`_)
- Trey Hunner (`treyhunner <https://github.com/treyhunner>`_)
- Ulysses Vilela
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
3 changes: 2 additions & 1 deletion simple_history/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions simple_history/tests/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)