Back to index

plone3  3.1.7
PloneBaseTool.py
Go to the documentation of this file.
00001 from Globals import InitializeClass
00002 from AccessControl import ClassSecurityInfo
00003 from Products.CMFPlone.interfaces.PloneBaseTool import IPloneBaseTool \
00004      as z2IPloneBaseTool
00005 from Products.CMFPlone.interfaces import IPloneBaseTool
00006 from Acquisition import aq_base
00007 from Acquisition import aq_parent
00008 from Acquisition import aq_inner
00009 
00010 from Products.CMFCore import Expression
00011 from Products.CMFCore.utils import getToolByName
00012 
00013 from zope.interface import implements
00014 from zope.component import getMultiAdapter
00015 
00016 TempFolderClass = None
00017 
00018 # getOAI() and getExprContext copied from CMF 1.5.1+cvs
00019 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
00020 # ZPL 2.1
00021 from Products.CMFCore.ActionInformation import oai
00022 
00023 def initializeTFC():
00024     """To work around circular imports ...
00025     """
00026     global TempFolderClass
00027     if TempFolderClass is None:
00028          from Products.CMFPlone.FactoryTool import TempFolder
00029          TempFolderClass = TempFolder
00030 
00031 def getOAI(context, object=None):
00032     initializeTFC()
00033     request = getattr(context, 'REQUEST', None)
00034     if request:
00035         cache = request.get('_oai_cache', None)
00036         if cache is None:
00037             request['_oai_cache'] = cache = {}
00038         info = cache.get( id(object), None )
00039     else:
00040         info = None
00041     if info is None:
00042         if object is None or not hasattr(object, 'aq_base'):
00043             folder = None
00044         else:
00045             folder = object
00046             # Search up the containment hierarchy until we find an
00047             # object that claims it's a folder.
00048             while folder is not None:
00049                 if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
00050                     # found it.
00051                     break
00052                 else:
00053                     # If the parent of the object at hand is a TempFolder
00054                     # don't strip off its outer acquisition context (Plone)
00055                     parent = aq_parent(aq_inner(folder))
00056                     if getattr(parent, '__class__', None) == TempFolderClass:
00057                         folder = aq_parent(folder)
00058                     else:
00059                         folder = parent
00060         info = oai(context, folder, object)
00061         if request:
00062             cache[ id(object) ] = info
00063     return info
00064 
00065 def createExprContext(folder, portal, object):
00066     expr_context = Expression.createExprContext(folder, portal, object)
00067     view_obj = object
00068     if view_obj is None:
00069         view_obj = portal
00070     req = view_obj.REQUEST
00071     
00072     globals_view = getMultiAdapter((view_obj, req), name='plone')
00073     expr_context.setGlobal('globals_view', globals_view)
00074     
00075     # XXX: For some reason, when using getMultiAdapter() here we get 
00076     # authoriziation problems in some cases (e.g. when using one of these
00077     # in a python: expression in an action).
00078     
00079     plone_portal_state = view_obj.restrictedTraverse('@@plone_portal_state')
00080     expr_context.setGlobal('plone_portal_state', plone_portal_state)
00081     
00082     plone_context_state = view_obj.restrictedTraverse('@@plone_context_state')
00083     expr_context.setGlobal('plone_context_state', plone_context_state)
00084     
00085     plone_tools = view_obj.restrictedTraverse('@@plone_tools')
00086     expr_context.setGlobal('plone_tools', plone_tools)
00087     
00088     # Add checkPermission to the action expression context to make cleaner
00089     # faster expressions
00090     membership_tool = getToolByName(view_obj, 'portal_membership')
00091     checkPerm = membership_tool.checkPermission
00092     expr_context.setGlobal('checkPermission', checkPerm)
00093 
00094     return expr_context
00095 
00096 def getExprContext(context, object=None):
00097     initializeTFC()
00098     request = getattr(context, 'REQUEST', None)
00099     if request:
00100         cache = request.get('_ec_cache', None)
00101         if cache is None:
00102             request['_ec_cache'] = cache = {}
00103         ec = cache.get( id(object), None )
00104     else:
00105         ec = None
00106     if ec is None:
00107         utool = getToolByName(context, 'portal_url')
00108         portal = utool.getPortalObject()
00109         if object is None or not hasattr(object, 'aq_base'):
00110             folder = portal
00111         else:
00112             folder = object
00113             # Search up the containment hierarchy until we find an
00114             # object that claims it's a folder.
00115             while folder is not None:
00116                 if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
00117                     # found it.
00118                     break
00119                 else:
00120                     # If the parent of the object at hand is a TempFolder
00121                     # don't strip off its outer acquisition context (Plone)
00122                     parent = aq_parent(aq_inner(folder))
00123                     if getattr(parent, '__class__', None) == TempFolderClass:
00124                         folder = aq_parent(folder)
00125                     else:
00126                         folder = parent
00127         ec = createExprContext(folder, portal, object)
00128         if request:
00129             cache[ id(object) ] = ec
00130     return ec
00131 
00132 class PloneBaseTool:
00133     """Base class of all tools used in CMFPlone and Plone Core
00134     """
00135 
00136     security = ClassSecurityInfo()
00137 
00138     __implements__ = z2IPloneBaseTool
00139 
00140     implements(IPloneBaseTool)
00141 
00142     # overwrite getOAI and getExprContext to use our variants that understand the
00143     # temp folder of portal factory
00144     def _getOAI(self, object):
00145         return getOAI(self, object)
00146 
00147     def _getExprContext(self, object):
00148         return getExprContext(self, object)
00149 
00150 InitializeClass(PloneBaseTool)