src/pyams_content_es/shared/view/theme.py
changeset 138 739f21526374
parent 137 fc81a5fb2225
child 159 bc34f44a9711
equal deleted inserted replaced
137:fc81a5fb2225 138:739f21526374
       
     1 #
       
     2 # Copyright (c) 2008-2015 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 from elasticsearch_dsl import Q
       
    16 from zope.intid.interfaces import IIntIds
       
    17 
       
    18 from pyams_content.component.theme import ITagsManager
       
    19 from pyams_content.shared.view.interfaces import IViewCollectionsSettings, IViewQueryEsParamsExtension, \
       
    20     IViewTagsSettings, IViewThemesSettings, IViewUserQuery, IWfView
       
    21 from pyams_content_es.shared.view import EsSearchFolderQuery
       
    22 from pyams_thesaurus.interfaces.thesaurus import IThesaurus
       
    23 from pyams_utils.adapter import ContextAdapter, adapter_config
       
    24 from pyams_utils.registry import query_utility
       
    25 
       
    26 
       
    27 #
       
    28 # Tags query
       
    29 #
       
    30 
       
    31 @adapter_config(name='tags', context=IWfView, provides=IViewQueryEsParamsExtension)
       
    32 class ViewTagsQueryEsParamsExtension(ContextAdapter):
       
    33     """View tags query adapter """
       
    34 
       
    35     weight = 60
       
    36 
       
    37     def get_es_params(self, context, request=None):
       
    38         settings = IViewTagsSettings(self.context)
       
    39         tags = settings.get_tags_index(context)
       
    40         if tags:
       
    41             yield Q('terms', **{'tags': tags})
       
    42         elif settings.select_context_tags:
       
    43             yield None
       
    44 
       
    45 
       
    46 @adapter_config(name='tag', context=EsSearchFolderQuery, provides=IViewUserQuery)
       
    47 class EsSearchFolderTagQuery(ContextAdapter):
       
    48     """Search folder tag query for Elasticsearch"""
       
    49 
       
    50     @staticmethod
       
    51     def get_user_params(request):
       
    52         tag = request.params.get('tag')
       
    53         if tag:
       
    54             manager = ITagsManager(request.root, None)
       
    55             if manager is None:
       
    56                 raise StopIteration
       
    57             thesaurus = query_utility(IThesaurus, name=manager.thesaurus_name)
       
    58             if thesaurus is None:
       
    59                 raise StopIteration
       
    60             term = thesaurus.terms.get(tag)
       
    61             if term is not None:
       
    62                 intids = query_utility(IIntIds)
       
    63                 yield Q('term', **{'tags': intids.queryId(term)})
       
    64 
       
    65 
       
    66 #
       
    67 # Themes query
       
    68 #
       
    69 
       
    70 @adapter_config(name='themes', context=IWfView, provides=IViewQueryEsParamsExtension)
       
    71 class ViewThemesQueryEsParamsExtension(ContextAdapter):
       
    72     """View themes query adapter """
       
    73 
       
    74     weight = 62
       
    75 
       
    76     def get_es_params(self, context, request=None):
       
    77         settings = IViewThemesSettings(self.context)
       
    78         themes = settings.get_themes_index(context)
       
    79         if themes:
       
    80             yield Q('terms', **{'themes.terms': themes})
       
    81         elif settings.select_context_themes:
       
    82             yield None
       
    83 
       
    84 
       
    85 #
       
    86 # Collections query
       
    87 #
       
    88 
       
    89 @adapter_config(name='collections', context=IWfView, provides=IViewQueryEsParamsExtension)
       
    90 class ViewCollectionsQueryEsParamsExtension(ContextAdapter):
       
    91     """View collections query adapter """
       
    92 
       
    93     weight = 64
       
    94 
       
    95     def get_es_params(self, context, request=None):
       
    96         settings = IViewCollectionsSettings(self.context)
       
    97         collections = settings.get_collections_index(context)
       
    98         if collections:
       
    99             yield Q('terms', **{'collections': collections})
       
   100         elif settings.select_context_collections:
       
   101             yield None