def _collections(request): """Handle the request for collections.""" # Sorting by relevance isn't an option. Instead the default is `weekly`. initial = {'sort': 'weekly'} # Update with GET variables. initial.update(request.GET.items()) # Ignore appver/platform and set default number of collections per page. initial.update(appver=None, platform=None, pp=DEFAULT_NUM_COLLECTIONS) form = SecondarySearchForm(initial) form.is_valid() qs = Collection.search().filter(listed=True, app=request.APP.id) filters = ['sort'] mapping = { 'weekly': '-weekly_subscribers', 'monthly': '-monthly_subscribers', 'all': '-subscribers', 'rating': '-rating', 'created': '-created', 'name': 'name_sort', 'updated': '-modified' } results = _filter_search(request, qs, form.cleaned_data, filters, sorting=mapping, sorting_default='-weekly_subscribers', types=amo.COLLECTION_SEARCH_CHOICES) form_data = form.cleaned_data.get('q', '') search_opts = {} search_opts['limit'] = form.cleaned_data.get('pp', DEFAULT_NUM_COLLECTIONS) page = form.cleaned_data.get('page') or 1 search_opts['offset'] = (page - 1) * search_opts['limit'] search_opts['sort'] = form.cleaned_data.get('sort') pager = amo.utils.paginate(request, results, per_page=search_opts['limit']) context = { 'pager': pager, 'form': form, 'query': form_data, 'opts': search_opts, 'filter': get_filter_view(request), 'search_placeholder': 'collections' } return render(request, 'search/collections.html', context)
def _collections(request): """Handle the request for collections.""" # Sorting by relevance isn't an option. Instead the default is `weekly`. initial = {'sort': 'weekly'} # Update with GET variables. initial.update(request.GET.items()) # Ignore appver/platform and set default number of collections per page. initial.update(appver=None, platform=None, pp=DEFAULT_NUM_COLLECTIONS) form = SecondarySearchForm(initial) form.is_valid() qs = Collection.search().filter(listed=True, app=request.APP.id) filters = ['sort'] mapping = { 'weekly': '-weekly_subscribers', 'monthly': '-monthly_subscribers', 'all': '-subscribers', 'rating': '-rating', 'created': '-created', 'name': 'name_sort', 'updated': '-modified'} results = _filter_search(request, qs, form.cleaned_data, filters, sorting=mapping, sorting_default='-weekly_subscribers', types=amo.COLLECTION_SEARCH_CHOICES) form_data = form.cleaned_data.get('q', '') search_opts = {} search_opts['limit'] = form.cleaned_data.get('pp', DEFAULT_NUM_COLLECTIONS) page = form.cleaned_data.get('page') or 1 search_opts['offset'] = (page - 1) * search_opts['limit'] search_opts['sort'] = form.cleaned_data.get('sort') pager = amo.utils.paginate(request, results, per_page=search_opts['limit']) context = { 'pager': pager, 'form': form, 'query': form_data, 'opts': search_opts, 'filter': get_filter_view(request), 'search_placeholder': 'collections'} return render(request, 'search/collections.html', context)
def es_collections_json(request): app = request.GET.get('app', '') q = request.GET.get('q', '') qs = Collection.search() try: qs = qs.query(id__startswith=int(q)) except ValueError: qs = qs.query(name__match=q) try: qs = qs.filter(app=int(app)) except ValueError: pass data = [] for c in qs[:7]: data.append({'id': c.id, 'name': unicode(c.name), 'all_personas': c.all_personas, 'url': c.get_url_path()}) return data