Added site contents vocabulary
authorThierry Florac <tflorac@ulthar.net>
Fri, 13 Nov 2020 13:08:48 +0100
changeset 1427 43bd56f3a726
parent 1426 ea7a363b538e
child 1428 48c1ee485dc0
Added site contents vocabulary
src/pyams_content/shared/site/container.py
--- a/src/pyams_content/shared/site/container.py	Fri Nov 13 13:04:58 2020 +0100
+++ b/src/pyams_content/shared/site/container.py	Fri Nov 13 13:08:48 2020 +0100
@@ -17,18 +17,24 @@
 from pyramid.location import lineage
 from zope.interface import implementer
 from zope.intid.interfaces import IIntIds
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 
 from pyams_content.shared.common.interfaces import ISharedContentFactory
 from pyams_content.shared.site import SiteTopic
-from pyams_content.shared.site.interfaces import ISiteContainer, ISiteElementNavigation, ISiteFolder
+from pyams_content.shared.site.interfaces import IBaseSiteItem, ISiteContainer, \
+    ISiteElementNavigation, \
+    ISiteFolder, ISiteManager
 from pyams_i18n.interfaces import II18n
 from pyams_utils.adapter import adapter_config
-from pyams_utils.registry import get_global_registry, get_utility
+from pyams_utils.container import find_objects_providing
+from pyams_utils.registry import get_local_registry, get_global_registry, get_utility
 from pyams_utils.request import check_request, query_request
+from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
 
 
 @implementer(ISiteContainer)
-class SiteContainerMixin(object):
+class SiteContainerMixin:
     """Site container mixin class"""
 
     def get_visible_items(self, request=None):
@@ -97,3 +103,24 @@
 @adapter_config(context=ISiteContainer, provides=ISharedContentFactory)
 def site_container_topic_factory(context):
     return SiteTopic
+
+
+@vocabulary_config(name='PyAMS site contents')
+class SiteManagerContentsVocabulary(SimpleVocabulary):
+    """Site manager folders vocabulary"""
+
+    def __init__(self, context):
+        terms = []
+        site = get_parent(context, ISiteManager)
+        if site is not None:
+            registry = get_local_registry()
+            if registry is not None:
+                request = query_request()
+                intids = registry.getUtility(IIntIds)
+                for folder, depth in find_objects_providing(site, IBaseSiteItem, with_depth=True):
+                    terms.append(SimpleTerm(value=intids.queryId(folder),
+                                            title='{}{}'.format(
+                                                '- ' * depth,
+                                                II18n(folder).query_attribute('title',
+                                                                              request=request))))
+        super(SiteManagerContentsVocabulary, self).__init__(terms)