Skip to content

Commit

Permalink
By default Djangos AdminSite object adds the value from each_context …
Browse files Browse the repository at this point in the history
…into every view. Update the "index" and "view" views to also receive this context.
  • Loading branch information
Ben Hastings authored and kezabelle committed Jun 23, 2018
1 parent 2a189b9 commit a98206e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 15 additions & 0 deletions haystackbrowser/admin.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions haystackbrowser/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit a98206e

Please sign in to comment.