From a98206efcd20b81a5a45cd722657ae2b7b345c87 Mon Sep 17 00:00:00 2001 From: Ben Hastings Date: Thu, 11 Jan 2018 08:33:04 +0000 Subject: [PATCH] By default Djangos AdminSite object adds the value from each_context into every view. Update the "index" and "view" views to also receive this context. --- haystackbrowser/admin.py | 15 +++++++++++++++ haystackbrowser/test_admin.py | 12 ++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/haystackbrowser/admin.py b/haystackbrowser/admin.py index d779aa0..3dde16b 100644 --- a/haystackbrowser/admin.py +++ b/haystackbrowser/admin.py @@ -1,4 +1,5 @@ import logging +from inspect import getargspec from django.core.exceptions import PermissionDenied from django.core.paginator import Paginator, InvalidPage from haystack.exceptions import SearchBackendError @@ -289,6 +290,16 @@ def do_render(self, request, template_name, context): return render_to_response(template_name=template_name, context=context, context_instance=RequestContext(request)) + def each_context_compat(self, request): + # Django didn't always have an AdminSite.each_context method. + if not hasattr(self.admin_site, 'each_context'): + return {} + method_sig = getargspec(self.admin_site.each_context) + # Django didn't always pass along request. + if 'request' in method_sig.args: + return self.admin_site.each_context(request) + return self.admin_site.each_context() + def index(self, request): """The view for showing all the results in the Haystack index. Emulates the standard Django ChangeList mostly. @@ -388,6 +399,8 @@ def index(self, request): # See #1 (https://github.com/kezabelle/django-haystackbrowser/pull/1) 'media': Media() } + # Update the context with variables that should be available to every page + context.update(self.each_context_compat(request)) return self.do_render(request=request, template_name='admin/haystackbrowser/result_list.html', context=context) @@ -454,6 +467,8 @@ def view(self, request, content_type, pk): 'form': form, 'form_valid': form_valid, } + # Update the context with variables that should be available to every page + context.update(self.each_context_compat(request)) return self.do_render(request=request, template_name='admin/haystackbrowser/view.html', context=context) diff --git a/haystackbrowser/test_admin.py b/haystackbrowser/test_admin.py index 1e16cb9..0e0198c 100644 --- a/haystackbrowser/test_admin.py +++ b/haystackbrowser/test_admin.py @@ -3,6 +3,7 @@ from __future__ import print_function from __future__ import unicode_literals from __future__ import division +import django import pytest from functools import partial from django.conf import settings @@ -63,7 +64,8 @@ def test_detailview_has_view_result_templateresponse(mocker, detailview): from django.template.response import TemplateResponse assert isinstance(response, TemplateResponse) is True assert response.status_code == 200 - assert sorted(response.context_data.keys()) == [ + context_keys = set(response.context_data.keys()) + assert context_keys.issuperset({ 'app_label', 'form', 'form_valid', @@ -74,7 +76,13 @@ def test_detailview_has_view_result_templateresponse(mocker, detailview): 'original', 'similar_objects', 'title' - ] + }) + if django.VERSION[0:2] >= (1, 7): + assert 'site_header' in context_keys + assert 'site_title' in context_keys + else: + assert 'site_header' not in context_keys + assert 'site_title' not in context_keys assert response.context_data['form_valid'] is False assert response.context_data['has_change_permission'] is True assert len(response.context_data['similar_objects']) == 2