<s> from django . conf import settings <EOL> from rest_framework import serializers <EOL> from rest_framework . decorators import api_view <EOL> from rest_framework . response import Response <EOL> from kitsune . products . models import Product <EOL> from kitsune . questions . models import Question , QuestionMappingType <EOL> from kitsune . questions . api import QuestionSerializer <EOL> from kitsune . search import es_utils <EOL> from kitsune . sumo . api_utils import GenericAPIException <EOL> from kitsune . wiki . api import DocumentDetailSerializer <EOL> from kitsune . wiki . models import Document , DocumentMappingType <EOL> def positive_integer ( value ) : <EOL> if value < <NUM_LIT:0> : <EOL> raise serializers . ValidationError ( '<STR_LIT>' ) <EOL> def valid_product ( value ) : <EOL> if not value : <EOL> return <EOL> if not Product . objects . filter ( slug = value ) . exists ( ) : <EOL> raise serializers . ValidationError ( <EOL> '<STR_LIT>' . format ( value ) <EOL> ) <EOL> def valid_locale ( value ) : <EOL> if not value : <EOL> return <EOL> if value not in settings . SUMO_LANGUAGES : <EOL> if value in settings . NON_SUPPORTED_LOCALES : <EOL> fallback = settings . NON_SUPPORTED_LOCALES [ value ] or settings . WIKI_DEFAULT_LANGUAGE <EOL> raise serializers . ValidationError ( <EOL> '<STR_LIT>' . format ( <EOL> value , fallback ) ) <EOL> else : <EOL> raise serializers . ValidationError ( <EOL> '<STR_LIT>' . format ( value ) <EOL> ) <EOL> class SuggestSerializer ( serializers . Serializer ) : <EOL> q = serializers . CharField ( required = True ) <EOL> locale = serializers . CharField ( <EOL> required = False , default = settings . WIKI_DEFAULT_LANGUAGE , <EOL> validators = [ valid_locale ] ) <EOL> product = serializers . CharField ( <EOL> required = False , default = '<STR_LIT>' , <EOL> validators = [ valid_product ] ) <EOL> max_questions = serializers . IntegerField ( <EOL> required = False , default = <NUM_LIT:10> , <EOL> validators = [ positive_integer ] ) <EOL> max_documents = serializers . IntegerField ( <EOL> required = False , default = <NUM_LIT:10> , <EOL> validators = [ positive_integer ] ) <EOL> @ api_view ( [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def suggest ( request ) : <EOL> if request . data and request . GET : <EOL> raise GenericAPIException ( <EOL> <NUM_LIT> , '<STR_LIT>' ) <EOL> serializer = SuggestSerializer ( data = ( request . data or request . GET ) ) <EOL> if not serializer . is_valid ( ) : <EOL> raise GenericAPIException ( <NUM_LIT> , serializer . errors ) <EOL> searcher = ( <EOL> es_utils . AnalyzerS ( ) <EOL> . es ( urls = settings . ES_URLS ) <EOL> . indexes ( es_utils . read_index ( '<STR_LIT:default>' ) ) ) <EOL> data = serializer . validated_data <EOL> return Response ( { <EOL> '<STR_LIT>' : _question_suggestions ( <EOL> searcher , data [ '<STR_LIT:q>' ] , data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : _document_suggestions ( <EOL> searcher , data [ '<STR_LIT:q>' ] , data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] ) , <EOL> } ) <EOL> def _question_suggestions ( searcher , text , locale , product , max_results ) : <EOL> if max_results <= <NUM_LIT:0> : <EOL> return [ ] <EOL> search_filter = es_utils . F ( <EOL> model = '<STR_LIT>' , <EOL> question_is_archived = False , <EOL> question_is_locked = False , <EOL> question_is_solved = True ) <EOL> if product : <EOL> search_filter &= es_utils . F ( product = product ) <EOL> if locale : <EOL> search_filter &= es_utils . F ( question_locale = locale ) <EOL> questions = [ ] <EOL> searcher = _query ( searcher , QuestionMappingType , search_filter , text , locale ) <EOL> question_ids = [ result [ '<STR_LIT:id>' ] for result in searcher [ : max_results ] ] <EOL> questions = [ <EOL> QuestionSerializer ( instance = q ) . data <EOL> for q in Question . objects . filter ( id__in = question_ids ) <EOL> ] <EOL> return questions <EOL> def _document_suggestions ( searcher , text , locale , product , max_results ) : <EOL> if max_results <= <NUM_LIT:0> : <EOL> return [ ] <EOL> search_filter = es_utils . F ( <EOL> model = '<STR_LIT>' , <EOL> document_category__in = settings . SEARCH_DEFAULT_CATEGORIES , <EOL> document_locale = locale , <EOL> document_is_archived = False ) <EOL> if product : <EOL> search_filter &= es_utils . F ( product = product ) <EOL> documents = [ ] <EOL> searcher = _query ( searcher , DocumentMappingType , search_filter , text , locale ) <EOL> doc_ids = [ result [ '<STR_LIT:id>' ] for result in searcher [ : max_results ] ] <EOL> documents = [ <EOL> DocumentDetailSerializer ( instance = doc ) . data <EOL> for doc in Document . objects . filter ( id__in = doc_ids ) <EOL> ] <EOL> return documents <EOL> def _query ( searcher , mapping_type , search_filter , query_text , locale ) : <EOL> query_fields = mapping_type . get_query_fields ( ) <EOL> query = { } <EOL> for field in query_fields : <EOL> for query_type in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> key = '<STR_LIT>' . format ( field , query_type ) <EOL> query [ key ] = query_text <EOL> query = es_utils . es_query_with_analyzer ( query , locale ) <EOL> return ( searcher <EOL> . doctypes ( mapping_type . get_mapping_type_name ( ) ) <EOL> . filter ( search_filter ) <EOL> . query ( should = True , ** query ) ) </s>
