<s> import json <EOL> import logging <EOL> import time <EOL> from datetime import datetime , timedelta <EOL> from itertools import chain <EOL> from django . conf import settings <EOL> from django . http import ( <EOL> HttpResponse , HttpResponseBadRequest , HttpResponseRedirect ) <EOL> from django . shortcuts import render , render_to_response <EOL> from django . utils . html import escape <EOL> from django . utils . http import urlquote <EOL> from django . utils . translation import ugettext as _ , pgettext , pgettext_lazy <EOL> from django . views . decorators . cache import cache_page <EOL> import bleach <EOL> import jinja2 <EOL> from elasticutils . utils import format_explanation <EOL> from elasticutils . contrib . django import ES_EXCEPTIONS <EOL> from mobility . decorators import mobile_template <EOL> from kitsune import search as constants <EOL> from kitsune . forums . models import Forum , ThreadMappingType <EOL> from kitsune . products . models import Product <EOL> from kitsune . questions . models import QuestionMappingType <EOL> from kitsune . search . utils import locale_or_default , clean_excerpt <EOL> from kitsune . search import es_utils <EOL> from kitsune . search . forms import SimpleSearchForm , AdvancedSearchForm <EOL> from kitsune . search . es_utils import F , AnalyzerS , handle_es_errors <EOL> from kitsune . search . search_utils import apply_boosts , generate_simple_search <EOL> from kitsune . sumo . api_utils import JSONRenderer <EOL> from kitsune . sumo . templatetags . jinja_helpers import Paginator <EOL> from kitsune . sumo . json_utils import markup_json <EOL> from kitsune . sumo . urlresolvers import reverse <EOL> from kitsune . sumo . utils import paginate <EOL> from kitsune . wiki . facets import documents_for <EOL> from kitsune . wiki . models import DocumentMappingType <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> EXCERPT_JOINER = pgettext_lazy ( '<STR_LIT>' , u'<STR_LIT>' ) <EOL> def cache_control ( resp , cache_period ) : <EOL> """<STR_LIT>""" <EOL> resp [ '<STR_LIT>' ] = '<STR_LIT>' % ( cache_period * <NUM_LIT> ) <EOL> resp [ '<STR_LIT>' ] = ( <EOL> ( datetime . utcnow ( ) + timedelta ( minutes = cache_period ) ) <EOL> . strftime ( '<STR_LIT>' ) ) <EOL> return resp <EOL> def _es_down_template ( request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' if request . MOBILE else '<STR_LIT>' <EOL> class UnknownDocType ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def build_results_list ( pages , is_json ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> for rank , doc in enumerate ( pages , pages . start_index ( ) ) : <EOL> if doc [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> summary = _build_es_excerpt ( doc ) <EOL> if not summary : <EOL> summary = doc [ '<STR_LIT>' ] <EOL> result = { <EOL> '<STR_LIT:title>' : doc [ '<STR_LIT>' ] , <EOL> '<STR_LIT:type>' : '<STR_LIT>' } <EOL> elif doc [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> summary = _build_es_excerpt ( doc ) <EOL> if not summary : <EOL> summary = bleach . clean ( doc [ '<STR_LIT>' ] , strip = True ) [ : <NUM_LIT> ] <EOL> result = { <EOL> '<STR_LIT:title>' : doc [ '<STR_LIT>' ] , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : datetime . fromtimestamp ( doc [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : doc [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : doc [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : doc [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : doc [ '<STR_LIT>' ] } <EOL> elif doc [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> summary = _build_es_excerpt ( doc , first_only = True ) <EOL> result = { <EOL> '<STR_LIT:title>' : doc [ '<STR_LIT>' ] , <EOL> '<STR_LIT:type>' : '<STR_LIT>' } <EOL> else : <EOL> raise UnknownDocType ( '<STR_LIT>' % doc [ '<STR_LIT>' ] ) <EOL> result [ '<STR_LIT:url>' ] = doc [ '<STR_LIT:url>' ] <EOL> if not is_json : <EOL> result [ '<STR_LIT:object>' ] = doc <EOL> result [ '<STR_LIT>' ] = summary <EOL> result [ '<STR_LIT>' ] = rank <EOL> result [ '<STR_LIT>' ] = doc . es_meta . score <EOL> result [ '<STR_LIT>' ] = escape ( format_explanation ( doc . es_meta . explanation ) ) <EOL> result [ '<STR_LIT:id>' ] = doc [ '<STR_LIT:id>' ] <EOL> results . append ( result ) <EOL> return results <EOL> @ markup_json <EOL> @ handle_es_errors ( _es_down_template ) <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> def simple_search ( request , template = None ) : <EOL> """<STR_LIT>""" <EOL> to_json = JSONRenderer ( ) . render <EOL> if request . GET . get ( '<STR_LIT:a>' ) in [ '<STR_LIT:1>' , '<STR_LIT:2>' ] : <EOL> new_url = reverse ( '<STR_LIT>' ) + '<STR_LIT:?>' + request . GET . urlencode ( ) <EOL> return HttpResponseRedirect ( new_url ) <EOL> search_form = SimpleSearchForm ( request . GET , auto_id = False ) <EOL> if not search_form . is_valid ( ) : <EOL> if request . IS_JSON : <EOL> return HttpResponse ( <EOL> json . dumps ( { '<STR_LIT:error>' : _ ( '<STR_LIT>' ) } ) , <EOL> content_type = request . CONTENT_TYPE , <EOL> status = <NUM_LIT> ) <EOL> t = template if request . MOBILE else '<STR_LIT>' <EOL> return cache_control ( <EOL> render ( request , t , { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : search_form } ) , <EOL> settings . SEARCH_CACHE_PERIOD ) <EOL> cleaned = search_form . cleaned_data <EOL> if request . MOBILE and cleaned [ '<STR_LIT:w>' ] == constants . WHERE_BASIC : <EOL> cleaned [ '<STR_LIT:w>' ] = constants . WHERE_WIKI <EOL> language = locale_or_default ( cleaned [ '<STR_LIT>' ] or request . LANGUAGE_CODE ) <EOL> lang_name = settings . LANGUAGES_DICT . get ( language . lower ( ) ) or '<STR_LIT>' <EOL> searcher = generate_simple_search ( search_form , language , with_highlights = True ) <EOL> searcher = searcher [ : settings . SEARCH_MAX_RESULTS ] <EOL> pages = paginate ( request , searcher , settings . SEARCH_RESULTS_PER_PAGE ) <EOL> if pages . paginator . count == <NUM_LIT:0> : <EOL> fallback_results = _fallback_results ( language , cleaned [ '<STR_LIT>' ] ) <EOL> results = [ ] <EOL> else : <EOL> fallback_results = None <EOL> results = build_results_list ( pages , request . IS_JSON ) <EOL> product = Product . objects . filter ( slug__in = cleaned [ '<STR_LIT>' ] ) <EOL> if product : <EOL> product_titles = [ pgettext ( '<STR_LIT>' , p . title ) for p in product ] <EOL> else : <EOL> product_titles = [ _ ( '<STR_LIT>' ) ] <EOL> product_titles = '<STR_LIT:U+002CU+0020>' . join ( product_titles ) <EOL> data = { <EOL> '<STR_LIT>' : pages . paginator . count , <EOL> '<STR_LIT>' : results , <EOL> '<STR_LIT>' : fallback_results , <EOL> '<STR_LIT>' : product_titles , <EOL> '<STR_LIT:q>' : cleaned [ '<STR_LIT:q>' ] , <EOL> '<STR_LIT:w>' : cleaned [ '<STR_LIT:w>' ] , <EOL> '<STR_LIT>' : lang_name , <EOL> '<STR_LIT>' : Product . objects . filter ( visible = True ) } <EOL> if request . IS_JSON : <EOL> data [ '<STR_LIT>' ] = len ( data [ '<STR_LIT>' ] ) <EOL> data [ '<STR_LIT>' ] = [ { '<STR_LIT>' : p . slug , '<STR_LIT:title>' : p . title } <EOL> for p in data [ '<STR_LIT>' ] ] <EOL> if product : <EOL> data [ '<STR_LIT>' ] = product [ <NUM_LIT:0> ] . slug <EOL> pages = Paginator ( pages ) <EOL> data [ '<STR_LIT>' ] = dict ( <EOL> number = pages . pager . number , <EOL> num_pages = pages . pager . paginator . num_pages , <EOL> has_next = pages . pager . has_next ( ) , <EOL> has_previous = pages . pager . has_previous ( ) , <EOL> max = pages . max , <EOL> span = pages . span , <EOL> dotted_upper = pages . pager . dotted_upper , <EOL> dotted_lower = pages . pager . dotted_lower , <EOL> page_range = pages . pager . page_range , <EOL> url = pages . pager . url , <EOL> ) <EOL> if not results : <EOL> data [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> json_data = to_json ( data ) <EOL> if request . JSON_CALLBACK : <EOL> json_data = request . JSON_CALLBACK + '<STR_LIT:(>' + json_data + '<STR_LIT>' <EOL> return HttpResponse ( json_data , content_type = request . CONTENT_TYPE ) <EOL> data . update ( { <EOL> '<STR_LIT>' : product , <EOL> '<STR_LIT>' : pages , <EOL> '<STR_LIT>' : search_form , <EOL> '<STR_LIT>' : False , <EOL> } ) <EOL> resp = cache_control ( render ( request , template , data ) , settings . SEARCH_CACHE_PERIOD ) <EOL> resp . set_cookie ( settings . LAST_SEARCH_COOKIE , urlquote ( cleaned [ '<STR_LIT:q>' ] ) , <EOL> max_age = <NUM_LIT> , secure = False , httponly = False ) <EOL> return resp <EOL> @ markup_json <EOL> @ handle_es_errors ( _es_down_template ) <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> def advanced_search ( request , template = None ) : <EOL> """<STR_LIT>""" <EOL> to_json = JSONRenderer ( ) . render <EOL> r = request . GET . copy ( ) <EOL> a = request . GET . get ( '<STR_LIT:a>' , '<STR_LIT:2>' ) <EOL> r [ '<STR_LIT:a>' ] = <NUM_LIT:1> <EOL> language = locale_or_default ( request . GET . get ( '<STR_LIT>' , request . LANGUAGE_CODE ) ) <EOL> r [ '<STR_LIT>' ] = language <EOL> lang = language . lower ( ) <EOL> lang_name = settings . LANGUAGES_DICT . get ( lang ) or '<STR_LIT>' <EOL> search_form = AdvancedSearchForm ( r , auto_id = False ) <EOL> search_form . set_allowed_forums ( request . user ) <EOL> if a == '<STR_LIT:2>' or not search_form . is_valid ( ) : <EOL> if request . IS_JSON : <EOL> return HttpResponse ( <EOL> json . dumps ( { '<STR_LIT:error>' : _ ( '<STR_LIT>' ) } ) , <EOL> content_type = request . CONTENT_TYPE , <EOL> status = <NUM_LIT> ) <EOL> t = template if request . MOBILE else '<STR_LIT>' <EOL> data = { '<STR_LIT>' : True , <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : search_form } <EOL> last_search = request . COOKIES . get ( settings . LAST_SEARCH_COOKIE ) <EOL> if last_search and '<STR_LIT:q>' not in r : <EOL> cached_field = urlquote ( last_search ) <EOL> data . update ( { '<STR_LIT>' : cached_field } ) <EOL> return cache_control ( <EOL> render ( request , t , data ) , <EOL> settings . SEARCH_CACHE_PERIOD ) <EOL> cleaned = search_form . cleaned_data <EOL> if request . MOBILE and cleaned [ '<STR_LIT:w>' ] == constants . WHERE_BASIC : <EOL> cleaned [ '<STR_LIT:w>' ] = constants . WHERE_WIKI <EOL> searcher = ( AnalyzerS ( ) . es ( urls = settings . ES_URLS ) <EOL> . indexes ( es_utils . read_index ( '<STR_LIT:default>' ) ) ) <EOL> doctypes = [ ] <EOL> final_filter = F ( ) <EOL> unix_now = int ( time . time ( ) ) <EOL> interval_filters = ( <EOL> ( '<STR_LIT>' , cleaned [ '<STR_LIT>' ] , cleaned [ '<STR_LIT>' ] ) , <EOL> ( '<STR_LIT>' , cleaned [ '<STR_LIT>' ] , cleaned [ '<STR_LIT>' ] ) <EOL> ) <EOL> if cleaned [ '<STR_LIT:w>' ] & constants . WHERE_WIKI : <EOL> wiki_f = F ( model = '<STR_LIT>' ) <EOL> if cleaned [ '<STR_LIT>' ] : <EOL> wiki_f &= F ( document_category__in = cleaned [ '<STR_LIT>' ] ) <EOL> wiki_f &= F ( document_locale = language ) <EOL> products = cleaned [ '<STR_LIT>' ] <EOL> for p in products : <EOL> wiki_f &= F ( product = p ) <EOL> topics = cleaned [ '<STR_LIT>' ] <EOL> for t in topics : <EOL> wiki_f &= F ( topic = t ) <EOL> if not cleaned [ '<STR_LIT>' ] : <EOL> wiki_f &= F ( document_is_archived = False ) <EOL> sortby = cleaned [ '<STR_LIT>' ] <EOL> try : <EOL> searcher = searcher . order_by ( * constants . SORT_DOCUMENTS [ sortby ] ) <EOL> except IndexError : <EOL> pass <EOL> doctypes . append ( DocumentMappingType . get_mapping_type_name ( ) ) <EOL> final_filter |= wiki_f <EOL> if cleaned [ '<STR_LIT:w>' ] & constants . WHERE_SUPPORT : <EOL> question_f = F ( model = '<STR_LIT>' ) <EOL> ternary_filters = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> d = dict ( ( '<STR_LIT>' % filter_name , <EOL> _ternary_filter ( cleaned [ filter_name ] ) ) <EOL> for filter_name in ternary_filters if cleaned [ filter_name ] ) <EOL> if d : <EOL> question_f &= F ( ** d ) <EOL> if cleaned [ '<STR_LIT>' ] : <EOL> question_f &= F ( question_creator = cleaned [ '<STR_LIT>' ] ) <EOL> if cleaned [ '<STR_LIT>' ] : <EOL> question_f &= F ( question_answer_creator = cleaned [ '<STR_LIT>' ] ) <EOL> q_tags = [ t . strip ( ) for t in cleaned [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> for t in q_tags : <EOL> if t : <EOL> question_f &= F ( question_tag = t ) <EOL> products = cleaned [ '<STR_LIT>' ] <EOL> for p in products : <EOL> question_f &= F ( product = p ) <EOL> topics = cleaned [ '<STR_LIT>' ] <EOL> for t in topics : <EOL> question_f &= F ( topic = t ) <EOL> if cleaned [ '<STR_LIT>' ] == constants . INTERVAL_BEFORE : <EOL> question_f &= F ( question_num_votes__lte = max ( cleaned [ '<STR_LIT>' ] , <NUM_LIT:0> ) ) <EOL> elif cleaned [ '<STR_LIT>' ] == constants . INTERVAL_AFTER : <EOL> question_f &= F ( question_num_votes__gte = cleaned [ '<STR_LIT>' ] ) <EOL> sortby = cleaned [ '<STR_LIT>' ] <EOL> try : <EOL> searcher = searcher . order_by ( * constants . SORT_QUESTIONS [ sortby ] ) <EOL> except IndexError : <EOL> pass <EOL> for filter_name , filter_option , filter_date in interval_filters : <EOL> if filter_option == constants . INTERVAL_BEFORE : <EOL> before = { filter_name + '<STR_LIT>' : <NUM_LIT:0> , <EOL> filter_name + '<STR_LIT>' : max ( filter_date , <NUM_LIT:0> ) } <EOL> question_f &= F ( ** before ) <EOL> elif filter_option == constants . INTERVAL_AFTER : <EOL> after = { filter_name + '<STR_LIT>' : min ( filter_date , unix_now ) , <EOL> filter_name + '<STR_LIT>' : unix_now } <EOL> question_f &= F ( ** after ) <EOL> doctypes . append ( QuestionMappingType . get_mapping_type_name ( ) ) <EOL> final_filter |= question_f <EOL> if cleaned [ '<STR_LIT:w>' ] & constants . WHERE_DISCUSSION : <EOL> discussion_f = F ( model = '<STR_LIT>' ) <EOL> if cleaned [ '<STR_LIT>' ] : <EOL> discussion_f &= F ( post_author_ord = cleaned [ '<STR_LIT>' ] ) <EOL> if cleaned [ '<STR_LIT>' ] : <EOL> if constants . DISCUSSION_STICKY in cleaned [ '<STR_LIT>' ] : <EOL> discussion_f &= F ( post_is_sticky = <NUM_LIT:1> ) <EOL> if constants . DISCUSSION_LOCKED in cleaned [ '<STR_LIT>' ] : <EOL> discussion_f &= F ( post_is_locked = <NUM_LIT:1> ) <EOL> valid_forum_ids = [ f . id for f in Forum . authorized_forums_for_user ( request . user ) ] <EOL> forum_ids = None <EOL> if cleaned [ '<STR_LIT>' ] : <EOL> forum_ids = [ f for f in cleaned [ '<STR_LIT>' ] if f in valid_forum_ids ] <EOL> if not forum_ids : <EOL> forum_ids = valid_forum_ids <EOL> discussion_f &= F ( post_forum_id__in = forum_ids ) <EOL> for filter_name , filter_option , filter_date in interval_filters : <EOL> if filter_option == constants . INTERVAL_BEFORE : <EOL> before = { filter_name + '<STR_LIT>' : <NUM_LIT:0> , <EOL> filter_name + '<STR_LIT>' : max ( filter_date , <NUM_LIT:0> ) } <EOL> discussion_f &= F ( ** before ) <EOL> elif filter_option == constants . INTERVAL_AFTER : <EOL> after = { filter_name + '<STR_LIT>' : min ( filter_date , unix_now ) , <EOL> filter_name + '<STR_LIT>' : unix_now } <EOL> discussion_f &= F ( ** after ) <EOL> doctypes . append ( ThreadMappingType . get_mapping_type_name ( ) ) <EOL> final_filter |= discussion_f <EOL> searcher = searcher . doctypes ( * doctypes ) <EOL> searcher = searcher . filter ( final_filter ) <EOL> if '<STR_LIT>' in request . GET and request . GET [ '<STR_LIT>' ] == '<STR_LIT:1>' : <EOL> searcher = searcher . explain ( ) <EOL> cleaned_q = cleaned [ '<STR_LIT:q>' ] <EOL> searcher = searcher . highlight ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> pre_tags = [ '<STR_LIT>' ] , <EOL> post_tags = [ '<STR_LIT>' ] , <EOL> number_of_fragments = <NUM_LIT:0> ) <EOL> searcher = apply_boosts ( searcher ) <EOL> if cleaned_q : <EOL> query_fields = chain ( * [ <EOL> cls . get_query_fields ( ) for cls in [ <EOL> DocumentMappingType , <EOL> ThreadMappingType , <EOL> QuestionMappingType <EOL> ] <EOL> ] ) <EOL> query = { } <EOL> for field in query_fields : <EOL> query [ '<STR_LIT>' % field ] = cleaned_q <EOL> query = es_utils . es_query_with_analyzer ( query , language ) <EOL> searcher = searcher . query ( should = True , ** query ) <EOL> searcher = searcher [ : settings . SEARCH_MAX_RESULTS ] <EOL> pages = paginate ( request , searcher , settings . SEARCH_RESULTS_PER_PAGE ) <EOL> if pages . paginator . count == <NUM_LIT:0> : <EOL> fallback_results = _fallback_results ( language , cleaned [ '<STR_LIT>' ] ) <EOL> results = [ ] <EOL> else : <EOL> fallback_results = None <EOL> results = build_results_list ( pages , request . IS_JSON ) <EOL> items = [ ( k , v ) for k in search_form . fields for <EOL> v in r . getlist ( k ) if v and k != '<STR_LIT:a>' ] <EOL> items . append ( ( '<STR_LIT:a>' , '<STR_LIT:2>' ) ) <EOL> product = Product . objects . filter ( slug__in = cleaned [ '<STR_LIT>' ] ) <EOL> if product : <EOL> product_titles = [ pgettext ( '<STR_LIT>' , p . title ) for p in product ] <EOL> else : <EOL> product_titles = [ _ ( '<STR_LIT>' ) ] <EOL> product_titles = '<STR_LIT:U+002CU+0020>' . join ( product_titles ) <EOL> data = { <EOL> '<STR_LIT>' : pages . paginator . count , <EOL> '<STR_LIT>' : results , <EOL> '<STR_LIT>' : fallback_results , <EOL> '<STR_LIT>' : product_titles , <EOL> '<STR_LIT:q>' : cleaned [ '<STR_LIT:q>' ] , <EOL> '<STR_LIT:w>' : cleaned [ '<STR_LIT:w>' ] , <EOL> '<STR_LIT>' : lang_name , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : Product . objects . filter ( visible = True ) <EOL> } <EOL> if request . IS_JSON : <EOL> data [ '<STR_LIT>' ] = len ( data [ '<STR_LIT>' ] ) <EOL> data [ '<STR_LIT>' ] = [ { '<STR_LIT>' : p . slug , '<STR_LIT:title>' : p . title } <EOL> for p in data [ '<STR_LIT>' ] ] <EOL> if product : <EOL> data [ '<STR_LIT>' ] = product [ <NUM_LIT:0> ] . slug <EOL> pages = Paginator ( pages ) <EOL> data [ '<STR_LIT>' ] = dict ( <EOL> number = pages . pager . number , <EOL> num_pages = pages . pager . paginator . num_pages , <EOL> has_next = pages . pager . has_next ( ) , <EOL> has_previous = pages . pager . has_previous ( ) , <EOL> max = pages . max , <EOL> span = pages . span , <EOL> dotted_upper = pages . pager . dotted_upper , <EOL> dotted_lower = pages . pager . dotted_lower , <EOL> page_range = pages . pager . page_range , <EOL> url = pages . pager . url , <EOL> ) <EOL> if not results : <EOL> data [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> json_data = to_json ( data ) <EOL> if request . JSON_CALLBACK : <EOL> json_data = request . JSON_CALLBACK + '<STR_LIT:(>' + json_data + '<STR_LIT>' <EOL> return HttpResponse ( json_data , content_type = request . CONTENT_TYPE ) <EOL> data . update ( { <EOL> '<STR_LIT>' : product , <EOL> '<STR_LIT>' : pages , <EOL> '<STR_LIT>' : search_form <EOL> } ) <EOL> resp = cache_control ( render ( request , template , data ) , settings . SEARCH_CACHE_PERIOD ) <EOL> resp . set_cookie ( settings . LAST_SEARCH_COOKIE , urlquote ( cleaned [ '<STR_LIT:q>' ] ) , <EOL> max_age = <NUM_LIT> , secure = False , httponly = False ) <EOL> return resp <EOL> @ cache_page ( <NUM_LIT> * <NUM_LIT:15> ) <EOL> def opensearch_suggestions ( request ) : <EOL> """<STR_LIT>""" <EOL> content_type = '<STR_LIT>' <EOL> search_form = SimpleSearchForm ( request . GET , auto_id = False ) <EOL> if not search_form . is_valid ( ) : <EOL> return HttpResponseBadRequest ( content_type = content_type ) <EOL> cleaned = search_form . cleaned_data <EOL> language = locale_or_default ( cleaned [ '<STR_LIT>' ] or request . LANGUAGE_CODE ) <EOL> searcher = generate_simple_search ( search_form , language , with_highlights = False ) <EOL> searcher = searcher . values_dict ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:url>' ) <EOL> results = searcher [ : <NUM_LIT:10> ] <EOL> def urlize ( r ) : <EOL> return u'<STR_LIT>' % ( <EOL> '<STR_LIT>' if request . is_secure ( ) else '<STR_LIT:http>' , <EOL> request . get_host ( ) , <EOL> r [ '<STR_LIT:url>' ] [ <NUM_LIT:0> ] <EOL> ) <EOL> def titleize ( r ) : <EOL> return r . get ( '<STR_LIT>' , r . get ( '<STR_LIT>' , [ _ ( '<STR_LIT>' ) ] ) ) [ <NUM_LIT:0> ] <EOL> try : <EOL> data = [ <EOL> cleaned [ '<STR_LIT:q>' ] , <EOL> [ titleize ( r ) for r in results ] , <EOL> [ ] , <EOL> [ urlize ( r ) for r in results ] <EOL> ] <EOL> except ES_EXCEPTIONS : <EOL> data = [ ] <EOL> return HttpResponse ( json . dumps ( data ) , content_type = content_type ) <EOL> @ cache_page ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> def opensearch_plugin ( request ) : <EOL> """<STR_LIT>""" <EOL> host = u'<STR_LIT>' % ( '<STR_LIT>' if request . is_secure ( ) else '<STR_LIT:http>' , request . get_host ( ) ) <EOL> return render_to_response ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT:host>' : host , <EOL> '<STR_LIT>' : request . LANGUAGE_CODE <EOL> } , <EOL> content_type = '<STR_LIT>' <EOL> ) <EOL> def _ternary_filter ( ternary_value ) : <EOL> """<STR_LIT>""" <EOL> return ternary_value == constants . TERNARY_YES <EOL> def _build_es_excerpt ( result , first_only = False ) : <EOL> """<STR_LIT>""" <EOL> bits = [ m . strip ( ) for m in chain ( * result . es_meta . highlight . values ( ) ) ] <EOL> if first_only and bits : <EOL> excerpt = bits [ <NUM_LIT:0> ] <EOL> else : <EOL> excerpt = EXCERPT_JOINER . join ( bits ) <EOL> return jinja2 . Markup ( clean_excerpt ( excerpt ) ) <EOL> def _fallback_results ( locale , product_slugs ) : <EOL> """<STR_LIT>""" <EOL> products = [ ] <EOL> for slug in product_slugs : <EOL> try : <EOL> p = Product . objects . get ( slug = slug ) <EOL> products . append ( p ) <EOL> except Product . DoesNotExist : <EOL> pass <EOL> docs , fallback = documents_for ( locale , products = products ) <EOL> docs = docs + ( fallback or [ ] ) <EOL> return docs [ : <NUM_LIT:20> ] </s>
