src/pyams_content/features/search/portlet/__init__.py
changeset 1121 852aa448da04
child 1157 ffb751b038cc
equal deleted inserted replaced
1120:52ed8cc1c947 1121:852aa448da04
       
     1 #
       
     2 # Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 import math
       
    16 
       
    17 from zope.interface import implementer
       
    18 from zope.schema.fieldproperty import FieldProperty
       
    19 
       
    20 from pyams_content.features.search import ISearchFolder
       
    21 from pyams_content.features.search.portlet.interfaces import ISearchResultsPortletSettings
       
    22 from pyams_content.shared.view.interfaces import RELEVANCE_ORDER
       
    23 from pyams_portal.portlet import Portlet, PortletSettings, portlet_config
       
    24 from pyams_utils.factory import factory_config
       
    25 from pyams_utils.interfaces import VIEW_PERMISSION
       
    26 from pyams_utils.request import check_request
       
    27 from pyams_utils.traversing import get_parent
       
    28 
       
    29 from pyams_content import _
       
    30 
       
    31 
       
    32 SEARCH_RESULTS_PORTLET_NAME = 'pyams_content.portlet.search.results'
       
    33 
       
    34 
       
    35 @implementer(ISearchResultsPortletSettings)
       
    36 @factory_config(provided=ISearchResultsPortletSettings)
       
    37 class SearchResultsPortletSettings(PortletSettings):
       
    38     """Search results portlet settings"""
       
    39 
       
    40     title = FieldProperty(ISearchResultsPortletSettings['title'])
       
    41 
       
    42     @staticmethod
       
    43     def get_items(request=None, limit=None, ignore_cache=False):
       
    44         context = get_parent(request.context, ISearchFolder)
       
    45         if context is None:
       
    46             raise StopIteration
       
    47         if request is None:
       
    48             request = check_request()
       
    49         params = request.params
       
    50         sort_index = params.get('order_by', RELEVANCE_ORDER)
       
    51         yield from context.get_results(context, sort_index,
       
    52                                        reverse=sort_index != RELEVANCE_ORDER,
       
    53                                        limit=limit,
       
    54                                        start=int(params.get('start', 0)),
       
    55                                        length=int(params.get('length', 10)),
       
    56                                        ignore_cache=ignore_cache,
       
    57                                        get_count=True)
       
    58 
       
    59     @staticmethod
       
    60     def get_pages(request, count):
       
    61         params = request.params
       
    62         start = int(params.get('start', 0)) + 1
       
    63         length = int(params.get('length', 10))
       
    64         current = math.ceil(start / length)
       
    65         nb_pages = math.ceil(count / length)
       
    66         return current, nb_pages
       
    67 
       
    68 
       
    69 @portlet_config(permission=VIEW_PERMISSION)
       
    70 class SearchResultsPortlet(Portlet):
       
    71     """Search results portlet"""
       
    72 
       
    73     name = SEARCH_RESULTS_PORTLET_NAME
       
    74     label = _("Search results")
       
    75 
       
    76     toolbar_css_class = 'fa fa-fw fa-2x fa-search-plus'
       
    77 
       
    78     settings_factory = ISearchResultsPortletSettings