Back to index

plone3  3.1.7
URLTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ CMFCore portal_url tool.
00014 
00015 $Id: URLTool.py 77186 2007-06-28 19:06:19Z yuppie $
00016 """
00017 
00018 from AccessControl import ClassSecurityInfo
00019 from Acquisition import aq_inner
00020 from Acquisition import aq_parent
00021 from Globals import DTMLFile
00022 from Globals import InitializeClass
00023 from OFS.SimpleItem import SimpleItem
00024 from zope.interface import implements
00025 
00026 from ActionProviderBase import ActionProviderBase
00027 from interfaces import IURLTool
00028 from interfaces.portal_url import portal_url as z2IURLTool
00029 from permissions import ManagePortal
00030 from permissions import View
00031 from utils import _dtmldir
00032 from utils import UniqueObject
00033 
00034 
00035 class URLTool(UniqueObject, SimpleItem, ActionProviderBase):
00036 
00037     """ CMF URL Tool.
00038     """
00039 
00040     implements(IURLTool)
00041     __implements__ = (z2IURLTool, ActionProviderBase.__implements__)
00042 
00043     id = 'portal_url'
00044     meta_type = 'CMF URL Tool'
00045 
00046     security = ClassSecurityInfo()
00047     security.declareObjectProtected(View)
00048 
00049     manage_options = ( ActionProviderBase.manage_options
00050                      + ( {'label':'Overview',
00051                           'action':'manage_overview'}
00052                        ,
00053                        )
00054                      + SimpleItem.manage_options
00055                      )
00056 
00057     #
00058     #   ZMI methods
00059     #
00060     security.declareProtected(ManagePortal, 'manage_overview')
00061     manage_overview = DTMLFile('explainURLTool', _dtmldir)
00062 
00063     #
00064     #   'portal_url' interface methods
00065     #
00066     security.declarePublic('__call__')
00067     def __call__(self, relative=0, *args, **kw):
00068         """ Get by default the absolute URL of the portal.
00069         """
00070         # XXX: this method violates the rules for tools/utilities:
00071         # absolute_url() depends implicitly on REQUEST
00072         return self.getPortalObject().absolute_url(relative=relative)
00073 
00074     security.declarePublic('getPortalObject')
00075     def getPortalObject(self):
00076         """ Get the portal object itself.
00077         """
00078         # XXX: this method violates the rules for tools/utilities:
00079         # queryUtility(ISiteRoot) doesn't work because we need the REQUEST
00080         return aq_parent( aq_inner(self) )
00081 
00082     security.declarePublic('getRelativeContentPath')
00083     def getRelativeContentPath(self, content):
00084         """ Get the path for an object, relative to the portal root.
00085         """
00086         portal_path_length = len( self.getPortalObject().getPhysicalPath() )
00087         content_path = content.getPhysicalPath()
00088         return content_path[portal_path_length:]
00089 
00090     security.declarePublic('getRelativeContentURL')
00091     def getRelativeContentURL(self, content):
00092         """ Get the URL for an object, relative to the portal root.
00093         """
00094         return '/'.join( self.getRelativeContentPath(content) )
00095 
00096     security.declarePublic('getRelativeUrl')
00097     getRelativeUrl = getRelativeContentURL
00098 
00099     security.declarePublic('getPortalPath')
00100     def getPortalPath(self):
00101         """ Get the portal object's URL without the server URL component.
00102         """
00103         return '/'.join( self.getPortalObject().getPhysicalPath() )
00104 
00105 InitializeClass(URLTool)