<s> """<STR_LIT>""" <EOL> import urllib <EOL> from google . appengine . api import search <EOL> from google . appengine . tools . devappserver2 . admin import admin_request_handler <EOL> class BaseSearchHandler ( admin_request_handler . AdminRequestHandler ) : <EOL> _MAX_RESULTS_PER_PAGE = <NUM_LIT:20> <EOL> def _handle_paging ( self , start , has_more , values ) : <EOL> if has_more : <EOL> values [ '<STR_LIT>' ] = self . _construct_url ( <EOL> add = { '<STR_LIT:start>' : start + self . _MAX_RESULTS_PER_PAGE } ) <EOL> values [ '<STR_LIT>' ] = True <EOL> if start > <NUM_LIT:0> : <EOL> values [ '<STR_LIT>' ] = self . _construct_url ( <EOL> add = { '<STR_LIT:start>' : max ( <NUM_LIT:0> , start - self . _MAX_RESULTS_PER_PAGE ) } ) <EOL> values [ '<STR_LIT>' ] = True <EOL> class SearchIndexesListHandler ( BaseSearchHandler ) : <EOL> def get ( self ) : <EOL> start = self . request . get_range ( '<STR_LIT:start>' , min_value = <NUM_LIT:0> , default = <NUM_LIT:0> ) <EOL> namespace = self . request . get ( '<STR_LIT>' , default_value = None ) <EOL> resp = search . get_indexes ( offset = start , <EOL> limit = self . _MAX_RESULTS_PER_PAGE + <NUM_LIT:1> , <EOL> namespace = namespace or '<STR_LIT>' ) <EOL> has_more = len ( resp . results ) > self . _MAX_RESULTS_PER_PAGE <EOL> indexes = resp . results [ : self . _MAX_RESULTS_PER_PAGE ] <EOL> values = { <EOL> '<STR_LIT>' : namespace or '<STR_LIT>' , <EOL> '<STR_LIT>' : namespace is not None , <EOL> '<STR_LIT>' : indexes } <EOL> self . _handle_paging ( start , has_more , values ) <EOL> self . response . write ( self . render ( '<STR_LIT>' , values ) ) <EOL> class SearchIndexHandler ( BaseSearchHandler ) : <EOL> def _process_search_response ( self , response ) : <EOL> documents = [ ] <EOL> field_names = set ( ) <EOL> for result in response . results : <EOL> fields = dict ( ( field . name , field . value ) for field in result . fields ) <EOL> field_names . update ( fields ) <EOL> documents . append ( ( result . doc_id , fields ) ) <EOL> field_names = sorted ( field_names ) <EOL> template_documents = [ ] <EOL> for doc_id , fields in documents : <EOL> doc_fields = [ fields . get ( field_name , '<STR_LIT>' ) for field_name in field_names ] <EOL> template_documents . append ( { '<STR_LIT>' : doc_id , '<STR_LIT>' : doc_fields } ) <EOL> return { '<STR_LIT>' : template_documents , '<STR_LIT>' : field_names } <EOL> def get ( self ) : <EOL> index_name = self . request . get ( '<STR_LIT:index>' ) <EOL> if not index_name : <EOL> self . redirect ( '<STR_LIT>' ) <EOL> return <EOL> start = self . request . get_range ( '<STR_LIT:start>' , min_value = <NUM_LIT:0> , default = <NUM_LIT:0> ) <EOL> query = self . request . get ( '<STR_LIT>' ) <EOL> namespace = self . request . get ( '<STR_LIT>' ) <EOL> index = search . Index ( name = index_name , namespace = namespace ) <EOL> resp = index . search ( query = search . Query ( <EOL> query_string = query , <EOL> options = search . QueryOptions ( offset = start , <EOL> limit = self . _MAX_RESULTS_PER_PAGE ) ) ) <EOL> has_more = resp . number_found > start + self . _MAX_RESULTS_PER_PAGE <EOL> values = { <EOL> '<STR_LIT>' : namespace , <EOL> '<STR_LIT:index>' : index_name , <EOL> '<STR_LIT:start>' : start , <EOL> '<STR_LIT>' : query , <EOL> '<STR_LIT>' : self . _process_search_response ( resp ) , <EOL> } <EOL> self . _handle_paging ( start , has_more , values ) <EOL> self . response . write ( self . render ( '<STR_LIT>' , values ) ) <EOL> def post ( self ) : <EOL> index_name = self . request . get ( '<STR_LIT:index>' ) <EOL> namespace = self . request . get ( '<STR_LIT>' ) <EOL> start = self . request . get_range ( '<STR_LIT:start>' , min_value = <NUM_LIT:0> , default = <NUM_LIT:0> ) <EOL> index = <NUM_LIT:0> <EOL> num_docs = int ( self . request . get ( '<STR_LIT>' ) ) <EOL> docs = self . request . params . getall ( '<STR_LIT>' ) <EOL> if len ( docs ) == num_docs : <EOL> start = max ( <NUM_LIT:0> , start - self . _MAX_RESULTS_PER_PAGE ) <EOL> index = search . Index ( name = index_name , namespace = namespace ) <EOL> index . delete ( docs ) <EOL> self . redirect ( '<STR_LIT>' % urllib . urlencode ( <EOL> { '<STR_LIT>' : namespace , <EOL> '<STR_LIT:index>' : index_name , <EOL> '<STR_LIT:start>' : start } ) ) <EOL> class SearchDocumentHandler ( BaseSearchHandler ) : <EOL> def get ( self ) : <EOL> index_name = self . request . get ( '<STR_LIT:index>' ) <EOL> doc_id = self . request . get ( '<STR_LIT:id>' ) <EOL> namespace = self . request . get ( '<STR_LIT>' ) <EOL> doc = None <EOL> index = search . Index ( name = index_name , namespace = namespace ) <EOL> resp = index . get_range ( start_id = doc_id , limit = <NUM_LIT:1> ) <EOL> if resp . results and resp . results [ <NUM_LIT:0> ] . doc_id == doc_id : <EOL> doc = resp . results [ <NUM_LIT:0> ] <EOL> values = { <EOL> '<STR_LIT>' : namespace , <EOL> '<STR_LIT:index>' : index_name , <EOL> '<STR_LIT>' : doc_id , <EOL> '<STR_LIT>' : doc , <EOL> } <EOL> self . response . write ( self . render ( '<STR_LIT>' , values ) ) </s>
