Added IPathElements interface and default adapter
authorThierry Florac <thierry.florac@onf.fr>
Wed, 20 May 2015 12:39:05 +0200
changeset 31 31d5cfdbd741
parent 30 80ae7e361ee3
child 32 c784991b55a4
Added IPathElements interface and default adapter
src/pyams_utils/interfaces/traversing.py
src/pyams_utils/traversing.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_utils/interfaces/traversing.py	Wed May 20 12:39:05 2015 +0200
@@ -0,0 +1,30 @@
+#
+# Copyright (c) 2008-2015 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
+
+# import packages
+from zope.interface import Interface
+from zope.schema import List, Int
+
+
+class IPathElements(Interface):
+    """Path elements interface"""
+
+    parents = List(title="Element parents",
+                   description="Internal IDs of parents objects",
+                   value_type=Int())
--- a/src/pyams_utils/traversing.py	Mon Apr 20 09:53:15 2015 +0200
+++ b/src/pyams_utils/traversing.py	Wed May 20 12:39:05 2015 +0200
@@ -16,12 +16,18 @@
 # import standard library
 
 # import interfaces
+from pyams_utils.interfaces.traversing import IPathElements
 from pyramid.interfaces import VH_ROOT_KEY
+from zope.intid.interfaces import IIntIds
+from zope.location.interfaces import IContained
 from zope.traversing.interfaces import ITraversable, BeforeTraverseEvent
 
 # import packages
+from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.registry import query_utility
 from pyramid.compat import decode_path_info, is_nonstr_iter
 from pyramid.exceptions import URLDecodeError, NotFound
+from pyramid.location import lineage
 from pyramid.threadlocal import get_current_registry
 from pyramid.traversal import ResourceTreeTraverser, slash, split_path_info, empty
 from zope.interface import Interface
@@ -172,3 +178,19 @@
             return interface(parent)
         parent = getattr(parent, '__parent__', None)
     return None
+
+
+@adapter_config(context=IContained, provides=IPathElements)
+class PathElementsAdapter(ContextAdapter):
+    """Contained object path elements adapter
+
+    This interface is intended to be used inside a keyword index to
+    be able to search object based on a given parent
+    """
+
+    @property
+    def parents(self):
+        intids = query_utility(IIntIds)
+        if intids is None:
+            return []
+        return [intids.register(parent) for parent in lineage(self.context)]