当前位置: 首页>>代码示例>>Python>>正文


Python EmptySearchQuerySet.facet方法代码示例

本文整理汇总了Python中haystack.query.EmptySearchQuerySet.facet方法的典型用法代码示例。如果您正苦于以下问题:Python EmptySearchQuerySet.facet方法的具体用法?Python EmptySearchQuerySet.facet怎么用?Python EmptySearchQuerySet.facet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在haystack.query.EmptySearchQuerySet的用法示例。


在下文中一共展示了EmptySearchQuerySet.facet方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get

# 需要导入模块: from haystack.query import EmptySearchQuerySet [as 别名]
# 或者: from haystack.query.EmptySearchQuerySet import facet [as 别名]
 def get(self, request, fmt=None):
     context = {}
     if fmt:
         query = ''
         results = EmptySearchQuerySet()
         form = self.form_class(request.GET, searchqueryset=self.searchqueryset, load_all=True)
         if form.is_valid():
             print form.cleaned_data
             query = form.cleaned_data['q']
             order_by = request.GET.get('order_by', None)
             results = form.search(order_by)
         else:
             results = form.no_query_found()
         if self.facets:
             for facet in self.facets:
                 results = results.facet(facet[0], mincount=1, sort=facet[1])
             context['facets'] = results.facet_counts()
         paginator = Paginator(results, 25)
         page = request.GET.get('page', '1')
         try:
             content = paginator.page(page)
         except PageNotAnInteger:
             content = paginator.page(1)
         except EmptyPage:
             content = paginator.page(paginator.num_pages)
         context['content'] = content
         context['paginator'] = paginator
         context['query'] = query
         context['form'] = form
         return self.render_to_format(request, context, self.template_name, fmt)
     else:
         params = []
         if not request.GET.get('q') and not request.GET.get('order_by'):
             params.append('order_by={}'.format(self.default_order))
         for param in self.params:
             print param['name']
             param_value = request.GET.get(param['name'], param['default'])
             if param_value:
                 params.append('{}={}'.format(param['name'], param_value))
         print params
         context['params'] = '&'.join(params)
         context['status_code'] = 303
         context['additional_headers'] = {'location': self.path}
         context['content'] = None
         return self.render(request, context, self.template_name)
开发者ID:wragge,项目名称:chaf-django,代码行数:47,代码来源:views.py

示例2: catalogue_search

# 需要导入模块: from haystack.query import EmptySearchQuerySet [as 别名]
# 或者: from haystack.query.EmptySearchQuerySet import facet [as 别名]
def catalogue_search(request, template='search/search.html', load_all=True,
    form_class=CatalogueSearchForm, searchqueryset=None, extra_context=None,
    results_per_page=None):
    """
    A more traditional view that also demonstrate an alternative
    way to use Haystack.

    Useful as an example of for basing heavily custom views off of.

    Also has the benefit of thread-safety, which the ``SearchView`` class may
    not be.

    Template:: ``search/search.html``
    Context::
        * form
          An instance of the ``form_class``. (default: ``ModelSearchForm``)
        * page
          The current page of search results.
        * paginator
          A paginator instance for the results.
        * query
          The query received by the form.
    """
    query = ''
    results = EmptySearchQuerySet()
    results_per_page = results_per_page or RESULTS_PER_PAGE

    if request.GET.get('q'):
        form = form_class(request.GET, searchqueryset=searchqueryset, load_all=load_all)

        if form.is_valid():
            query = form.cleaned_data['q']
            results = form.search()
    else:
        form = form_class(searchqueryset=searchqueryset, load_all=load_all)

    results = results.facet('categories').facet('country').facet('has_images').facet('global_region').facet('item_name')

    if form.is_valid():
        results = filter_with_facet(form, results, 'item_name', 'item_name')
        results = filter_with_facet(form, results, 'category', 'categories')
        results = filter_with_facet(form, results, 'global_region', 'global_region')
        results = filter_with_facet(form, results, 'country', 'country')

        if form.cleaned_data['person']:
            sqs = SearchQuerySet()
            for p in form.cleaned_data['person'].split():
                term = sqs.query.clean(p)
                results = results.narrow(u'people:"%s"' % term)

        if form.cleaned_data['has_images'] == True:
            results = results.narrow(u'has_images_exact:true')

    facets = results.facet_counts()

    if facets:
        # Prepare the form with all the available facets
        load_facets_into_form(form, facets, 'item_name', 'item_name')
        load_facets_into_form(form, facets, 'category', 'categories')
        load_facets_into_form(form, facets, 'global_region', 'global_region')
        load_facets_into_form(form, facets, 'country', 'country')

        # Append count of images into form
        appended_count = False
        for name, val in facets['fields']['has_images']:
            if name == 'true':
                form.fields['has_images'].label += ' (%s)' % val
                appended_count = True
        if not appended_count:
            form.fields['has_images'].label += ' (0)'

    results = results.order_by('registration_number')

    paginator = Paginator(results, results_per_page)

    try:
        page = paginator.page(int(request.GET.get('page', 1)))
    except InvalidPage:
        raise Http404("No such page of results!")

    context = {
        'form': form,
        'page': page,
        'paginator': paginator,
        'query': query,
        'suggestion': None,
        'facets': facets,
    }

    # Store query for paging through results on item detail page
    if form.is_valid():
        request.session['search_query'] = form.cleaned_data
    # Access the first result to prevent ZeroDivisionError
    if results:
        results[0]
    request.session['search_results'] = results
    request.session['search_results_per_page'] = results_per_page

    if getattr(settings, 'HAYSTACK_INCLUDE_SPELLING', False):
        context['suggestion'] = form.get_suggestion()
#.........这里部分代码省略.........
开发者ID:uq-eresearch,项目名称:uqam,代码行数:103,代码来源:views.py


注:本文中的haystack.query.EmptySearchQuerySet.facet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。