Added internal references params extension
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Jun 2018 14:57:59 +0200
changeset 65 7ef9d814b2d2
parent 64 fc04752a10a5
child 66 a213457e7a7c
Added internal references params extension
src/pyams_content_es/component/reference.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content_es/component/reference.py	Mon Jun 11 14:57:59 2018 +0200
@@ -0,0 +1,38 @@
+#
+# Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
+# 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)))