From 8fb2af7ddb7b79e2c1e3b9cc9ef3146ad74108b0 Mon Sep 17 00:00:00 2001 From: Daniel Lindsley Date: Tue, 19 Jun 2012 00:30:02 -0500 Subject: [PATCH] Fixed Django 1.4 compatibility. Thanks to bloodchild for the report! --- haystack/admin.py | 53 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/haystack/admin.py b/haystack/admin.py index f018aa8ef..859228424 100644 --- a/haystack/admin.py +++ b/haystack/admin.py @@ -12,13 +12,13 @@ from django.contrib.admin.options import csrf_protect_m except ImportError: from haystack.utils.decorators import method_decorator - + # Do nothing on Django 1.1 and below. def csrf_protect(view): def wraps(request, *args, **kwargs): return view(request, *args, **kwargs) return wraps - + csrf_protect_m = method_decorator(csrf_protect) def list_max_show_all(changelist): @@ -39,18 +39,18 @@ class SearchChangeList(ChangeList): def get_results(self, request): if not SEARCH_VAR in request.GET: return super(SearchChangeList, self).get_results(request) - + # Note that pagination is 0-based, not 1-based. sqs = SearchQuerySet().models(self.model).auto_query(request.GET[SEARCH_VAR]).load_all() - + paginator = Paginator(sqs, self.list_per_page) # Get the number of objects, with admin filters applied. result_count = paginator.count full_result_count = SearchQuerySet().models(self.model).all().count() - + can_show_all = result_count <= list_max_show_all(self) multi_page = result_count > self.list_per_page - + # Get the list of objects to display on this page. try: result_list = paginator.page(self.page_num+1).object_list @@ -59,7 +59,7 @@ def get_results(self, request): result_list = [result.object for result in result_list] except InvalidPage: result_list = () - + self.result_count = result_count self.full_result_count = full_result_count self.result_list = result_list @@ -73,26 +73,44 @@ class SearchModelAdmin(ModelAdmin): def changelist_view(self, request, extra_context=None): if not self.has_change_permission(request, None): raise PermissionDenied - + if not SEARCH_VAR in request.GET: # Do the usual song and dance. return super(SearchModelAdmin, self).changelist_view(request, extra_context) - + # Do a search of just this model and populate a Changelist with the # returned bits. if not self.model in connections['default'].get_unified_index().get_indexed_models(): # Oops. That model isn't being indexed. Return the usual # behavior instead. return super(SearchModelAdmin, self).changelist_view(request, extra_context) - + # So. Much. Boilerplate. # Why copy-paste a few lines when you can copy-paste TONS of lines? list_display = list(self.list_display) - - changelist = SearchChangeList(request, self.model, list_display, self.list_display_links, self.list_filter, self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_editable, self) + + kwargs = { + 'request': request, + 'model': self.model, + 'list_display': list_display, + 'list_display_links': self.list_display_links, + 'list_filter': self.list_filter, + 'date_hierarchy': self.date_hierarchy, + 'search_fields': self.search_fields, + 'list_select_related': self.list_select_related, + 'list_per_page': self.list_per_page, + 'list_editable': self.list_editable, + 'model_admin': self + } + + # Django 1.4 compatibility. + if hasattr(self, 'list_max_show_all'): + kwargs['list_max_show_all'] = self.list_max_show_all + + changelist = SearchChangeList(**kwargs) formset = changelist.formset = None media = self.media - + # Build the action form and populate it with available actions. # Check actions to see if any are available on this changelist actions = self.get_actions(request) @@ -101,12 +119,12 @@ def changelist_view(self, request, extra_context=None): action_form.fields['action'].choices = self.get_action_choices(request) else: action_form = None - + selection_note = ungettext('0 of %(count)d selected', 'of %(count)d selected', len(changelist.result_list)) selection_note_all = ungettext('%(total_count)s selected', 'All %(total_count)s selected', changelist.result_count) - + context = { 'module_name': force_unicode(self.model._meta.verbose_name_plural), 'selection_note': selection_note % {'count': len(changelist.result_list)}, @@ -116,7 +134,8 @@ def changelist_view(self, request, extra_context=None): 'cl': changelist, 'media': media, 'has_add_permission': self.has_add_permission(request), - 'root_path': self.admin_site.root_path, + # More Django 1.4 compatibility + 'root_path': getattr(self.admin_site, 'root_path', None), 'app_label': self.model._meta.app_label, 'action_form': action_form, 'actions_on_top': self.actions_on_top, @@ -130,4 +149,4 @@ def changelist_view(self, request, extra_context=None): 'admin/%s/change_list.html' % self.model._meta.app_label, 'admin/change_list.html' ], context, context_instance=context_instance) - +