Back to index

plone3  3.1.7
referencebrowser_startupDirectory.py
Go to the documentation of this file.
00001 ## Script (Python) "referencebrowser_startupDirectory"
00002 ##bind container=container
00003 ##bind context=context
00004 ##bind namespace=
00005 ##bind script=script
00006 ##bind subpath=traverse_subpath
00007 ##parameters=directory=''
00008 ##title=
00009 ##
00010 
00011 from Products.CMFCore.utils import getToolByName
00012 
00013 # Mapping works as follows:
00014 #
00015 #  directory == ''                  => Current object
00016 #  directory == '/absolute/url'     => Portal root + absolute url
00017 #  directory == '../relative/url'   => Current object + relative url
00018 #
00019 # If the object is in the portal_factory, remove the factory from the equation.
00020 # This creates an inconsistency with the case when directory is not set,
00021 # because the current object is in the factory and thus not generally useful
00022 # as a starting point for browsing (it won't contain any sub-objects, and the
00023 # parent object is the factory's temporary folder). Hence, in this case, the
00024 # startup directory is the parent folder.
00025 #
00026 # Similarly, if directory is a relative path starting with '../' and the object
00027 # is in the factory, let the first '../' part of the relative URL refer to the
00028 # destination parent folder, not the factory.
00029 
00030 def filterPortalFactory (url):
00031     """Return context's url + the relative url given, but remove any
00032     reference to portal_factory.
00033     """
00034 
00035     portal_factory = getToolByName (context, 'portal_factory')
00036 
00037     # Prepend / to ensure proper path separation, and ensure url is a string
00038     if url:
00039         url = '/' + url
00040     else:
00041         url = ''
00042     basePath = ''
00043 
00044     if portal_factory.isTemporary (context):
00045         pathParts = context.getPhysicalPath ()
00046 
00047         # Remove the factory from the path
00048         pathParts = pathParts[:-3]
00049 
00050         # If the object is in the portal factory, we'll be relative to the
00051         # parent folder, not the temporary object which does not yet exist,
00052         # so remove any explicit ../ from the relative path
00053         if url.startswith ('/..'):
00054             url = url[3:]
00055 
00056         basePath = '/'.join (pathParts)
00057     else:
00058         basePath = context.absolute_url (relative = 1)
00059 
00060 
00061     # Resolve the URL
00062     try:
00063         targetPath = basePath + url
00064         object = context.restrictedTraverse (targetPath)
00065         return object.absolute_url ()
00066     except:
00067         return context.absolute_url ()
00068 
00069 # checks if path starts with / - if, then
00070 # path is relative to portal root
00071 def checkPath(path):
00072     if path.startswith('/'):
00073         portal_url = getToolByName (context, 'portal_url')
00074         return portal_url () + path
00075     else:
00076         return path
00077 
00078 #
00079 # Main execution
00080 #
00081 
00082 # Default case - if no directory is given, search for a property
00083 # refwidget_startupdirectories in portal_properties/site_properties
00084 # that is a lines field having the following
00085 # form:
00086 #    path1:path2
00087 # path1 is the path where all widgets being under it set startup_directory
00088 # to path2 if no startup_directory is set.
00089 if directory.strip() == '':
00090 
00091     props = getToolByName(context, 'portal_properties').site_properties
00092     if hasattr(props, 'refwidget_startupdirectories'):
00093         startups = props.refwidget_startupdirectories
00094         ownpath = '/'.join(context.getPhysicalPath())
00095 
00096         # remove portal path - / is always portal_root
00097         purl = '/'.join(getToolByName(context, 'portal_url').getPortalObject().getPhysicalPath())
00098         ownpath = ownpath.replace(purl, '')
00099         
00100         for pathdef in startups:
00101             psplit = pathdef.split(':')
00102             if ownpath[0:len(psplit[0])] == psplit[0]:
00103                 dopath = psplit[1].strip()
00104                 if checkPath(dopath) == dopath:
00105                     return filterPortalFactory(dopath)
00106                 else: return checkPath(dopath)
00107     
00108     return filterPortalFactory (None)
00109 
00110 # If we have an absolute URL, return it relative to the portal root
00111 if checkPath(directory) != directory:
00112     return checkPath(directory)
00113     
00114 # Else, if we have a relative URL, get it relative to the context.
00115 return filterPortalFactory (directory)