<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> from django . contrib . admin . options import ModelAdmin , csrf_protect_m <EOL> from django . contrib . admin . views . main import SEARCH_VAR , ChangeList <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . paginator import InvalidPage , Paginator <EOL> from django . shortcuts import render <EOL> from django . utils . encoding import force_text <EOL> from django . utils . translation import ungettext <EOL> from haystack import connections <EOL> from haystack . query import SearchQuerySet <EOL> from haystack . utils import get_model_ct_tuple <EOL> def list_max_show_all ( changelist ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from django . contrib . admin . views . main import MAX_SHOW_ALL_ALLOWED <EOL> return MAX_SHOW_ALL_ALLOWED <EOL> except ImportError : <EOL> return changelist . list_max_show_all <EOL> class SearchChangeList ( ChangeList ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . haystack_connection = kwargs . pop ( '<STR_LIT>' , '<STR_LIT:default>' ) <EOL> super ( SearchChangeList , self ) . __init__ ( ** kwargs ) <EOL> def get_results ( self , request ) : <EOL> if not SEARCH_VAR in request . GET : <EOL> return super ( SearchChangeList , self ) . get_results ( request ) <EOL> sqs = SearchQuerySet ( self . haystack_connection ) . models ( self . model ) . auto_query ( request . GET [ SEARCH_VAR ] ) . load_all ( ) <EOL> paginator = Paginator ( sqs , self . list_per_page ) <EOL> result_count = paginator . count <EOL> full_result_count = SearchQuerySet ( self . haystack_connection ) . models ( self . model ) . all ( ) . count ( ) <EOL> can_show_all = result_count <= list_max_show_all ( self ) <EOL> multi_page = result_count > self . list_per_page <EOL> try : <EOL> result_list = paginator . page ( self . page_num + <NUM_LIT:1> ) . object_list <EOL> result_list = [ result . object for result in result_list ] <EOL> except InvalidPage : <EOL> result_list = ( ) <EOL> self . result_count = result_count <EOL> self . full_result_count = full_result_count <EOL> self . result_list = result_list <EOL> self . can_show_all = can_show_all <EOL> self . multi_page = multi_page <EOL> self . paginator = paginator <EOL> class SearchModelAdminMixin ( object ) : <EOL> haystack_connection = '<STR_LIT:default>' <EOL> @ csrf_protect_m <EOL> def changelist_view ( self , request , extra_context = None ) : <EOL> if not self . has_change_permission ( request , None ) : <EOL> raise PermissionDenied <EOL> if not SEARCH_VAR in request . GET : <EOL> return super ( SearchModelAdminMixin , self ) . changelist_view ( request , extra_context ) <EOL> if not self . model in connections [ self . haystack_connection ] . get_unified_index ( ) . get_indexed_models ( ) : <EOL> return super ( SearchModelAdminMixin , self ) . changelist_view ( request , extra_context ) <EOL> list_display = list ( self . list_display ) <EOL> kwargs = { <EOL> '<STR_LIT>' : self . haystack_connection , <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : self . model , <EOL> '<STR_LIT>' : list_display , <EOL> '<STR_LIT>' : self . list_display_links , <EOL> '<STR_LIT>' : self . list_filter , <EOL> '<STR_LIT>' : self . date_hierarchy , <EOL> '<STR_LIT>' : self . search_fields , <EOL> '<STR_LIT>' : self . list_select_related , <EOL> '<STR_LIT>' : self . list_per_page , <EOL> '<STR_LIT>' : self . list_editable , <EOL> '<STR_LIT>' : self <EOL> } <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT>' ] = self . list_max_show_all <EOL> changelist = SearchChangeList ( ** kwargs ) <EOL> formset = changelist . formset = None <EOL> media = self . media <EOL> actions = self . get_actions ( request ) <EOL> if actions : <EOL> action_form = self . action_form ( auto_id = None ) <EOL> action_form . fields [ '<STR_LIT:action>' ] . choices = self . get_action_choices ( request ) <EOL> else : <EOL> action_form = None <EOL> selection_note = ungettext ( '<STR_LIT>' , <EOL> '<STR_LIT>' , len ( changelist . result_list ) ) <EOL> selection_note_all = ungettext ( '<STR_LIT>' , <EOL> '<STR_LIT>' , changelist . result_count ) <EOL> context = { <EOL> '<STR_LIT>' : force_text ( self . model . _meta . verbose_name_plural ) , <EOL> '<STR_LIT>' : selection_note % { '<STR_LIT:count>' : len ( changelist . result_list ) } , <EOL> '<STR_LIT>' : selection_note_all % { '<STR_LIT>' : changelist . result_count } , <EOL> '<STR_LIT:title>' : changelist . title , <EOL> '<STR_LIT>' : changelist . is_popup , <EOL> '<STR_LIT>' : changelist , <EOL> '<STR_LIT>' : media , <EOL> '<STR_LIT>' : self . has_add_permission ( request ) , <EOL> '<STR_LIT>' : getattr ( self . admin_site , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : self . model . _meta . app_label , <EOL> '<STR_LIT>' : action_form , <EOL> '<STR_LIT>' : self . actions_on_top , <EOL> '<STR_LIT>' : self . actions_on_bottom , <EOL> '<STR_LIT>' : getattr ( self , '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> } <EOL> context . update ( extra_context or { } ) <EOL> request . current_app = self . admin_site . name <EOL> app_name , model_name = get_model_ct_tuple ( self . model ) <EOL> return render ( request , self . change_list_template or [ <EOL> '<STR_LIT>' % ( app_name , model_name ) , <EOL> '<STR_LIT>' % app_name , <EOL> '<STR_LIT>' <EOL> ] , context ) <EOL> class SearchModelAdmin ( SearchModelAdminMixin , ModelAdmin ) : <EOL> pass </s>
