<s> import functools <EOL> import httplib as http <EOL> import json <EOL> import logging <EOL> import time <EOL> from urllib2 import unquote <EOL> import bleach <EOL> from flask import request <EOL> from modularodm import Q <EOL> from framework . auth . decorators import collect_auth <EOL> from framework . auth . decorators import must_be_logged_in <EOL> from framework . exceptions import HTTPError <EOL> from framework import sentry <EOL> from website import language <EOL> from website import settings <EOL> from website . models import Node , User <EOL> from website . project . views . contributor import get_node_contributors_abbrev <EOL> from website . search import exceptions <EOL> from website . search import share_search <EOL> from website . search import util <EOL> from website . search . exceptions import IndexNotFoundError , MalformedQueryError <EOL> import website . search . search as search <EOL> from website . search . util import build_query <EOL> from website . util import api_url_for <EOL> logger = logging . getLogger ( __name__ ) <EOL> RESULTS_PER_PAGE = <NUM_LIT> <EOL> def handle_search_errors ( func ) : <EOL> @ functools . wraps ( func ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> try : <EOL> return func ( * args , ** kwargs ) <EOL> except exceptions . MalformedQueryError : <EOL> raise HTTPError ( http . BAD_REQUEST , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : language . SEARCH_QUERY_HELP , <EOL> } ) <EOL> except exceptions . SearchUnavailableError : <EOL> raise HTTPError ( http . SERVICE_UNAVAILABLE , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> } ) <EOL> except exceptions . SearchException : <EOL> sentry . log_exception ( ) <EOL> sentry . log_message ( '<STR_LIT>' ) <EOL> raise HTTPError ( http . BAD_REQUEST , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : language . SEARCH_QUERY_HELP , <EOL> } ) <EOL> return wrapped <EOL> @ handle_search_errors <EOL> def search_search ( ** kwargs ) : <EOL> _type = kwargs . get ( '<STR_LIT:type>' , None ) <EOL> tick = time . time ( ) <EOL> results = { } <EOL> if request . method == '<STR_LIT:POST>' : <EOL> results = search . search ( request . get_json ( ) , doc_type = _type ) <EOL> elif request . method == '<STR_LIT:GET>' : <EOL> q = request . args . get ( '<STR_LIT:q>' , '<STR_LIT:*>' ) <EOL> start = request . args . get ( '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> size = request . args . get ( '<STR_LIT:size>' , '<STR_LIT>' ) <EOL> results = search . search ( build_query ( q , start , size ) , doc_type = _type ) <EOL> results [ '<STR_LIT:time>' ] = round ( time . time ( ) - tick , <NUM_LIT:2> ) <EOL> return results <EOL> def conditionally_add_query_item ( query , item , condition ) : <EOL> """<STR_LIT>""" <EOL> condition = condition . lower ( ) <EOL> if condition == "<STR_LIT:yes>" : <EOL> return query & Q ( item , '<STR_LIT>' , True ) <EOL> elif condition == "<STR_LIT>" : <EOL> return query & Q ( item , '<STR_LIT>' , False ) <EOL> elif condition == "<STR_LIT>" : <EOL> return query <EOL> raise HTTPError ( http . BAD_REQUEST ) <EOL> @ must_be_logged_in <EOL> def search_projects_by_title ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> user = kwargs [ '<STR_LIT>' ] . user <EOL> term = request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> max_results = int ( request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> category = request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) <EOL> is_deleted = request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) <EOL> is_collection = request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) <EOL> is_registration = request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) <EOL> include_public = request . args . get ( '<STR_LIT>' , '<STR_LIT:yes>' ) . lower ( ) <EOL> include_contributed = request . args . get ( '<STR_LIT>' , '<STR_LIT:yes>' ) . lower ( ) <EOL> ignore_nodes = request . args . getlist ( '<STR_LIT>' , [ ] ) <EOL> matching_title = ( <EOL> Q ( '<STR_LIT:title>' , '<STR_LIT>' , term ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , category ) <EOL> ) <EOL> matching_title = conditionally_add_query_item ( matching_title , '<STR_LIT>' , is_deleted ) <EOL> matching_title = conditionally_add_query_item ( matching_title , '<STR_LIT>' , is_collection ) <EOL> matching_title = conditionally_add_query_item ( matching_title , '<STR_LIT>' , is_registration ) <EOL> if len ( ignore_nodes ) > <NUM_LIT:0> : <EOL> for node_id in ignore_nodes : <EOL> matching_title = matching_title & Q ( '<STR_LIT>' , '<STR_LIT>' , node_id ) <EOL> my_projects = [ ] <EOL> my_project_count = <NUM_LIT:0> <EOL> public_projects = [ ] <EOL> if include_contributed == "<STR_LIT:yes>" : <EOL> my_projects = Node . find ( <EOL> matching_title & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , user . _id ) <EOL> ) . limit ( max_results ) <EOL> my_project_count = my_project_count <EOL> if my_project_count < max_results and include_public == "<STR_LIT:yes>" : <EOL> public_projects = Node . find ( <EOL> matching_title & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , True ) <EOL> ) . limit ( max_results - my_project_count ) <EOL> results = list ( my_projects ) + list ( public_projects ) <EOL> ret = process_project_search_results ( results , ** kwargs ) <EOL> return ret <EOL> @ must_be_logged_in <EOL> def process_project_search_results ( results , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> user = kwargs [ '<STR_LIT>' ] . user <EOL> ret = [ ] <EOL> for project in results : <EOL> authors = get_node_contributors_abbrev ( project = project , auth = kwargs [ '<STR_LIT>' ] ) <EOL> authors_html = '<STR_LIT>' <EOL> for author in authors [ '<STR_LIT>' ] : <EOL> a = User . load ( author [ '<STR_LIT>' ] ) <EOL> authors_html += '<STR_LIT>' % ( a . url , a . fullname ) <EOL> authors_html += author [ '<STR_LIT>' ] + '<STR_LIT:U+0020>' <EOL> authors_html += '<STR_LIT:U+0020>' + authors [ '<STR_LIT>' ] <EOL> ret . append ( { <EOL> '<STR_LIT:id>' : project . _id , <EOL> '<STR_LIT:label>' : project . title , <EOL> '<STR_LIT:value>' : project . title , <EOL> '<STR_LIT>' : '<STR_LIT>' if user in project . contributors else '<STR_LIT>' , <EOL> '<STR_LIT>' : authors_html , <EOL> } ) <EOL> return ret <EOL> @ collect_auth <EOL> def search_contributor ( auth ) : <EOL> user = auth . user if auth else None <EOL> nid = request . args . get ( '<STR_LIT>' ) <EOL> exclude = Node . load ( nid ) . contributors if nid else [ ] <EOL> query = bleach . clean ( request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) , tags = [ ] , strip = True ) <EOL> page = int ( bleach . clean ( request . args . get ( '<STR_LIT>' , '<STR_LIT:0>' ) , tags = [ ] , strip = True ) ) <EOL> size = int ( bleach . clean ( request . args . get ( '<STR_LIT:size>' , '<STR_LIT:5>' ) , tags = [ ] , strip = True ) ) <EOL> return search . search_contributor ( query = query , page = page , size = size , <EOL> exclude = exclude , current_user = user ) <EOL> @ handle_search_errors <EOL> def search_share ( ) : <EOL> tick = time . time ( ) <EOL> results = { } <EOL> count = request . args . get ( '<STR_LIT:count>' ) is not None <EOL> raw = request . args . get ( '<STR_LIT>' ) is not None <EOL> version = request . args . get ( '<STR_LIT:v>' ) <EOL> if version : <EOL> index = settings . SHARE_ELASTIC_INDEX_TEMPLATE . format ( version ) <EOL> else : <EOL> index = settings . SHARE_ELASTIC_INDEX <EOL> if request . method == '<STR_LIT:POST>' : <EOL> query = request . get_json ( ) <EOL> elif request . method == '<STR_LIT:GET>' : <EOL> query = build_query ( <EOL> request . args . get ( '<STR_LIT:q>' , '<STR_LIT:*>' ) , <EOL> request . args . get ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> request . args . get ( '<STR_LIT:size>' , <NUM_LIT:10> ) , <EOL> sort = request . args . get ( '<STR_LIT>' ) <EOL> ) <EOL> if count : <EOL> results = search . count_share ( query , index = index ) <EOL> else : <EOL> results = search . search_share ( query , raw , index = index ) <EOL> results [ '<STR_LIT:time>' ] = round ( time . time ( ) - tick , <NUM_LIT:2> ) <EOL> return results <EOL> @ handle_search_errors <EOL> def search_share_stats ( ) : <EOL> q = request . args . get ( '<STR_LIT:q>' ) <EOL> query = build_query ( q , <NUM_LIT:0> , <NUM_LIT:0> ) if q else { } <EOL> return search . share_stats ( query = query ) <EOL> @ handle_search_errors <EOL> def search_share_atom ( ** kwargs ) : <EOL> json_query = request . args . get ( '<STR_LIT>' ) <EOL> start = util . compute_start ( request . args . get ( '<STR_LIT>' , <NUM_LIT:1> ) , RESULTS_PER_PAGE ) <EOL> if not json_query : <EOL> q = request . args . get ( '<STR_LIT:q>' , '<STR_LIT:*>' ) <EOL> sort = request . args . get ( '<STR_LIT>' ) <EOL> query = build_query ( q , size = RESULTS_PER_PAGE , start = start , sort = sort ) <EOL> else : <EOL> query = json . loads ( unquote ( json_query ) ) <EOL> query [ '<STR_LIT>' ] = start <EOL> query [ '<STR_LIT:size>' ] = RESULTS_PER_PAGE <EOL> for field in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if query . get ( field ) : <EOL> del query [ field ] <EOL> q = query <EOL> try : <EOL> search_results = search . search_share ( query ) <EOL> except MalformedQueryError : <EOL> raise HTTPError ( http . BAD_REQUEST ) <EOL> except IndexNotFoundError : <EOL> search_results = { <EOL> '<STR_LIT:count>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> atom_url = api_url_for ( '<STR_LIT>' , _xml = True , _absolute = True ) <EOL> return util . create_atom_feed ( <EOL> name = '<STR_LIT>' , <EOL> data = search_results [ '<STR_LIT>' ] , <EOL> query = q , <EOL> size = RESULTS_PER_PAGE , <EOL> start = start , <EOL> url = atom_url , <EOL> to_atom = share_search . to_atom <EOL> ) <EOL> def search_share_providers ( ) : <EOL> return search . share_providers ( ) </s>
