<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> from django . conf import settings <EOL> from django . core . paginator import InvalidPage , Paginator <EOL> from django . http import Http404 <EOL> from django . shortcuts import render <EOL> from haystack . forms import FacetedSearchForm , ModelSearchForm <EOL> from haystack . query import EmptySearchQuerySet <EOL> RESULTS_PER_PAGE = getattr ( settings , '<STR_LIT>' , <NUM_LIT:20> ) <EOL> class SearchView ( object ) : <EOL> template = '<STR_LIT>' <EOL> extra_context = { } <EOL> query = '<STR_LIT>' <EOL> results = EmptySearchQuerySet ( ) <EOL> request = None <EOL> form = None <EOL> results_per_page = RESULTS_PER_PAGE <EOL> def __init__ ( self , template = None , load_all = True , form_class = None , searchqueryset = None , results_per_page = None ) : <EOL> self . load_all = load_all <EOL> self . form_class = form_class <EOL> self . searchqueryset = searchqueryset <EOL> if form_class is None : <EOL> self . form_class = ModelSearchForm <EOL> if not results_per_page is None : <EOL> self . results_per_page = results_per_page <EOL> if template : <EOL> self . template = template <EOL> def __call__ ( self , request ) : <EOL> """<STR_LIT>""" <EOL> self . request = request <EOL> self . form = self . build_form ( ) <EOL> self . query = self . get_query ( ) <EOL> self . results = self . get_results ( ) <EOL> return self . create_response ( ) <EOL> def build_form ( self , form_kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> data = None <EOL> kwargs = { <EOL> '<STR_LIT>' : self . load_all , <EOL> } <EOL> if form_kwargs : <EOL> kwargs . update ( form_kwargs ) <EOL> if len ( self . request . GET ) : <EOL> data = self . request . GET <EOL> if self . searchqueryset is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . searchqueryset <EOL> return self . form_class ( data , ** kwargs ) <EOL> def get_query ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . form . is_valid ( ) : <EOL> return self . form . cleaned_data [ '<STR_LIT:q>' ] <EOL> return '<STR_LIT>' <EOL> def get_results ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . form . search ( ) <EOL> def build_page ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> page_no = int ( self . request . GET . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> except ( TypeError , ValueError ) : <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> if page_no < <NUM_LIT:1> : <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> start_offset = ( page_no - <NUM_LIT:1> ) * self . results_per_page <EOL> self . results [ start_offset : start_offset + self . results_per_page ] <EOL> paginator = Paginator ( self . results , self . results_per_page ) <EOL> try : <EOL> page = paginator . page ( page_no ) <EOL> except InvalidPage : <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> return ( paginator , page ) <EOL> def extra_context ( self ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def get_context ( self ) : <EOL> ( paginator , page ) = self . build_page ( ) <EOL> context = { <EOL> '<STR_LIT>' : self . query , <EOL> '<STR_LIT>' : self . form , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : paginator , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> if self . results and hasattr ( self . results , '<STR_LIT>' ) and self . results . query . backend . include_spelling : <EOL> context [ '<STR_LIT>' ] = self . form . get_suggestion ( ) <EOL> context . update ( self . extra_context ( ) ) <EOL> return context <EOL> def create_response ( self ) : <EOL> """<STR_LIT>""" <EOL> context = self . get_context ( ) <EOL> return render ( self . request , self . template , context ) <EOL> def search_view_factory ( view_class = SearchView , * args , ** kwargs ) : <EOL> def search_view ( request ) : <EOL> return view_class ( * args , ** kwargs ) ( request ) <EOL> return search_view <EOL> class FacetedSearchView ( SearchView ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if kwargs . get ( '<STR_LIT>' ) is None : <EOL> kwargs [ '<STR_LIT>' ] = FacetedSearchForm <EOL> super ( FacetedSearchView , self ) . __init__ ( * args , ** kwargs ) <EOL> def build_form ( self , form_kwargs = None ) : <EOL> if form_kwargs is None : <EOL> form_kwargs = { } <EOL> form_kwargs [ '<STR_LIT>' ] = self . request . GET . getlist ( "<STR_LIT>" ) <EOL> return super ( FacetedSearchView , self ) . build_form ( form_kwargs ) <EOL> def extra_context ( self ) : <EOL> extra = super ( FacetedSearchView , self ) . extra_context ( ) <EOL> extra [ '<STR_LIT>' ] = self . request <EOL> extra [ '<STR_LIT>' ] = self . results . facet_counts ( ) <EOL> return extra <EOL> def basic_search ( request , template = '<STR_LIT>' , load_all = True , form_class = ModelSearchForm , searchqueryset = None , extra_context = None , results_per_page = None ) : <EOL> """<STR_LIT>""" <EOL> query = '<STR_LIT>' <EOL> results = EmptySearchQuerySet ( ) <EOL> if request . GET . get ( '<STR_LIT:q>' ) : <EOL> form = form_class ( request . GET , searchqueryset = searchqueryset , load_all = load_all ) <EOL> if form . is_valid ( ) : <EOL> query = form . cleaned_data [ '<STR_LIT:q>' ] <EOL> results = form . search ( ) <EOL> else : <EOL> form = form_class ( searchqueryset = searchqueryset , load_all = load_all ) <EOL> paginator = Paginator ( results , results_per_page or RESULTS_PER_PAGE ) <EOL> try : <EOL> page = paginator . page ( int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) ) <EOL> except InvalidPage : <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> context = { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : paginator , <EOL> '<STR_LIT>' : query , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> if results . query . backend . include_spelling : <EOL> context [ '<STR_LIT>' ] = form . get_suggestion ( ) <EOL> if extra_context : <EOL> context . update ( extra_context ) <EOL> return render ( request , template , context ) </s>
