Back to index

plone3  3.1.7
root.py
Go to the documentation of this file.
00001 from Acquisition import aq_base
00002 
00003 from plone.app.layout.navigation.interfaces import INavigationRoot
00004 
00005 from Products.CMFCore.utils import getToolByName
00006 from Products.CMFPlone import utils
00007 
00008 def getNavigationRoot(context, relativeRoot=None):
00009     """Get the path to the root of the navigation tree. If context or one of
00010     its parents until (but not including) the portal root implements
00011     INavigationRoot, return this.
00012 
00013     Otherwise, if an explicit root is set in navtree_properties or given as
00014     relativeRoot, use this. If the property is not set or is set to '/', use 
00015     the portal root.
00016     """
00017 
00018     portal_url = getToolByName(context, 'portal_url')
00019     
00020     if not relativeRoot:
00021         portal_properties = getToolByName(context, 'portal_properties')
00022         navtree_properties = getattr(portal_properties, 'navtree_properties')
00023         relativeRoot = navtree_properties.getProperty('root', None)
00024 
00025     portal = portal_url.getPortalObject()
00026     obj = context
00027     while not INavigationRoot.providedBy(obj) and aq_base(obj) is not aq_base(portal):
00028         obj = utils.parent(obj)
00029     if INavigationRoot.providedBy(obj) and aq_base(obj) is not aq_base(portal):
00030         return '/'.join(obj.getPhysicalPath())
00031 
00032     rootPath = relativeRoot
00033     portalPath = portal_url.getPortalPath()
00034     contextPath = '/'.join(context.getPhysicalPath())
00035 
00036     if rootPath:
00037         if rootPath == '/':
00038             return portalPath
00039         else:
00040             if len(rootPath) > 1 and rootPath[0] == '/':
00041                 return portalPath + rootPath
00042             else:
00043                 return portalPath
00044 
00045     # This code is stolen from Sprout, but it's unclear exactly how it
00046     # should work and the test from Sprout isn't directly transferable
00047     # to testNavTree.py, since it's testing something slightly different.
00048     # Hoping Sidnei or someone else with a real use case can do this.
00049     # The idea is that if the 'root' variable is set to '', you'll get
00050     # the virtual root. This should probably also be used by the default
00051     # search, as well as the tabs and breadcrumbs. Also, the text in
00052     # prefs_navigation_form.cpt should be updated if this is re-enabled.
00053     #
00054     # Attempt to get use the virtual host root as root if an explicit
00055     # root is not set
00056     # if rootPath == '':
00057     #    request = getattr(context, 'REQUEST', None)
00058     #    if request is not None:
00059     #        vroot = request.get('VirtualRootPhysicalPath', None)
00060     #        if vroot is not None:
00061     #            return '/'.join(('',) + vroot[len(portalPath):])
00062 
00063     # Fall back on the portal root
00064     if not rootPath:
00065         return portalPath