Updated menu interface
authorThierry Florac <thierry.florac@onf.fr>
Fri, 23 Nov 2018 12:12:52 +0100
changeset 1110 b368b0b2c5d2
parent 1109 fa671427ce7c
child 1111 e937fd3c40c2
Updated menu interface
src/pyams_content/features/menu/__init__.py
src/pyams_content/features/menu/interfaces.py
--- a/src/pyams_content/features/menu/__init__.py	Fri Nov 23 09:23:38 2018 +0100
+++ b/src/pyams_content/features/menu/__init__.py	Fri Nov 23 12:12:52 2018 +0100
@@ -16,11 +16,17 @@
 from zope.schema.fieldproperty import FieldProperty
 
 from pyams_content.component.association.container import AssociationContainer
-from pyams_content.component.links import InternalReferenceMixin
+from pyams_content.component.association.interfaces import IAssociationInfo
+from pyams_content.component.links import InternalReferenceMixin, InternalLinkAssociationInfoAdapter
 from pyams_content.features.menu.interfaces import IMenu, IMenuLink, IMenusContainer
 from pyams_content.reference.pictograms import IPictogramTable
+from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import query_utility
+from pyams_utils.request import check_request
+from pyams_utils.url import relative_url
 from pyams_utils.zodb import volatile_property
+from pyams_workflow.interfaces import IWorkflowPublicationInfo
+from pyams_zmi.layer import IAdminLayer
 
 
 #
@@ -52,10 +58,35 @@
         if table is not None:
             return table.get(self._pictogram_name)
 
+    def is_visible(self, request=None):
+        if not self.reference:
+            return True
+        target = self.get_target()
+        if target is not None:
+            publication_info = IWorkflowPublicationInfo(target, None)
+            if publication_info is not None:
+                return publication_info.is_visible(request)
+
+    def get_url(self, request=None, view_name=None):
+        target = self.get_target()
+        if target is not None:
+            if request is None:
+                request = check_request()
+            return relative_url(target, request, view_name=view_name)
+        else:
+            return ''
+
+
+@adapter_config(context=IMenu, provides=IAssociationInfo)
+class MenuAssociationInfoAdapter(InternalLinkAssociationInfoAdapter):
+    """Menu association adapter"""
+
 
 @implementer(IMenusContainer)
 class MenusContainer(AssociationContainer):
     """Associations menus container"""
 
     def get_visible_items(self, request=None):
-        yield from filter(lambda x: IMenu(x).visible, self.values())
+        for menu in filter(lambda x: IMenu(x).visible, self.values()):
+            if IAdminLayer.providedBy(request) or menu.is_visible(request):
+                yield menu
--- a/src/pyams_content/features/menu/interfaces.py	Fri Nov 23 09:23:38 2018 +0100
+++ b/src/pyams_content/features/menu/interfaces.py	Fri Nov 23 12:12:52 2018 +0100
@@ -57,6 +57,9 @@
                    required=True,
                    default=True)
 
+    def is_visible(self, request=None):
+        """Is menu visible?"""
+
     title = I18nTextLineField(title=_("Menu title"),
                               description=_("Displayed menu label"),
                               required=True)
@@ -72,6 +75,9 @@
 
     pictogram = Attribute("Pictogram object reference")
 
+    def get_url(self, request=None, view_name=None):
+        """Menu menu target URL"""
+
 
 class IMenusContainer(IAssociationContainer):
     """Menus container interface"""