src/pyams_content_es/component/theme.py
changeset 137 fc81a5fb2225
parent 115 6387a1195db4
child 159 bc34f44a9711
equal deleted inserted replaced
136:68ae22131209 137:fc81a5fb2225
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
    13 __docformat__ = 'restructuredtext'
    13 __docformat__ = 'restructuredtext'
    14 
    14 
    15 from elasticsearch_dsl import Q
       
    16 from zope.intid.interfaces import IIntIds
    15 from zope.intid.interfaces import IIntIds
    17 
    16 
    18 from pyams_content.component.theme import ICollectionsInfo, ICollectionsTarget, ITagsManager
    17 from pyams_content.component.theme import ICollectionsInfo, ICollectionsTarget
    19 from pyams_content.component.theme.interfaces import ITagsInfo, ITagsTarget, IThemesInfo, IThemesTarget
    18 from pyams_content.component.theme.interfaces import ITagsInfo, ITagsTarget, IThemesInfo, IThemesTarget
    20 from pyams_content.shared.view.interfaces import IViewCollectionsSettings, IViewQueryEsParamsExtension, \
       
    21     IViewTagsSettings, IViewThemesSettings, IViewUserQuery, IWfView
       
    22 from pyams_content_es.interfaces import IDocumentIndexInfo
    19 from pyams_content_es.interfaces import IDocumentIndexInfo
    23 from pyams_content_es.shared.view import EsSearchFolderQuery
    20 from pyams_utils.adapter import adapter_config
    24 from pyams_thesaurus.interfaces.thesaurus import IThesaurus
       
    25 from pyams_utils.adapter import ContextAdapter, adapter_config
       
    26 from pyams_utils.list import unique
    21 from pyams_utils.list import unique
    27 from pyams_utils.registry import get_utility, query_utility
    22 from pyams_utils.registry import get_utility
    28 
    23 
    29 
    24 
    30 #
    25 #
    31 # Tags index
    26 # Tags index
    32 #
    27 #
    37     intids = get_utility(IIntIds)
    32     intids = get_utility(IIntIds)
    38     tags = ITagsInfo(content).tags or ()
    33     tags = ITagsInfo(content).tags or ()
    39     return {
    34     return {
    40         'tags': [intids.register(tag) for tag in unique(tags)]
    35         'tags': [intids.register(tag) for tag in unique(tags)]
    41     }
    36     }
    42 
       
    43 
       
    44 @adapter_config(name='tags', context=IWfView, provides=IViewQueryEsParamsExtension)
       
    45 class ViewTagsQueryEsParamsExtension(ContextAdapter):
       
    46     """View tags query adapter """
       
    47 
       
    48     weight = 60
       
    49 
       
    50     def get_es_params(self, context, request=None):
       
    51         settings = IViewTagsSettings(self.context)
       
    52         tags = settings.get_tags_index(context)
       
    53         if tags:
       
    54             yield Q('terms', **{'tags': tags})
       
    55 
       
    56 
       
    57 @adapter_config(name='tag', context=EsSearchFolderQuery, provides=IViewUserQuery)
       
    58 class EsSearchFolderTagQuery(ContextAdapter):
       
    59     """Search folder tag query for Elasticsearch"""
       
    60 
       
    61     @staticmethod
       
    62     def get_user_params(request):
       
    63         tag = request.params.get('tag')
       
    64         if tag:
       
    65             manager = ITagsManager(request.root, None)
       
    66             if manager is None:
       
    67                 raise StopIteration
       
    68             thesaurus = query_utility(IThesaurus, name=manager.thesaurus_name)
       
    69             if thesaurus is None:
       
    70                 raise StopIteration
       
    71             term = thesaurus.terms.get(tag)
       
    72             if term is not None:
       
    73                 intids = query_utility(IIntIds)
       
    74                 yield Q('term', **{'tags': intids.queryId(term)})
       
    75 
    37 
    76 
    38 
    77 #
    39 #
    78 # Themes index
    40 # Themes index
    79 #
    41 #
   101             'associations': [intids.register(term) for term in unique(associations)]
    63             'associations': [intids.register(term) for term in unique(associations)]
   102         }
    64         }
   103     }
    65     }
   104 
    66 
   105 
    67 
   106 @adapter_config(name='themes', context=IWfView, provides=IViewQueryEsParamsExtension)
       
   107 class ViewThemesQueryEsParamsExtension(ContextAdapter):
       
   108     """View themes query adapter """
       
   109 
       
   110     weight = 62
       
   111 
       
   112     def get_es_params(self, context, request=None):
       
   113         settings = IViewThemesSettings(self.context)
       
   114         themes = settings.get_themes_index(context)
       
   115         if themes:
       
   116             yield Q('terms', **{'themes.terms': themes})
       
   117 
       
   118 
       
   119 #
    68 #
   120 # Collections index
    69 # Collections index
   121 #
    70 #
   122 
    71 
   123 @adapter_config(name='collections', context=ICollectionsTarget, provides=IDocumentIndexInfo)
    72 @adapter_config(name='collections', context=ICollectionsTarget, provides=IDocumentIndexInfo)
   126     intids = get_utility(IIntIds)
    75     intids = get_utility(IIntIds)
   127     collections = ICollectionsInfo(content).collections or ()
    76     collections = ICollectionsInfo(content).collections or ()
   128     return {
    77     return {
   129         'collections': [intids.register(tag) for tag in unique(collections)]
    78         'collections': [intids.register(tag) for tag in unique(collections)]
   130     }
    79     }
   131 
       
   132 
       
   133 @adapter_config(name='collections', context=IWfView, provides=IViewQueryEsParamsExtension)
       
   134 class ViewCollectionsQueryEsParamsExtension(ContextAdapter):
       
   135     """View collections query adapter """
       
   136 
       
   137     weight = 64
       
   138 
       
   139     def get_es_params(self, context, request=None):
       
   140         settings = IViewCollectionsSettings(self.context)
       
   141         collections = settings.get_collections_index(context)
       
   142         if collections:
       
   143             yield Q('terms', **{'collections': collections})