Back to index

plone3  3.1.7
Expression.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 """ Expressions in a web-configurable workflow.
00014 
00015 $Id: Expression.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 
00018 from AccessControl import ClassSecurityInfo
00019 from Acquisition import aq_base, aq_inner, aq_parent
00020 from Globals import InitializeClass
00021 from Globals import Persistent
00022 from Products.PageTemplates.Expressions import getEngine
00023 from Products.PageTemplates.Expressions import SecureModuleImporter
00024 
00025 from utils import getToolByName
00026 
00027 
00028 class Expression(Persistent):
00029 
00030     text = ''
00031     _v_compiled = None
00032 
00033     security = ClassSecurityInfo()
00034 
00035     def __init__(self, text):
00036         self.text = text
00037         if text.strip():
00038             self._v_compiled = getEngine().compile(text)
00039 
00040     def __call__(self, econtext):
00041         if not self.text.strip():
00042             return ''
00043         compiled = self._v_compiled
00044         if compiled is None:
00045             compiled = self._v_compiled = getEngine().compile(self.text)
00046         # ?? Maybe expressions should manipulate the security
00047         # context stack.
00048         res = compiled(econtext)
00049         if isinstance(res, Exception):
00050             raise res
00051         return res
00052 
00053 InitializeClass(Expression)
00054 
00055 
00056 def getExprContext(context, object=None):
00057     request = getattr(context, 'REQUEST', None)
00058     if request:
00059         cache = request.get('_ec_cache', None)
00060         if cache is None:
00061             request['_ec_cache'] = cache = {}
00062         ec = cache.get( id(object), None )
00063     else:
00064         ec = None
00065     if ec is None:
00066         utool = getToolByName(context, 'portal_url')
00067         portal = utool.getPortalObject()
00068         if object is None or not hasattr(object, 'aq_base'):
00069             folder = portal
00070         else:
00071             folder = object
00072             # Search up the containment hierarchy until we find an
00073             # object that claims it's a folder.
00074             while folder is not None:
00075                 if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
00076                     # found it.
00077                     break
00078                 else:
00079                     folder = aq_parent(aq_inner(folder))
00080         ec = createExprContext(folder, portal, object)
00081         if request:
00082             cache[ id(object) ] = ec
00083     return ec
00084 
00085 
00086 def createExprContext(folder, portal, object):
00087     '''
00088     An expression context provides names for TALES expressions.
00089     '''
00090     pm = getToolByName(portal, 'portal_membership')
00091     if object is None:
00092         object_url = ''
00093     else:
00094         object_url = object.absolute_url()
00095     if pm.isAnonymousUser():
00096         member = None
00097     else:
00098         member = pm.getAuthenticatedMember()
00099     data = {
00100         'object_url':   object_url,
00101         'folder_url':   folder.absolute_url(),
00102         'portal_url':   portal.absolute_url(),
00103         'object':       object,
00104         'folder':       folder,
00105         'portal':       portal,
00106         'nothing':      None,
00107         'request':      getattr(portal, 'REQUEST', None),
00108         'modules':      SecureModuleImporter,
00109         'member':       member,
00110         'here':         object,
00111         }
00112     return getEngine().getContext(data)