Back to index

plone3  3.1.7
navigationParent.py
Go to the documentation of this file.
00001 ## Script (Python) "navigationParent"
00002 ##bind container=container
00003 ##bind context=context
00004 ##bind namespace=
00005 ##bind script=script
00006 ##bind subpath=traverse_subpath
00007 ##parameters=obj=None,templateId=None,fallThroughDefaultPage=True,checkPermissions=[]
00008 ##title=Returns the appropriate url for the parent object
00009 ##
00010 
00011 # - If obj is given, use this as the object to find the parent of, else
00012 # use the context.
00013 #
00014 # - If fallThroughDefaultPage is set to True (default), then if obj/context is
00015 # the default page (index_html or default_page) of its parent folder, get the
00016 # parent of that folder. If you intend to link straight to the returned value
00017 # this is probably what you want - else Zope will show the parent object, which
00018 # will in turn show obj again (as it is the default page). However, if you
00019 # intend to append a page template to the link, set this to false to get the
00020 # "real" parent. folder_contents does this.
00021 #
00022 # - If you want to make sure that the current user has permissions other than
00023 # "View" on the parent object, pass these in as a list in checkPermissions.
00024 # folder_contents uses this to check the "List folder contents" permission,
00025 # for example.
00026 #
00027 # - templateId is for historical reasons, ignored
00028 #
00029 # Returns the absolute url to the parent object, or None if it cannot be
00030 # found or accessed
00031 
00032 from ZODB.POSException import ConflictError
00033 from Products.CMFCore.utils import getToolByName, getUtilityByInterfaceName
00034 from AccessControl import Unauthorized
00035 
00036 portal = getUtilityByInterfaceName('Products.CMFCore.interfaces.ISiteRoot')
00037 plone_utils = getToolByName(context, 'plone_utils')
00038 portal_membership = getToolByName(context, 'portal_membership')
00039 
00040 if obj is None:
00041     obj = context
00042 
00043 checkPermission = portal_membership.checkPermission
00044 
00045 if fallThroughDefaultPage:
00046     # In the case that we have an index_html inside and index_html,
00047     # we actually need to go the ultimate non-default parent
00048     try:
00049         while obj is not None and plone_utils.isDefaultPage(obj):
00050             obj = obj.aq_parent
00051     except Unauthorized:
00052         return None
00053 
00054 # Abort if we are at the root of the portal
00055 if obj.getPhysicalPath() == portal.getPhysicalPath():
00056     return None
00057 
00058 # Get the parent. If we can't get it (unauthorized), use the portal
00059 try:
00060     parent = obj.aq_parent
00061 except ConflictError:
00062     raise
00063 except:
00064     return None
00065 
00066 # We may get an unauthorized exception if we're not allowed to access#
00067 # the parent. In this case, return None
00068 try:
00069     if getattr(parent, 'getId', None) is None or \
00070            parent.getId() == 'talkback':
00071         # Skip any Z3 views that may be in the acq tree;
00072         # Skip past the talkback container if that's where we are
00073         parent = parent.aq_parent
00074 
00075     for perm in checkPermissions:
00076         if not checkPermission(perm, parent):
00077             return None
00078     
00079     return parent.absolute_url()
00080 
00081 except Unauthorized:
00082     return None