<s> from django import http <EOL> from django . db . models import Q <EOL> from django . db . transaction import non_atomic_requests <EOL> from django . shortcuts import render <EOL> from django . utils import translation <EOL> from django . utils . encoding import smart_str <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views . decorators . vary import vary_on_headers <EOL> import commonware . log <EOL> from mobility . decorators import mobile_template <EOL> from olympia import amo <EOL> from olympia . bandwagon . views import get_filter as get_filter_view <EOL> from olympia . browse . views import personas_listing as personas_listing_view <EOL> from olympia . addons . models import Addon , Category <EOL> from olympia . amo . decorators import json_view <EOL> from olympia . amo . helpers import locale_url , urlparams <EOL> from olympia . amo . utils import sorted_groupby <EOL> from olympia . bandwagon . models import Collection <EOL> from olympia . versions . compare import dict_from_int , version_dict , version_int <EOL> from . forms import ESSearchForm , SecondarySearchForm <EOL> from . filters import get_locale_analyzer <EOL> DEFAULT_NUM_COLLECTIONS = <NUM_LIT:20> <EOL> DEFAULT_NUM_PERSONAS = <NUM_LIT> <EOL> log = commonware . log . getLogger ( '<STR_LIT>' ) <EOL> def _personas ( request ) : <EOL> """<STR_LIT>""" <EOL> initial = dict ( request . GET . items ( ) ) <EOL> initial . update ( appver = None , platform = None ) <EOL> form = ESSearchForm ( initial , type = amo . ADDON_PERSONA ) <EOL> form . is_valid ( ) <EOL> qs = Addon . search_public ( ) <EOL> filters = [ '<STR_LIT>' ] <EOL> mapping = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> results = _filter_search ( request , qs , form . cleaned_data , filters , <EOL> sorting = mapping , types = [ amo . ADDON_PERSONA ] ) <EOL> form_data = form . cleaned_data . get ( '<STR_LIT:q>' , '<STR_LIT>' ) <EOL> search_opts = { } <EOL> search_opts [ '<STR_LIT>' ] = form . cleaned_data . get ( '<STR_LIT>' , DEFAULT_NUM_PERSONAS ) <EOL> page = form . cleaned_data . get ( '<STR_LIT>' ) or <NUM_LIT:1> <EOL> search_opts [ '<STR_LIT>' ] = ( page - <NUM_LIT:1> ) * search_opts [ '<STR_LIT>' ] <EOL> pager = amo . utils . paginate ( request , results , per_page = search_opts [ '<STR_LIT>' ] ) <EOL> categories , filter , base , category = personas_listing_view ( request ) <EOL> c = dict ( pager = pager , form = form , categories = categories , query = form_data , <EOL> filter = filter , search_placeholder = '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' , c ) <EOL> def _collections ( request ) : <EOL> """<STR_LIT>""" <EOL> initial = dict ( sort = '<STR_LIT>' ) <EOL> initial . update ( request . GET . items ( ) ) <EOL> initial . update ( appver = None , platform = None , pp = DEFAULT_NUM_COLLECTIONS ) <EOL> form = SecondarySearchForm ( initial ) <EOL> form . is_valid ( ) <EOL> qs = Collection . search ( ) . filter ( listed = True , app = request . APP . id ) <EOL> filters = [ '<STR_LIT>' ] <EOL> mapping = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:all>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> results = _filter_search ( request , qs , form . cleaned_data , filters , <EOL> sorting = mapping , <EOL> sorting_default = '<STR_LIT>' , <EOL> types = amo . COLLECTION_SEARCH_CHOICES ) <EOL> form_data = form . cleaned_data . get ( '<STR_LIT:q>' , '<STR_LIT>' ) <EOL> search_opts = { } <EOL> search_opts [ '<STR_LIT>' ] = form . cleaned_data . get ( '<STR_LIT>' , DEFAULT_NUM_COLLECTIONS ) <EOL> page = form . cleaned_data . get ( '<STR_LIT>' ) or <NUM_LIT:1> <EOL> search_opts [ '<STR_LIT>' ] = ( page - <NUM_LIT:1> ) * search_opts [ '<STR_LIT>' ] <EOL> search_opts [ '<STR_LIT>' ] = form . cleaned_data . get ( '<STR_LIT>' ) <EOL> pager = amo . utils . paginate ( request , results , per_page = search_opts [ '<STR_LIT>' ] ) <EOL> c = dict ( pager = pager , form = form , query = form_data , opts = search_opts , <EOL> filter = get_filter_view ( request ) , <EOL> search_placeholder = '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' , c ) <EOL> class BaseAjaxSearch ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , excluded_ids = ( ) , ratings = False ) : <EOL> self . request = request <EOL> self . excluded_ids = excluded_ids <EOL> self . src = getattr ( self , '<STR_LIT:src>' , None ) <EOL> self . types = getattr ( self , '<STR_LIT>' , amo . ADDON_TYPES . keys ( ) ) <EOL> self . limit = <NUM_LIT:10> <EOL> self . key = '<STR_LIT:q>' <EOL> self . ratings = ratings <EOL> default_fields = { <EOL> '<STR_LIT:id>' : '<STR_LIT:id>' , <EOL> '<STR_LIT:name>' : '<STR_LIT:name>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , <NUM_LIT:32> ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , <NUM_LIT:64> ) <EOL> } <EOL> } <EOL> self . fields = getattr ( self , '<STR_LIT>' , default_fields ) <EOL> if self . ratings : <EOL> self . fields [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def queryset ( self ) : <EOL> """<STR_LIT>""" <EOL> results = Addon . objects . none ( ) <EOL> q = self . request . GET . get ( self . key ) <EOL> if q : <EOL> try : <EOL> pk = int ( q ) <EOL> except ValueError : <EOL> pk = None <EOL> qs = None <EOL> if pk : <EOL> qs = Addon . objects . reviewed ( ) . filter ( id = int ( q ) ) <EOL> elif len ( q ) > <NUM_LIT:2> : <EOL> qs = ( Addon . search_public ( ) <EOL> . query ( or_ = name_only_query ( q . lower ( ) ) ) ) <EOL> if qs : <EOL> results = qs . filter ( type__in = self . types ) <EOL> return results <EOL> def _build_fields ( self , item , fields ) : <EOL> data = { } <EOL> for key , prop in fields . iteritems ( ) : <EOL> if isinstance ( prop , dict ) : <EOL> data [ key ] = self . _build_fields ( item , prop ) <EOL> else : <EOL> if isinstance ( prop , tuple ) : <EOL> val = getattr ( item , prop [ <NUM_LIT:0> ] ) ( * prop [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> val = getattr ( item , prop , '<STR_LIT>' ) <EOL> if callable ( val ) : <EOL> val = val ( ) <EOL> data [ key ] = unicode ( val ) <EOL> return data <EOL> def build_list ( self ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> for item in self . queryset ( ) [ : self . limit ] : <EOL> if item . id in self . excluded_ids : <EOL> continue <EOL> d = self . _build_fields ( item , self . fields ) <EOL> if self . src and '<STR_LIT:url>' in d : <EOL> d [ '<STR_LIT:url>' ] = urlparams ( d [ '<STR_LIT:url>' ] , src = self . src ) <EOL> results . append ( d ) <EOL> return results <EOL> @ property <EOL> def items ( self ) : <EOL> return self . build_list ( ) <EOL> class SearchSuggestionsAjax ( BaseAjaxSearch ) : <EOL> src = '<STR_LIT>' <EOL> class AddonSuggestionsAjax ( SearchSuggestionsAjax ) : <EOL> types = [ amo . ADDON_ANY , amo . ADDON_EXTENSION , amo . ADDON_THEME , <EOL> amo . ADDON_DICT , amo . ADDON_SEARCH , amo . ADDON_LPAPP ] <EOL> class PersonaSuggestionsAjax ( SearchSuggestionsAjax ) : <EOL> types = [ amo . ADDON_PERSONA ] <EOL> @ json_view <EOL> @ non_atomic_requests <EOL> def ajax_search ( request ) : <EOL> """<STR_LIT>""" <EOL> search_obj = BaseAjaxSearch ( request ) <EOL> search_obj . types = amo . ADDON_SEARCH_TYPES <EOL> return search_obj . items <EOL> @ json_view <EOL> @ non_atomic_requests <EOL> def ajax_search_suggestions ( request ) : <EOL> cat = request . GET . get ( '<STR_LIT>' , '<STR_LIT:all>' ) <EOL> suggesterClass = { <EOL> '<STR_LIT:all>' : AddonSuggestionsAjax , <EOL> '<STR_LIT>' : PersonaSuggestionsAjax , <EOL> } . get ( cat , AddonSuggestionsAjax ) <EOL> suggester = suggesterClass ( request , ratings = False ) <EOL> return _build_suggestions ( <EOL> request , <EOL> cat , <EOL> suggester ) <EOL> def _build_suggestions ( request , cat , suggester ) : <EOL> results = [ ] <EOL> q = request . GET . get ( '<STR_LIT:q>' ) <EOL> if q and ( q . isdigit ( ) or len ( q ) > <NUM_LIT:2> ) : <EOL> q_ = q . lower ( ) <EOL> if cat != '<STR_LIT>' : <EOL> for a in amo . APP_USAGE : <EOL> name_ = unicode ( a . pretty ) . lower ( ) <EOL> word_matches = [ w for w in q_ . split ( ) if name_ in w ] <EOL> if q_ in name_ or word_matches : <EOL> results . append ( { <EOL> '<STR_LIT:id>' : a . id , <EOL> '<STR_LIT:name>' : _ ( u'<STR_LIT>' ) . format ( a . pretty ) , <EOL> '<STR_LIT:url>' : locale_url ( a . short ) , <EOL> '<STR_LIT>' : '<STR_LIT>' + a . short <EOL> } ) <EOL> cats = Category . objects <EOL> cats = cats . filter ( Q ( application = request . APP . id ) | <EOL> Q ( type = amo . ADDON_SEARCH ) ) <EOL> if cat == '<STR_LIT>' : <EOL> cats = cats . filter ( type = amo . ADDON_PERSONA ) <EOL> else : <EOL> cats = cats . exclude ( type = amo . ADDON_PERSONA ) <EOL> for c in cats : <EOL> if not c . name : <EOL> continue <EOL> name_ = unicode ( c . name ) . lower ( ) <EOL> word_matches = [ w for w in q_ . split ( ) if name_ in w ] <EOL> if q_ in name_ or word_matches : <EOL> results . append ( { <EOL> '<STR_LIT:id>' : c . id , <EOL> '<STR_LIT:name>' : unicode ( c . name ) , <EOL> '<STR_LIT:url>' : c . get_url_path ( ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> results += suggester . items <EOL> return results <EOL> def name_only_query ( q ) : <EOL> d = { } <EOL> rules = { '<STR_LIT>' : { '<STR_LIT>' : q , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : q , '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT:value>' : q , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:4> } , <EOL> '<STR_LIT>' : { '<STR_LIT:value>' : q , '<STR_LIT>' : <NUM_LIT> } } <EOL> for k , v in rules . iteritems ( ) : <EOL> for field in ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> d [ '<STR_LIT>' % ( field , k ) ] = v <EOL> analyzer = get_locale_analyzer ( translation . get_language ( ) ) <EOL> if analyzer : <EOL> d [ '<STR_LIT>' % analyzer ] = { '<STR_LIT>' : q , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : analyzer } <EOL> return d <EOL> def name_query ( q ) : <EOL> more = dict ( summary__match = { '<STR_LIT>' : q , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> description__match = { '<STR_LIT>' : q , '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> tags__match = { '<STR_LIT>' : q . split ( ) , '<STR_LIT>' : <NUM_LIT:0.1> } ) <EOL> analyzer = get_locale_analyzer ( translation . get_language ( ) ) <EOL> if analyzer : <EOL> more [ '<STR_LIT>' % analyzer ] = { '<STR_LIT>' : q , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : analyzer } <EOL> more [ '<STR_LIT>' % analyzer ] = { '<STR_LIT>' : q , <EOL> '<STR_LIT>' : <NUM_LIT:0.1> , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : analyzer } <EOL> return dict ( more , ** name_only_query ( q ) ) <EOL> def _filter_search ( request , qs , query , filters , sorting , <EOL> sorting_default = '<STR_LIT>' , types = [ ] ) : <EOL> """<STR_LIT>""" <EOL> APP = request . APP <EOL> show = [ f for f in filters if query . get ( f ) ] <EOL> if query . get ( '<STR_LIT:q>' ) : <EOL> qs = qs . query ( or_ = name_query ( query [ '<STR_LIT:q>' ] ) ) <EOL> if '<STR_LIT>' in show and query [ '<STR_LIT>' ] in amo . PLATFORM_DICT : <EOL> ps = ( amo . PLATFORM_DICT [ query [ '<STR_LIT>' ] ] . id , amo . PLATFORM_ALL . id ) <EOL> if ps [ <NUM_LIT:0> ] != ps [ <NUM_LIT:1> ] : <EOL> qs = qs . filter ( platform__in = ps ) <EOL> if '<STR_LIT>' in show : <EOL> low = version_int ( query [ '<STR_LIT>' ] ) <EOL> high = version_int ( query [ '<STR_LIT>' ] + '<STR_LIT:a>' ) <EOL> extensions_shown = ( not query . get ( '<STR_LIT>' ) or <EOL> query [ '<STR_LIT>' ] == amo . ADDON_EXTENSION ) <EOL> if not extensions_shown or low < version_int ( '<STR_LIT>' ) : <EOL> qs = qs . filter ( ** { '<STR_LIT>' % APP . id : high , <EOL> '<STR_LIT>' % APP . id : low } ) <EOL> if '<STR_LIT>' in show and query [ '<STR_LIT>' ] in amo . ADDON_TYPES : <EOL> qs = qs . filter ( type = query [ '<STR_LIT>' ] ) <EOL> else : <EOL> qs = qs . filter ( type__in = types ) <EOL> if '<STR_LIT>' in show : <EOL> cat = ( Category . objects . filter ( id = query [ '<STR_LIT>' ] ) <EOL> . filter ( Q ( application = APP . id ) | Q ( type = amo . ADDON_SEARCH ) ) ) <EOL> if not cat . exists ( ) : <EOL> show . remove ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in show : <EOL> qs = qs . filter ( category = query [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in show : <EOL> qs = qs . filter ( tag = query [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in show : <EOL> qs = qs . order_by ( sorting [ query [ '<STR_LIT>' ] ] ) <EOL> elif not query . get ( '<STR_LIT:q>' ) : <EOL> qs = qs . order_by ( sorting_default ) <EOL> return qs <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> @ vary_on_headers ( '<STR_LIT>' ) <EOL> @ non_atomic_requests <EOL> def search ( request , tag_name = None , template = None ) : <EOL> APP = request . APP <EOL> types = ( amo . ADDON_EXTENSION , amo . ADDON_THEME , amo . ADDON_DICT , <EOL> amo . ADDON_SEARCH , amo . ADDON_LPAPP ) <EOL> category = request . GET . get ( '<STR_LIT>' ) <EOL> if category == '<STR_LIT>' : <EOL> extra_params = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> else : <EOL> extra_params = None <EOL> fixed = fix_search_query ( request . GET , extra_params = extra_params ) <EOL> if fixed is not request . GET : <EOL> return http . HttpResponsePermanentRedirect ( urlparams ( request . path , <EOL> ** fixed ) ) <EOL> facets = request . GET . copy ( ) <EOL> if '<STR_LIT>' in facets and facets [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> facets [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> form = ESSearchForm ( facets or { } ) <EOL> form . is_valid ( ) <EOL> form_data = form . cleaned_data <EOL> if tag_name : <EOL> form_data [ '<STR_LIT>' ] = tag_name <EOL> if category == '<STR_LIT>' : <EOL> return _collections ( request ) <EOL> elif category == '<STR_LIT>' or form_data . get ( '<STR_LIT>' ) == amo . ADDON_PERSONA : <EOL> return _personas ( request ) <EOL> sort , extra_sort = split_choices ( form . sort_choices , '<STR_LIT>' ) <EOL> if form_data . get ( '<STR_LIT>' ) == amo . ADDON_SEARCH : <EOL> sort , extra_sort = list ( sort ) , list ( extra_sort ) <EOL> sort [ <NUM_LIT:1> ] = extra_sort [ <NUM_LIT:1> ] <EOL> del extra_sort [ <NUM_LIT:1> ] <EOL> qs = ( Addon . search_public ( ) . filter ( app = APP . id ) <EOL> . facet ( tags = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } , <EOL> platforms = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } , <EOL> appversions = { '<STR_LIT>' : <EOL> { '<STR_LIT>' : '<STR_LIT>' % APP . id } } , <EOL> categories = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT:200> } } ) ) <EOL> filters = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> mapping = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> qs = _filter_search ( request , qs , form_data , filters , mapping , types = types ) <EOL> pager = amo . utils . paginate ( request , qs ) <EOL> ctx = { <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : pager , <EOL> '<STR_LIT>' : form_data , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : sort , <EOL> '<STR_LIT>' : extra_sort , <EOL> '<STR_LIT>' : sort_sidebar ( request , form_data , form ) , <EOL> '<STR_LIT>' : form_data . get ( '<STR_LIT>' ) , <EOL> } <EOL> if not ctx [ '<STR_LIT>' ] : <EOL> facets = pager . object_list . facets <EOL> ctx . update ( { <EOL> '<STR_LIT>' : tag_name , <EOL> '<STR_LIT>' : category_sidebar ( request , form_data , facets ) , <EOL> '<STR_LIT>' : platform_sidebar ( request , form_data , facets ) , <EOL> '<STR_LIT>' : version_sidebar ( request , form_data , facets ) , <EOL> '<STR_LIT>' : tag_sidebar ( request , form_data , facets ) , <EOL> } ) <EOL> return render ( request , template , ctx ) <EOL> class FacetLink ( object ) : <EOL> def __init__ ( self , text , urlparams , selected = False , children = None ) : <EOL> self . text = text <EOL> self . urlparams = urlparams <EOL> self . selected = selected <EOL> self . children = children or [ ] <EOL> def sort_sidebar ( request , form_data , form ) : <EOL> sort = form_data . get ( '<STR_LIT>' ) <EOL> return [ FacetLink ( text , dict ( sort = key ) , key == sort ) <EOL> for key , text in form . sort_choices ] <EOL> def category_sidebar ( request , form_data , facets ) : <EOL> APP = request . APP <EOL> qatype , qcat = form_data . get ( '<STR_LIT>' ) , form_data . get ( '<STR_LIT>' ) <EOL> cats = [ f [ '<STR_LIT>' ] for f in facets [ '<STR_LIT>' ] ] <EOL> categories = Category . objects . filter ( id__in = cats ) <EOL> if qatype in amo . ADDON_TYPES : <EOL> categories = categories . filter ( type = qatype ) <EOL> categories = categories . filter ( Q ( application = APP . id ) | <EOL> Q ( type = amo . ADDON_SEARCH ) ) <EOL> if qcat in cats and not qatype : <EOL> qatype = True <EOL> if qcat not in categories . values_list ( '<STR_LIT:id>' , flat = True ) : <EOL> qatype = qcat = None <EOL> categories = [ ( _atype , sorted ( _cats , key = lambda x : x . name ) ) <EOL> for _atype , _cats in sorted_groupby ( categories , '<STR_LIT:type>' ) ] <EOL> rv = [ ] <EOL> cat_params = dict ( cat = None ) <EOL> all_label = _ ( u'<STR_LIT>' ) <EOL> rv = [ FacetLink ( all_label , dict ( atype = None , cat = None ) , not qatype ) ] <EOL> for addon_type , cats in categories : <EOL> selected = addon_type == qatype and not qcat <EOL> cat_params = cat_params . copy ( ) <EOL> cat_params . update ( atype = addon_type ) <EOL> link = FacetLink ( amo . ADDON_TYPES [ addon_type ] , <EOL> cat_params , selected ) <EOL> link . children = [ FacetLink ( c . name , dict ( cat_params , ** dict ( cat = c . id ) ) , <EOL> c . id == qcat ) for c in cats ] <EOL> rv . append ( link ) <EOL> return rv <EOL> def version_sidebar ( request , form_data , facets ) : <EOL> appver = '<STR_LIT>' <EOL> if '<STR_LIT>' in request . GET or form_data . get ( '<STR_LIT>' ) : <EOL> appver = form_data . get ( '<STR_LIT>' ) <EOL> app = unicode ( request . APP . pretty ) <EOL> exclude_versions = getattr ( request . APP , '<STR_LIT>' , [ ] ) <EOL> rv = [ FacetLink ( _ ( u'<STR_LIT>' ) . format ( app ) , dict ( appver = '<STR_LIT>' ) , not appver ) ] <EOL> vs = [ dict_from_int ( f [ '<STR_LIT>' ] ) for f in facets [ '<STR_LIT>' ] ] <EOL> av_dict = version_dict ( appver ) <EOL> if av_dict and av_dict not in vs and av_dict [ '<STR_LIT>' ] : <EOL> vs . append ( av_dict ) <EOL> vs = set ( ( v [ '<STR_LIT>' ] , v [ '<STR_LIT>' ] if v [ '<STR_LIT>' ] not in ( None , <NUM_LIT> ) else <NUM_LIT:0> ) <EOL> for v in vs ) <EOL> versions = [ '<STR_LIT>' % v for v in sorted ( vs , reverse = True ) ] <EOL> for version , floated in zip ( versions , map ( float , versions ) ) : <EOL> if ( floated not in exclude_versions <EOL> and floated > request . APP . min_display_version ) : <EOL> rv . append ( FacetLink ( '<STR_LIT>' % ( app , version ) , dict ( appver = version ) , <EOL> appver == version ) ) <EOL> return rv <EOL> def platform_sidebar ( request , form_data , facets ) : <EOL> qplatform = form_data . get ( '<STR_LIT>' ) <EOL> app_platforms = request . APP . platforms . values ( ) <EOL> ALL = app_platforms . pop ( <NUM_LIT:0> ) <EOL> selected = amo . PLATFORM_DICT . get ( qplatform , ALL ) <EOL> if selected != ALL and selected not in app_platforms : <EOL> app_platforms . append ( selected ) <EOL> rv = [ FacetLink ( _ ( u'<STR_LIT>' ) , dict ( platform = ALL . shortname ) , <EOL> selected == ALL ) ] <EOL> for platform in app_platforms : <EOL> rv . append ( FacetLink ( platform . name , dict ( platform = platform . shortname ) , <EOL> platform == selected ) ) <EOL> return rv <EOL> def tag_sidebar ( request , form_data , facets ) : <EOL> qtag = form_data . get ( '<STR_LIT>' ) <EOL> tags = [ facet [ '<STR_LIT>' ] for facet in facets [ '<STR_LIT>' ] ] <EOL> rv = [ FacetLink ( _ ( u'<STR_LIT>' ) , dict ( tag = None ) , not qtag ) ] <EOL> rv += [ FacetLink ( tag , dict ( tag = tag ) , tag == qtag ) for tag in tags ] <EOL> if qtag and qtag not in tags : <EOL> rv += [ FacetLink ( qtag , dict ( tag = qtag ) , True ) ] <EOL> return rv <EOL> def fix_search_query ( query , extra_params = None ) : <EOL> rv = dict ( ( smart_str ( k ) , v ) for k , v in query . items ( ) ) <EOL> changed = False <EOL> keys = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> for old , new in keys . items ( ) : <EOL> if old in query : <EOL> rv [ new ] = rv . pop ( old ) <EOL> changed = True <EOL> params = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : dict ( ( str ( p . id ) , p . shortname ) <EOL> for p in amo . PLATFORMS . values ( ) ) <EOL> } <EOL> if extra_params : <EOL> params . update ( extra_params ) <EOL> for key , fixes in params . items ( ) : <EOL> if key in rv and rv [ key ] in fixes : <EOL> rv [ key ] = fixes [ rv [ key ] ] <EOL> changed = True <EOL> return rv if changed else query <EOL> def split_choices ( choices , split ) : <EOL> """<STR_LIT>""" <EOL> index = [ idx for idx , ( key , title ) in enumerate ( choices ) <EOL> if key == split ] <EOL> if index : <EOL> index = index [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> return choices [ : index ] , choices [ index : ] <EOL> else : <EOL> return choices , [ ] </s>
