vendor/CMF/1.6.1/DCWorkflow

view Expression.py @ 0:238bab7e7116

CMF 1.6.1 vendor import
author fguillaume
date Tue, 13 Jun 2006 14:57:59 +0000
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: Expression.py 68537 2006-06-09 13:40:32Z efge $
16 """
18 import Globals
19 from Globals import Persistent
20 from Acquisition import aq_inner, aq_parent
21 from AccessControl import getSecurityManager, ClassSecurityInfo
22 from DateTime import DateTime
24 from Products.CMFCore.WorkflowCore import ObjectDeleted, ObjectMoved
25 from Products.CMFCore.Expression import Expression
26 from Products.PageTemplates.Expressions import getEngine
27 from Products.PageTemplates.Expressions import SecureModuleImporter
30 # We don't import SafeMapping from Products.PageTemplates.TALES
31 # because it's deprecated in Zope 2.10
32 from MultiMapping import MultiMapping
33 class SafeMapping(MultiMapping):
34 """Mapping with security declarations and limited method exposure.
36 Since it subclasses MultiMapping, this class can be used to wrap
37 one or more mapping objects. Restricted Python code will not be
38 able to mutate the SafeMapping or the wrapped mappings, but will be
39 able to read any value.
40 """
41 __allow_access_to_unprotected_subobjects__ = 1
42 push = pop = None
43 _push = MultiMapping.push
44 _pop = MultiMapping.pop
47 class StateChangeInfo:
48 '''
49 Provides information for expressions and scripts.
50 '''
51 _date = None
53 ObjectDeleted = ObjectDeleted
54 ObjectMoved = ObjectMoved
56 security = ClassSecurityInfo()
57 security.setDefaultAccess('allow')
59 def __init__(self, object, workflow, status=None, transition=None,
60 old_state=None, new_state=None, kwargs=None):
61 if kwargs is None:
62 kwargs = {}
63 else:
64 # Don't allow mutation
65 kwargs = SafeMapping(kwargs)
66 if status is None:
67 tool = aq_parent(aq_inner(workflow))
68 status = tool.getStatusOf(workflow.id, object)
69 if status is None:
70 status = {}
71 if status:
72 # Don't allow mutation
73 status = SafeMapping(status)
74 self.object = object
75 self.workflow = workflow
76 self.old_state = old_state
77 self.new_state = new_state
78 self.transition = transition
79 self.status = status
80 self.kwargs = kwargs
82 def __getitem__(self, name):
83 if name[:1] != '_' and hasattr(self, name):
84 return getattr(self, name)
85 raise KeyError, name
87 def getHistory(self):
88 wf = self.workflow
89 tool = aq_parent(aq_inner(wf))
90 wf_id = wf.id
91 h = tool.getHistoryOf(wf_id, self.object)
92 if h:
93 return map(lambda dict: dict.copy(), h) # Don't allow mutation
94 else:
95 return ()
97 def getPortal(self):
98 ob = self.object
99 while ob is not None and not getattr(ob, '_isPortalRoot', 0):
100 ob = aq_parent(aq_inner(ob))
101 return ob
103 def getDateTime(self):
104 date = self._date
105 if not date:
106 date = self._date = DateTime()
107 return date
109 Globals.InitializeClass(StateChangeInfo)
112 def createExprContext(sci):
113 '''
114 An expression context provides names for TALES expressions.
115 '''
116 ob = sci.object
117 wf = sci.workflow
118 container = aq_parent(aq_inner(ob))
119 data = {
120 'here': ob,
121 'object': ob,
122 'container': container,
123 'folder': container,
124 'nothing': None,
125 'root': wf.getPhysicalRoot(),
126 'request': getattr( ob, 'REQUEST', None ),
127 'modules': SecureModuleImporter,
128 'user': getSecurityManager().getUser(),
129 'state_change': sci,
130 'transition': sci.transition,
131 'status': sci.status,
132 'kwargs': sci.kwargs,
133 'workflow': wf,
134 'scripts': wf.scripts,
135 }
136 return getEngine().getContext(data)