Updated dynamic menus management
authorThierry Florac <thierry.florac@onf.fr>
Tue, 11 Dec 2018 13:50:27 +0100
changeset 1154 8e16cc0efae5
parent 1153 9664ae4c8abb
child 1155 f944a4efb65b
Updated dynamic menus management
src/pyams_content/component/links/__init__.py
src/pyams_content/features/menu/__init__.py
src/pyams_content/features/menu/interfaces.py
src/pyams_content/shared/site/folder.py
src/pyams_content/shared/site/link.py
--- a/src/pyams_content/component/links/__init__.py	Tue Dec 11 10:42:37 2018 +0100
+++ b/src/pyams_content/component/links/__init__.py	Tue Dec 11 13:50:27 2018 +0100
@@ -213,13 +213,6 @@
         return output
 
 
-def get_dynamic_link(target):
-    """Create dynamic internal link object from target"""
-    result = InternalLink()
-    result.reference = ISequentialIdInfo(target).hex_oid
-    return result
-
-
 #
 # External links
 #
--- a/src/pyams_content/features/menu/__init__.py	Tue Dec 11 10:42:37 2018 +0100
+++ b/src/pyams_content/features/menu/__init__.py	Tue Dec 11 13:50:27 2018 +0100
@@ -17,10 +17,12 @@
 
 from pyams_content.component.association.container import AssociationContainer
 from pyams_content.component.association.interfaces import IAssociationInfo
-from pyams_content.component.links import InternalLinkAssociationInfoAdapter, InternalReferenceMixin, get_dynamic_link
-from pyams_content.features.menu.interfaces import IMenu, IMenuLink, IMenusContainer
+from pyams_content.component.links import InternalLink, InternalLinkAssociationInfoAdapter, InternalReferenceMixin
+from pyams_content.features.menu.interfaces import IDynamicMenu, IMenu, IMenuLink, IMenusContainer
 from pyams_content.reference.pictograms import IPictogramTable
+from pyams_content.shared.common import IWfSharedContent
 from pyams_content.shared.site.interfaces import ISiteContainer
+from pyams_sequence.interfaces import ISequentialIdInfo, ISequentialIdTarget
 from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import query_utility
 from pyams_utils.request import check_request
@@ -71,8 +73,9 @@
 
     def get_visible_items(self, request=None):
         if self.dynamic_menu and ISiteContainer.providedBy(self.target):
-            yield from map(get_dynamic_link,
-                           self.target.get_visible_items(request))
+            yield from filter(None,
+                              map(lambda x: IDynamicMenu(x, None),
+                                  self.target.get_visible_items(request)))
         yield from super(Menu, self).get_visible_items(request)
 
     def get_url(self, request=None, view_name=None):
@@ -98,3 +101,14 @@
         for menu in filter(lambda x: IMenu(x).visible, self.values()):
             if IAdminLayer.providedBy(request) or menu.is_visible(request):
                 yield menu
+
+
+@adapter_config(context=ISequentialIdTarget, provides=IDynamicMenu)
+@adapter_config(context=IWfSharedContent, provides=IDynamicMenu)
+def content_dynamic_menu_factory(context):
+    """Shared content dynamic menu factory"""
+    sequence = ISequentialIdInfo(context, None)
+    if sequence is not None:
+        result = InternalLink()
+        result.reference = sequence.hex_oid
+        return result
--- a/src/pyams_content/features/menu/interfaces.py	Tue Dec 11 10:42:37 2018 +0100
+++ b/src/pyams_content/features/menu/interfaces.py	Tue Dec 11 13:50:27 2018 +0100
@@ -37,6 +37,10 @@
     """Menu external link marker interface"""
 
 
+class IDynamicMenu(Interface):
+    """Dynamic menu interface"""
+
+
 class IMenuLinksContainer(IAssociationContainer):
     """Menu links container interface"""
 
--- a/src/pyams_content/shared/site/folder.py	Tue Dec 11 10:42:37 2018 +0100
+++ b/src/pyams_content/shared/site/folder.py	Tue Dec 11 13:50:27 2018 +0100
@@ -19,6 +19,8 @@
 from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 
 from pyams_content.component.illustration import IIllustrationTarget, ILinkIllustrationTarget
+from pyams_content.component.links import InternalLink
+from pyams_content.features.menu import IDynamicMenu
 from pyams_content.features.preview.interfaces import IPreviewTarget
 from pyams_content.interfaces import MANAGE_SITE_PERMISSION
 from pyams_content.shared.common.manager import BaseSharedTool
@@ -28,6 +30,7 @@
 from pyams_i18n.interfaces import II18n
 from pyams_portal.interfaces import IPortalContext
 from pyams_security.interfaces import IDefaultProtectionPolicy
+from pyams_sequence.interfaces import ISequentialIdInfo
 from pyams_utils.adapter import ContextAdapter, adapter_config
 from pyams_utils.container import find_objects_providing
 from pyams_utils.registry import get_local_registry
@@ -72,6 +75,15 @@
     edit_permission = MANAGE_SITE_PERMISSION
 
 
+@adapter_config(context=ISiteFolder, provides=IDynamicMenu)
+def site_folder_dynamic_menu_factory(context):
+    """Site folder dynamic menu factory"""
+    result = InternalLink()
+    result.title = context.navigation_title.copy() if context.navigation_title else {}
+    result.reference = ISequentialIdInfo(context).hex_oid
+    return result
+
+
 @vocabulary_config(name='PyAMS site folders')
 class SiteManagerFoldersVocabulary(SimpleVocabulary):
     """Site manager folders vocabulary"""
--- a/src/pyams_content/shared/site/link.py	Tue Dec 11 10:42:37 2018 +0100
+++ b/src/pyams_content/shared/site/link.py	Tue Dec 11 13:50:27 2018 +0100
@@ -18,6 +18,8 @@
 from zope.schema.fieldproperty import FieldProperty
 
 from pyams_content.component.illustration import ILinkIllustrationTarget
+from pyams_content.component.links import ExternalLink, InternalLink
+from pyams_content.features.menu import IDynamicMenu
 from pyams_content.shared.site.interfaces import IContentLink, IExternalContentLink, ISiteElementNavigation, ISiteLink
 from pyams_sequence.reference import get_reference_target
 from pyams_skin.layer import IPyAMSLayer
@@ -75,6 +77,16 @@
         return self.target
 
 
+@adapter_config(context=IContentLink, provides=IDynamicMenu)
+def content_link_dynamic_menu_factory(context):
+    target = context.get_target()
+    if target is not None:
+        result = InternalLink()
+        result.title = context.navigation_title.copy() if context.navigation_title else {}
+        result.reference = context.reference
+        return result
+
+
 @adapter_config(context=(IContentLink, IPyAMSLayer), provides=ISiteElementNavigation)
 class ContentLinkNavigationAdapter(ContextRequestAdapter):
     """Content link navigation adapter"""
@@ -124,6 +136,15 @@
     content_name = _("External content link")
 
 
+@adapter_config(context=IExternalContentLink, provides=IDynamicMenu)
+def external_content_link_dynamic_menu_factory(context):
+    """External content link dynamic menu factory"""
+    result = ExternalLink()
+    result.title = context.navigation_title.copy() if context.navigation_title else {}
+    result.url = context.url
+    return result
+
+
 @adapter_config(context=(IExternalContentLink, IPyAMSLayer), provides=ISiteElementNavigation)
 class ExternalContentLinkNavigationAdapter(ContextRequestAdapter):
     """External content link navigation adapter"""