vendor/CMF/1.6.3/CMFCore

view Expression.py @ 2:4c712d7bd1d7

Added tag 1.6.3 for changeset 1babb9d61518
author Georges Racinet on purity.racinet.fr <georges@racinet.fr>
date Fri, 09 Sep 2011 12:44:00 +0200
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Expressions in a web-configurable workflow.
15 $Id$
16 """
18 from AccessControl import ClassSecurityInfo
19 from Acquisition import aq_base, aq_inner, aq_parent
20 from Globals import InitializeClass
21 from Globals import Persistent
22 from Products.PageTemplates.Expressions import getEngine
23 from Products.PageTemplates.Expressions import SecureModuleImporter
25 from utils import getToolByName
28 class Expression (Persistent):
29 text = ''
30 _v_compiled = None
32 security = ClassSecurityInfo()
34 def __init__(self, text):
35 self.text = text
36 self._v_compiled = getEngine().compile(text)
38 def __call__(self, econtext):
39 compiled = self._v_compiled
40 if compiled is None:
41 compiled = self._v_compiled = getEngine().compile(self.text)
42 # ?? Maybe expressions should manipulate the security
43 # context stack.
44 res = compiled(econtext)
45 if isinstance(res, Exception):
46 raise res
47 #print 'returning %s from %s' % (`res`, self.text)
48 return res
50 InitializeClass(Expression)
53 def getExprContext(context, object=None):
54 request = getattr(context, 'REQUEST', None)
55 if request:
56 cache = request.get('_ec_cache', None)
57 if cache is None:
58 request['_ec_cache'] = cache = {}
59 ec = cache.get( id(object), None )
60 else:
61 ec = None
62 if ec is None:
63 utool = getToolByName(context, 'portal_url')
64 portal = utool.getPortalObject()
65 if object is None or not hasattr(object, 'aq_base'):
66 folder = portal
67 else:
68 folder = object
69 # Search up the containment hierarchy until we find an
70 # object that claims it's a folder.
71 while folder is not None:
72 if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
73 # found it.
74 break
75 else:
76 folder = aq_parent(aq_inner(folder))
77 ec = createExprContext(folder, portal, object)
78 if request:
79 cache[ id(object) ] = ec
80 return ec
83 def createExprContext(folder, portal, object):
84 '''
85 An expression context provides names for TALES expressions.
86 '''
87 pm = getToolByName(portal, 'portal_membership')
88 if object is None:
89 object_url = ''
90 else:
91 object_url = object.absolute_url()
92 if pm.isAnonymousUser():
93 member = None
94 else:
95 member = pm.getAuthenticatedMember()
96 data = {
97 'object_url': object_url,
98 'folder_url': folder.absolute_url(),
99 'portal_url': portal.absolute_url(),
100 'object': object,
101 'folder': folder,
102 'portal': portal,
103 'nothing': None,
104 'request': getattr(portal, 'REQUEST', None),
105 'modules': SecureModuleImporter,
106 'member': member,
107 'here': object,
108 }
109 return getEngine().getContext(data)