<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> from django . conf import settings <EOL> from django . core . paginator import Paginator <EOL> from django . views . generic import FormView <EOL> from django . views . generic . edit import FormMixin <EOL> from django . views . generic . list import MultipleObjectMixin <EOL> from . forms import FacetedSearchForm , ModelSearchForm <EOL> from . query import SearchQuerySet <EOL> RESULTS_PER_PAGE = getattr ( settings , '<STR_LIT>' , <NUM_LIT:20> ) <EOL> class SearchMixin ( MultipleObjectMixin , FormMixin ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> load_all = True <EOL> form_class = ModelSearchForm <EOL> queryset = SearchQuerySet ( ) <EOL> context_object_name = None <EOL> paginate_by = RESULTS_PER_PAGE <EOL> paginate_orphans = <NUM_LIT:0> <EOL> paginator_class = Paginator <EOL> page_kwarg = '<STR_LIT>' <EOL> form_name = '<STR_LIT>' <EOL> search_field = '<STR_LIT:q>' <EOL> object_list = None <EOL> def get_form_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { '<STR_LIT>' : self . get_initial ( ) } <EOL> if self . request . method == '<STR_LIT:GET>' and self . request . GET : <EOL> kwargs . update ( { <EOL> '<STR_LIT:data>' : self . request . GET , <EOL> } ) <EOL> kwargs . update ( { '<STR_LIT>' : self . get_queryset ( ) } ) <EOL> return kwargs <EOL> def form_invalid ( self , form ) : <EOL> context = self . get_context_data ( ** { <EOL> self . form_name : form , <EOL> '<STR_LIT>' : self . get_queryset ( ) <EOL> } ) <EOL> return self . render_to_response ( context ) <EOL> def form_valid ( self , form ) : <EOL> self . queryset = form . search ( ) <EOL> context = self . get_context_data ( ** { <EOL> self . form_name : form , <EOL> '<STR_LIT>' : form . cleaned_data . get ( self . search_field ) , <EOL> '<STR_LIT>' : self . queryset <EOL> } ) <EOL> return self . render_to_response ( context ) <EOL> class FacetedSearchMixin ( SearchMixin ) : <EOL> """<STR_LIT>""" <EOL> form_class = FacetedSearchForm <EOL> facet_fields = None <EOL> def get_form_kwargs ( self ) : <EOL> kwargs = super ( FacetedSearchMixin , self ) . get_form_kwargs ( ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : self . request . GET . getlist ( "<STR_LIT>" ) <EOL> } ) <EOL> return kwargs <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( FacetedSearchMixin , self ) . get_context_data ( ** kwargs ) <EOL> context . update ( { '<STR_LIT>' : self . queryset . facet_counts ( ) } ) <EOL> return context <EOL> def get_queryset ( self ) : <EOL> qs = super ( FacetedSearchMixin , self ) . get_queryset ( ) <EOL> for field in self . facet_fields : <EOL> qs = qs . facet ( field ) <EOL> return qs <EOL> class SearchView ( SearchMixin , FormView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> form_class = self . get_form_class ( ) <EOL> form = self . get_form ( form_class ) <EOL> if form . is_valid ( ) : <EOL> return self . form_valid ( form ) <EOL> else : <EOL> return self . form_invalid ( form ) <EOL> class FacetedSearchView ( FacetedSearchMixin , SearchView ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
