# HG changeset patch # User Thierry Florac # Date 1530544349 -7200 # Node ID 1bfca7ed440bc9a7a46c628f38bc0bd36349cd8a # Parent a991347f4317c2ecd8bfb36c52977f30662a6bcb Reorganized package diff -r a991347f4317 -r 1bfca7ed440b src/pyams_content_es/component/reference.py --- a/src/pyams_content_es/component/reference.py Sun Jul 01 08:55:45 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -# -# Copyright (c) 2008-2018 Thierry Florac -# All Rights Reserved. -# -# This software is subject to the provisions of the Zope Public License, -# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. -# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED -# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS -# FOR A PARTICULAR PURPOSE. -# - -__docformat__ = 'restructuredtext' - - -# import standard library - -# import interfaces -from elasticsearch_dsl import Q -from pyams_content.shared.view.interfaces import IWfView, IViewQueryEsParamsExtension, IViewInternalReferencesSettings -from zope.intid.interfaces import IIntIds - -# import packages -from pyams_utils.adapter import adapter_config, ContextAdapter -from pyams_utils.registry import get_utility - - -@adapter_config(name='references', context=IWfView, provides=IViewQueryEsParamsExtension) -class ViewInternalReferencesQueryEsParamsExtension(ContextAdapter): - """View internal references query params extension""" - - weight = 50 - - def get_es_params(self, context): - settings = IViewInternalReferencesSettings(self.context) - if settings.exclude_context: - intids = get_utility(IIntIds) - return Q('bool', must_not=Q('term', internal_id=intids.register(context))) diff -r a991347f4317 -r 1bfca7ed440b src/pyams_content_es/component/view.py --- a/src/pyams_content_es/component/view.py Sun Jul 01 08:55:45 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -# -# Copyright (c) 2008-2015 Thierry Florac -# All Rights Reserved. -# -# This software is subject to the provisions of the Zope Public License, -# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. -# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED -# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS -# FOR A PARTICULAR PURPOSE. -# - -__docformat__ = 'restructuredtext' - - -# import standard library -from datetime import datetime - -# import interfaces -from pyams_content.shared.view.interfaces import IWfView, IViewQuery, IViewQueryEsParamsExtension, \ - IViewQueryFilterExtension -from pyams_workflow.interfaces import IWorkflow - -# import packages -from elasticsearch_dsl import Search, Q -from pyams_catalog.query import CatalogResultSet -from pyams_utils.adapter import adapter_config, ContextAdapter -from pyams_utils.list import unique -from pyams_utils.request import check_request -from pyams_utils.timezone import tztime -from pyramid.threadlocal import get_current_registry -from pyramid_es import get_client - - -@adapter_config(name='es', context=IWfView, provides=IViewQuery) -class EsViewQuery(ContextAdapter): - """View query for Elasticsearch""" - - def get_es_params(self, context): - view = self.context - registry = get_current_registry() - # check publication dates - now = tztime(datetime.utcnow()) - params = Q('range', **{'workflow.effective_date': {'lte': now}}) - params &= Q('bool', must=Q('range', **{'workflow.push_end_date': {'gte': now}})) | \ - Q('bool', must_not=Q('exists', **{'field': 'workflow.push_end_date'})) - # check content types - content_types = view.get_content_types(context) - if content_types: - params &= Q('terms', **{'content_type': content_types}) - # check workflow states - wf_params = [] - for workflow in registry.getAllUtilitiesRegisteredFor(IWorkflow): - wf_params.extend(workflow.published_states) - params &= Q('terms', **{'workflow.status': wf_params}) - # check custom extensions - for name, adapter in sorted(registry.getAdapters((view,), IViewQueryEsParamsExtension), - key=lambda x: x[1].weight): - new_params = adapter.get_es_params(context) - if new_params: - params &= new_params - return params - - def get_results(self, context, sort_index, reverse, limit): - request = check_request() - registry = request.registry - client = get_client(request) - params = self.get_es_params(context) - search = Search(using=client.es, index=client.index) \ - .query(params) \ - .sort('{0}workflow.{1}'.format('-' if reverse else '', - sort_index)) \ - .source(['internal_id']) - if limit: - search = search[:limit] - items = CatalogResultSet([result.internal_id for result in search]) - for name, adapter in sorted(registry.getAdapters((self.context,), IViewQueryFilterExtension), - key=lambda x: x[1].weight): - items = adapter.filter(context, items) - return unique(items) diff -r a991347f4317 -r 1bfca7ed440b src/pyams_content_es/shared/__init__.py diff -r a991347f4317 -r 1bfca7ed440b src/pyams_content_es/shared/view/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_content_es/shared/view/__init__.py Mon Jul 02 17:12:29 2018 +0200 @@ -0,0 +1,80 @@ +# +# Copyright (c) 2008-2015 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# + +__docformat__ = 'restructuredtext' + + +# import standard library +from datetime import datetime + +# import interfaces +from pyams_content.shared.view.interfaces import IWfView, IViewQuery, IViewQueryEsParamsExtension, \ + IViewQueryFilterExtension +from pyams_workflow.interfaces import IWorkflow + +# import packages +from elasticsearch_dsl import Search, Q +from pyams_catalog.query import CatalogResultSet +from pyams_utils.adapter import adapter_config, ContextAdapter +from pyams_utils.list import unique_iter +from pyams_utils.request import check_request +from pyams_utils.timezone import tztime +from pyramid.threadlocal import get_current_registry +from pyramid_es import get_client + + +@adapter_config(name='es', context=IWfView, provides=IViewQuery) +class EsViewQuery(ContextAdapter): + """View query for Elasticsearch""" + + def get_es_params(self, context): + view = self.context + registry = get_current_registry() + # check publication dates + now = tztime(datetime.utcnow()) + params = Q('range', **{'workflow.effective_date': {'lte': now}}) + params &= Q('bool', must=Q('range', **{'workflow.push_end_date': {'gte': now}})) | \ + Q('bool', must_not=Q('exists', **{'field': 'workflow.push_end_date'})) + # check content types + content_types = view.get_content_types(context) + if content_types: + params &= Q('terms', **{'content_type': content_types}) + # check workflow states + wf_params = [] + for workflow in registry.getAllUtilitiesRegisteredFor(IWorkflow): + wf_params.extend(workflow.published_states) + params &= Q('terms', **{'workflow.status': wf_params}) + # check custom extensions + for name, adapter in sorted(registry.getAdapters((view,), IViewQueryEsParamsExtension), + key=lambda x: x[1].weight): + new_params = adapter.get_es_params(context) + if new_params: + params &= new_params + return params + + def get_results(self, context, sort_index, reverse, limit): + request = check_request() + registry = request.registry + client = get_client(request) + params = self.get_es_params(context) + search = Search(using=client.es, index=client.index) \ + .query(params) \ + .sort('{0}workflow.{1}'.format('-' if reverse else '', + sort_index)) \ + .source(['internal_id']) + if limit: + search = search[:limit] + items = CatalogResultSet([result.internal_id for result in search]) + for name, adapter in sorted(registry.getAdapters((self.context,), IViewQueryFilterExtension), + key=lambda x: x[1].weight): + items = adapter.filter(context, items) + return unique_iter(items) diff -r a991347f4317 -r 1bfca7ed440b src/pyams_content_es/shared/view/reference.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_content_es/shared/view/reference.py Mon Jul 02 17:12:29 2018 +0200 @@ -0,0 +1,38 @@ +# +# Copyright (c) 2008-2018 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# + +__docformat__ = 'restructuredtext' + + +# import standard library + +# import interfaces +from elasticsearch_dsl import Q +from pyams_content.shared.view.interfaces import IWfView, IViewQueryEsParamsExtension, IViewInternalReferencesSettings +from zope.intid.interfaces import IIntIds + +# import packages +from pyams_utils.adapter import adapter_config, ContextAdapter +from pyams_utils.registry import get_utility + + +@adapter_config(name='references', context=IWfView, provides=IViewQueryEsParamsExtension) +class ViewInternalReferencesQueryEsParamsExtension(ContextAdapter): + """View internal references query params extension""" + + weight = 50 + + def get_es_params(self, context): + settings = IViewInternalReferencesSettings(self.context) + if settings.exclude_context: + intids = get_utility(IIntIds) + return Q('bool', must_not=Q('term', internal_id=intids.register(context)))