vendor/CMF/1.6.3/CMFCore

view ActionsTool.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +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 """ Basic action list tool.
15 $Id$
16 """
18 from warnings import warn
20 from AccessControl import ClassSecurityInfo
21 from Acquisition import aq_base
22 from Globals import DTMLFile
23 from Globals import InitializeClass
24 from OFS.Folder import Folder
26 from ActionInformation import ActionInformation
27 from ActionProviderBase import ActionProviderBase
28 from Expression import Expression
29 from interfaces.portal_actions import ActionProvider as IActionProvider
30 from interfaces.portal_actions import portal_actions as IActionsTool
31 from permissions import ListFolderContents
32 from permissions import ManagePortal
33 from utils import _checkPermission
34 from utils import _dtmldir
35 from utils import SimpleItemWithProperties
36 from utils import UniqueObject
39 class ActionsTool(UniqueObject, Folder, ActionProviderBase):
40 """
41 Weave together the various sources of "actions" which are apropos
42 to the current user and context.
43 """
45 __implements__ = (IActionsTool, ActionProviderBase.__implements__)
47 id = 'portal_actions'
48 meta_type = 'CMF Actions Tool'
49 _actions = (ActionInformation(id='folderContents'
50 , title='Folder contents'
51 , action=Expression(
52 text='string:${folder_url}/folder_contents')
53 , condition=Expression(
54 text='python: folder is not object')
55 , permissions=(ListFolderContents,)
56 , category='folder'
57 , visible=1
58 )
59 ,
60 )
62 action_providers = ( 'portal_membership'
63 , 'portal_actions'
64 , 'portal_registration'
65 , 'portal_types'
66 , 'portal_discussion'
67 , 'portal_undo'
68 , 'portal_syndication'
69 , 'portal_workflow'
70 , 'portal_properties'
71 )
73 security = ClassSecurityInfo()
75 manage_options = ( ActionProviderBase.manage_options
76 + ( { 'label' : 'Action Providers'
77 , 'action' : 'manage_actionProviders'
78 }
79 , { 'label' : 'Overview'
80 , 'action' : 'manage_overview'
81 }
82 ) + Folder.manage_options
83 )
85 #
86 # ZMI methods
87 #
88 security.declareProtected(ManagePortal, 'manage_overview')
89 manage_overview = DTMLFile( 'explainActionsTool', _dtmldir )
90 manage_actionProviders = DTMLFile('manageActionProviders', _dtmldir)
92 security.declareProtected(ManagePortal, 'manage_aproviders')
93 def manage_aproviders(self
94 , apname=''
95 , chosen=()
96 , add_provider=0
97 , del_provider=0
98 , REQUEST=None):
99 """
100 Manage action providers through-the-web.
101 """
102 providers = list(self.listActionProviders())
103 new_providers = []
104 if add_provider:
105 providers.append(apname)
106 elif del_provider:
107 for item in providers:
108 if item not in chosen:
109 new_providers.append(item)
110 providers = new_providers
111 self.action_providers = tuple(providers)
112 if REQUEST is not None:
113 return self.manage_actionProviders(self , REQUEST
114 , manage_tabs_message='Providers changed.')
116 #
117 # Programmatically manipulate the list of action providers
118 #
119 security.declareProtected(ManagePortal, 'listActionProviders')
120 def listActionProviders(self):
121 """ List the ids of all Action Providers queried by this tool.
122 """
123 return self.action_providers
125 security.declareProtected(ManagePortal, 'addActionProvider')
126 def addActionProvider( self, provider_name ):
127 """ Add an Action Provider id to the providers queried by this tool.
128 """
129 ap = list( self.action_providers )
130 if hasattr( self, provider_name ) and provider_name not in ap:
131 ap.append( provider_name )
132 self.action_providers = tuple( ap )
134 security.declareProtected(ManagePortal, 'deleteActionProvider')
135 def deleteActionProvider( self, provider_name ):
136 """ Delete an Action Provider id from providers queried by this tool.
137 """
138 ap = list( self.action_providers )
139 if provider_name in ap:
140 ap.remove( provider_name )
141 self.action_providers = tuple( ap )
143 #
144 # 'portal_actions' interface methods
145 #
146 security.declarePublic('listFilteredActionsFor')
147 def listFilteredActionsFor(self, object=None):
148 """ List all actions available to the user.
149 """
150 actions = []
152 # Include actions from specific tools.
153 for provider_name in self.listActionProviders():
154 provider = getattr(self, provider_name)
155 if IActionProvider.isImplementedBy(provider):
156 actions.extend( provider.listActionInfos(object=object) )
157 else:
158 # for Action Providers written for CMF versions before 1.5
159 actions.extend( self._listActionInfos(provider, object) )
161 # Include actions from object.
162 if object is not None:
163 base = aq_base(object)
164 if IActionProvider.isImplementedBy(base):
165 actions.extend( object.listActionInfos(object=object) )
166 elif hasattr(base, 'listActions'):
167 # for objects written for CMF versions before 1.5
168 actions.extend( self._listActionInfos(object, object) )
170 # Reorganize the actions by category.
171 filtered_actions={'user':[],
172 'folder':[],
173 'object':[],
174 'global':[],
175 'workflow':[],
176 }
178 for action in actions:
179 catlist = filtered_actions.setdefault(action['category'], [])
180 catlist.append(action)
182 return filtered_actions
184 # listFilteredActions() is an alias.
185 security.declarePublic('listFilteredActions')
186 listFilteredActions = listFilteredActionsFor
188 #
189 # Helper method for backwards compatibility
190 #
191 def _listActionInfos(self, provider, object):
192 """ for Action Providers written for CMF versions before 1.5
193 """
194 warn('ActionProvider interface not up to date. In CMF 2.0 '
195 'portal_actions will ignore listActions() of \'%s\'.'
196 % provider.getId(),
197 DeprecationWarning)
198 info = self._getOAI(object)
199 actions = provider.listActions(info)
201 action_infos = []
202 if actions and not isinstance(actions[0], dict):
203 ec = self._getExprContext(object)
204 for ai in actions:
205 if not ai.getVisibility():
206 continue
207 permissions = ai.getPermissions()
208 if permissions:
209 category = ai.getCategory()
210 if (object is not None and
211 (category.startswith('object') or
212 category.startswith('workflow'))):
213 context = object
214 elif (info['folder'] is not None and
215 category.startswith('folder')):
216 context = info['folder']
217 else:
218 context = info['portal']
219 for permission in permissions:
220 allowed = _checkPermission(permission, context)
221 if allowed:
222 break
223 if not allowed:
224 continue
225 if not ai.testCondition(ec):
226 continue
227 action_infos.append( ai.getAction(ec) )
228 else:
229 for i in actions:
230 if not i.get('visible', 1):
231 continue
232 permissions = i.get('permissions', None)
233 if permissions:
234 category = i['category']
235 if (object is not None and
236 (category.startswith('object') or
237 category.startswith('workflow'))):
238 context = object
239 elif (info['folder'] is not None and
240 category.startswith('folder')):
241 context = info['folder']
242 else:
243 context = info['portal']
245 for permission in permissions:
246 allowed = _checkPermission(permission, context)
247 if allowed:
248 break
249 if not allowed:
250 continue
251 action_infos.append(i)
252 return action_infos
254 InitializeClass(ActionsTool)